当前位置:网站首页>Divide candy
Divide candy
2022-07-06 21:14:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
Divide candy
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
In childhood, we will share good things with our friends as our own happiness . On the day ,C The children got candy , I'm going to give these sweets to my best friends . It is known that candy is passed from one person to another who needs 1 The second time , The same child will not accept candy repeatedly . Because there are enough sweets , Suppose a child accepts candy at some time . He will divide the candy into several parts , Give it to the children who are beside him and haven't got candy yet , And I will eat some candy . Because of greediness , The children can't wait to send out the candy , Will eat and distribute after getting candy . Every child needs... From accepting candy to eating it m The second time . that , Suppose the first second C The children began to send sugar , In a few seconds, all the children finished eating sugar ?
Input
Input has multiple sets of data , The data of each group is no 1 Act three numbers n(<=10000),p(<=600000),c Count the children , Relationship number and C Number... Children . The first 2 Behavior a number m(<=8000), It means the time for children to eat sugar . following p Two integers per line , It means that two children are beside each other .
Output
For each set of inputs, output a separate integer representation from Ts To Te The minimum total cost of . Data guarantees that there is at least one path .
Sample Input
4 3 1
2
1 2
2 3
1 4
Sample Output
5
Hint
The first second , Sugar in 1 On hand . The second seconds , The sugar reached 2、4 In the hands of . The third second . The sugar reached 3 In the hands of , here 1 I'm full . The fourth second ,2、4 I'm full . The fifth second .3 I'm full . So the answer is 5.
Author
HYNU
// The idea is to find out and 1 From the farthest point to 1 Distance of , First, mark the weight of each path as 1. Find the farthest point and add m That's the answer. . Because the last person to eat sugar needs m Time , Suppose this person has finished eating in the end , Then the person in front of him must have finished . When searching, we use the breadth first search of graph , because n A very large . So use vector Dynamic array to store edges .
#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;
}
Copyright notice : This article is the original article of the blogger . Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117097.html Link to the original text :https://javaforall.cn
边栏推荐
- 分糖果
- No Yum source to install SPuG monitoring
- 面试官:Redis中有序集合的内部实现方式是什么?
- js中,字符串和数组互转(一)——字符串转为数组的方法
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
- Spiral square PTA
- for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
- 性能测试过程和计划
- 数据湖(八):Iceberg数据存储格式
- JS operation DOM element (I) -- six ways to obtain DOM nodes
猜你喜欢
Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
Reference frame generation based on deep learning
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Why do job hopping take more than promotion?
Swagger UI tutorial API document artifact
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
随机推荐
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
What is the problem with the SQL group by statement
for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
JS traversal array and string
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
Nodejs tutorial let's create your first expressjs application with typescript
3D人脸重建:从基础知识到识别/重建方法!
Variable star --- article module (1)
js通过数组内容来获取数组下标
【OpenCV 例程200篇】220.对图像进行马赛克处理
966 minimum path sum
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
The biggest pain point of traffic management - the resource utilization rate cannot go up
Vim 基本配置和经常使用的命令
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
正则表达式收集
PHP saves session data to MySQL database
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件