当前位置:网站首页>分糖果
分糖果
2022-07-06 12:55:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
分糖果
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 18 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
童年的我们将和朋友分享美好的事物作为自己的快乐。这天,C小朋友得到了糖果,将要把这些糖果分给要好的朋友们。已知糖果从一个人传给还有一个人须要1秒的时间,同一个小朋友不会反复接受糖果。因为糖果足够多,假设某时刻某小朋友接受了糖果。他会将糖果分成若干份,分给那些在他身旁且还没有得到糖果的小朋友们,并且自己会吃一些糖果。因为嘴馋,小朋友们等不及将糖果发完,会在得到糖果后边吃边发。每一个小朋友从接受糖果到吃完糖果须要m秒的时间。那么,假设第一秒C小朋友開始发糖,第几秒全部小朋友都吃完了糖呢?
Input
输入有多组数据,每组数据第1行为三个数n(<=10000),p(<=600000),c为小朋友数,关系数和C小朋友的编号。 第2行为一个数m(<=8000),表示小朋友吃糖的时间。 以下p行每行两个整数,表示某两个小朋友在彼此身旁。
Output
对于每组输入输出一个单独的整数表示从Ts到Te的最小总费用。数据保证至少存在一条道路。
Sample Input
4 3 1
2
1 2
2 3
1 4Sample Output
5
Hint
第一秒,糖在1手上。第二秒,糖传到了2、4的手中。第三秒。糖传到了3的手中,此时1吃完了。第四秒,2、4吃完了。第五秒。3吃完了。所以答案是5。Author
HYNU
//思路就是找出与1相隔最远的那个点到1的距离,首先把每一条路径的权值都标记为1。找出最远点加上m就是答案。由于最后一个人吃糖须要m时间,假设最后这个这个人都吃完了,那他前面的人肯定已经吃完了。 在找的时候用到的是图的广度优先搜索,由于n非常大。那么使用vector动态数组来存储边。
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
vector<int>num[10001];
queue<int>q;
int vis[10001],f[10001];
void bfs()
{
int i,t;
while(!q.empty())
{
t=q.front();
q.pop();
for(i=0;i<num[t].size();i++)
{
if(!vis[num[t][i]])
{
vis[num[t][i]]=1;
f[num[t][i]]=f[t]+1;
q.push(num[t][i]);
}
}
}
}
int main()
{
int n,p,c,m,i,a,b;
while(scanf("%d%d%d%d",&n,&p,&c,&m)!=EOF)
{
for(i=0;i<=n;i++)
num[i].clear();
memset(vis,0,sizeof(vis));
memset(f,0,sizeof(f));
while(!q.empty()) q.pop();
for(i=0;i<p;i++)
{
scanf("%d%d",&a,&b);
num[a].push_back(b);
num[b].push_back(a);
}
q.push(c);
vis[c]=1;
f[c]=1;
bfs();
int ans=0;
for(i=1;i<=n;i++)
{
if(f[i]>ans) ans=f[i];
//printf("%d\n",f[i]);
}
printf("%d\n",ans+m);
}
return 0;
}版权声明:本文博主原创文章。博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117097.html原文链接:https://javaforall.cn
边栏推荐
- SAP UI5 框架的 manifest.json
- User defined current limiting annotation
- Huawei device command
- Swagger UI tutorial API document artifact
- 'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- 华为设备命令
- document.write()的用法-写入文本——修改样式、位置控制
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- Reflection operation exercise
猜你喜欢

硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件

Deployment of external server area and dual machine hot standby of firewall Foundation

【mysql】游标的基本使用

ICML 2022 | flowformer: task generic linear complexity transformer

Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an

Data Lake (VIII): Iceberg data storage format

SAP UI5 框架的 manifest.json

3D face reconstruction: from basic knowledge to recognition / reconstruction methods!

OAI 5g nr+usrp b210 installation and construction

Entity alignment two of knowledge map
随机推荐
快过年了,心也懒了
Dynamically switch data sources
Data Lake (VIII): Iceberg data storage format
Leetcode hot topic Hot 100 day 32: "minimum coverage substring"
2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
What's the best way to get TFS to output each project to its own directory?
Why do job hopping take more than promotion?
js之遍历数组、字符串
【mysql】游标的基本使用
3D人脸重建:从基础知识到识别/重建方法!
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
C language operators
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
1_ Introduction to go language
数据湖(八):Iceberg数据存储格式
@PathVariable
C language games - minesweeping
性能测试过程和计划
Reinforcement learning - learning notes 5 | alphago
OSPF multi zone configuration