当前位置:网站首页>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
边栏推荐
- 【滑动窗口】第九届蓝桥杯省赛B组:日志统计
- OAI 5g nr+usrp b210 installation and construction
- Huawei device command
- Redis insert data garbled solution
- 3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
- Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
- 【mysql】触发器
- Manifest of SAP ui5 framework json
- Taylor series fast Fourier transform (FFT)
- Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
猜你喜欢
Reinforcement learning - learning notes 5 | alphago
3D人脸重建:从基础知识到识别/重建方法!
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
OneNote 深度评测:使用资源、插件、模版
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
Opencv learning example code 3.2.3 image binarization
Study notes of grain Mall - phase I: Project Introduction
随机推荐
Deployment of external server area and dual machine hot standby of firewall Foundation
Variable star --- article module (1)
Common English vocabulary that every programmer must master (recommended Collection)
Aike AI frontier promotion (7.6)
PHP saves session data to MySQL database
代理和反向代理
Word bag model and TF-IDF
Ravendb starts -- document metadata
966 minimum path sum
3D人脸重建:从基础知识到识别/重建方法!
PG basics -- Logical Structure Management (transaction)
Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
Study notes of grain Mall - phase I: Project Introduction
[redis design and implementation] part I: summary of redis data structure and objects
JS学习笔记-OO创建怀疑的对象
OAI 5g nr+usrp b210 installation and construction
PG基础篇--逻辑结构管理(事务)
[asp.net core] set the format of Web API response data -- formatfilter feature
【Redis设计与实现】第一部分 :Redis数据结构和对象 总结