当前位置:网站首页>分糖果
分糖果
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
边栏推荐
- OneNote 深度评测:使用资源、插件、模版
- Pat 1085 perfect sequence (25 points) perfect sequence
- js通过数组内容来获取数组下标
- Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
- 快过年了,心也懒了
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
- 监控界的最强王者,没有之一!
- js中,字符串和数组互转(二)——数组转为字符串的方法
- Xcode6 error: "no matching provisioning profiles found for application"
- 基于STM32单片机设计的红外测温仪(带人脸检测)
猜你喜欢

968 edit distance

全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎

Detailed explanation of knowledge map construction process steps

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

Aike AI frontier promotion (7.6)

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

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

请问sql group by 语句问题

PHP online examination system version 4.0 source code computer + mobile terminal

3D人脸重建:从基础知识到识别/重建方法!
随机推荐
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
The biggest pain point of traffic management - the resource utilization rate cannot go up
嵌入式开发的7大原罪
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
愛可可AI前沿推介(7.6)
R语言可视化两个以上的分类(类别)变量之间的关系、使用vcd包中的Mosaic函数创建马赛克图( Mosaic plots)、分别可视化两个、三个、四个分类变量的关系的马赛克图
监控界的最强王者,没有之一!
Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
How do I remove duplicates from the list- How to remove duplicates from a list?
Entity alignment two of knowledge map
Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
基于深度学习的参考帧生成
【论文解读】用于白内障分级/分类的机器学习技术
Swagger UI教程 API 文档神器
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
Manifest of SAP ui5 framework json
@PathVariable
Three schemes of SVM to realize multi classification