当前位置:网站首页>分糖果
分糖果
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
边栏推荐
- @GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
- 拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
- OAI 5G NR+USRP B210安装搭建
- 20220211 failure - maximum amount of data supported by mongodb
- Entity alignment two of knowledge map
- 1_ Introduction to go language
- Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
- 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
- 防火墙基础之外网服务器区部署和双机热备
- 【mysql】游标的基本使用
猜你喜欢

Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)

968 edit distance

新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀

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

Performance test process and plan

基于STM32单片机设计的红外测温仪(带人脸检测)

Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?

SAP UI5 框架的 manifest.json

【mysql】游标的基本使用

【微信小程序】運行機制和更新機制
随机推荐
Why do job hopping take more than promotion?
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
快过年了,心也懒了
7. Data permission annotation
document.write()的用法-写入文本——修改样式、位置控制
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
JS操作dom元素(一)——获取DOM节点的六种方式
PHP saves session data to MySQL database
Pycharm remote execution
PHP online examination system version 4.0 source code computer + mobile terminal
Nodejs教程之Expressjs一篇文章快速入门
The mail command is used in combination with the pipeline command statement
How do I remove duplicates from the list- How to remove duplicates from a list?
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
What are RDB and AOF
【微信小程序】運行機制和更新機制
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
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
华为设备命令
Nodejs教程之让我们用 typescript 创建你的第一个 expressjs 应用程序