博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2015沈阳区域赛Meeting(最短路 + 建图)
阅读量:4477 次
发布时间:2019-06-08

本文共 4815 字,大约阅读时间需要 16 分钟。

Meeting

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 6865    Accepted Submission(s): 2085

Problem Description
Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his
fences they were separated into different blocks. John's farm are divided into 
n blocks labelled from 1 to n.
Bessie lives in the first block while Elsie lives in the n-th one. They have a map of the farm
which shows that it takes they ti minutes to travel from a block in Ei to another block
in Ei where Ei (1im) is a set of blocks. They want to know how soon they can meet each other
and which block should be chosen to have the meeting.
 

 

Input
The first line contains an integer 
T (1T6), the number of test cases. Then T test cases
follow.
The first line of input contains n and m2n105. The following m lines describe the sets Ei (1im). Each line will contain two integers ti(1ti109)and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that mi=1Si106.
 

 

Output
For each test case, if they cannot have the meeting, then output "Evil John" (without quotes) in one line.
Otherwise, output two lines. The first line contains an integer, the time it takes for they to meet.
The second line contains the numbers of blocks where they meet. If there are multiple
optional blocks, output all of them in ascending order.
 

 

Sample Input
2 5 4 1 3 1 2 3 2 2 3 4 10 2 1 5 3 3 3 4 5 3 1 1 2 1 2
 

 

Sample Output
Case #1: 3 3 4 Case #2: Evil John
Hint
In the first case, it will take Bessie 1 minute travelling to the 3rd block, and it will take Elsie 3 minutes travelling to the 3rd block. It will take Bessie 3 minutes travelling to the 4th block, and it will take Elsie 3 minutes travelling to the 4th block. In the second case, it is impossible for them to meet.
 

 

Source
 

 

Recommend
wange2014   |   We have carefully selected several similar problems for you:            
 

 

 

1 /*************************************************************************  2     > File Name: meeting.cpp  3     > Author: CruelKing  4     > Mail: 2016586625@qq.com   5     > Created Time: 2019年09月21日 星期六 11时26分48秒  6     本题思路:用Dijkstra结果wa了,很无奈,换了spfa就过了,哦,不是就过了,是  7     各种TLE,WA,PE都不提示,给别人提示的是WA,时段不同也能T.搞心态的题......  8     就是个傻逼题:稍加分析就知道一个块内全部建边是不可行的,对每个块建立一个超级  9     源点,块内的点向超级源点连一条边,跑一波最短路,后续细节处理很简单.傻逼题. 10  ************************************************************************/ 11  12 #include 
13 #include
14 #include
15 #include
16 #include
17 using namespace std; 18 19 const int maxn = 2000000 + 5, maxm = 1000000 + 5; 20 21 typedef long long ll; 22 23 ll inf = LLONG_MAX; 24 25 struct Edge { 26 int to, cost, next; 27 } edge[maxm << 1]; 28 29 int head[maxn], tot; 30 31 int n, m; 32 33 ll diss[maxn], dist[maxn]; 34 35 ll dis[maxn]; 36 37 bool vis[maxn]; 38 39 void init() { 40 memset(head, -1,sizeof head); 41 tot = 0; 42 } 43 44 void addedge(int u, int v, int w) { 45 edge[tot].to = v; edge[tot].cost = w; edge[tot].next = head[u]; head[u] = tot ++; 46 edge[tot].to = u; edge[tot].cost = w; edge[tot].next = head[v]; head[v] = tot ++; 47 } 48 49 void dijkstra(int s, ll (&_dis)[maxn]) { 50 memset(vis, false, sizeof vis); 51 for(int i = 1; i <= m + n; i ++) _dis[i] = inf; 52 _dis[s] = 0; 53 vis[s] = true; 54 queue
que; 55 que.push(s); 56 while(!que.empty()) { 57 int u = que.front(); que.pop(); vis[u] = false; 58 for(int i = head[u]; ~i; i = edge[i].next) { 59 int v = edge[i].to; 60 if(_dis[v] > _dis[u] + edge[i].cost) { 61 _dis[v] = _dis[u] + edge[i].cost; 62 if(!vis[v]) { 63 vis[v] = true; 64 que.push(v); 65 } 66 } 67 } 68 } 69 } 70 71 int ans[maxn], cnt; 72 73 int main() { 74 int t, v, w, num, _case = 0; 75 scanf("%d", &t); 76 while(t --) { 77 cnt = 0; 78 init(); 79 // memset(dis, inf ,sizeof dis); 80 scanf("%d %d", &n, &m); 81 for(int i = 1; i <= n + m; i ++) dis[i] = inf; 82 for(int u = 1; u <= m; u ++) { 83 scanf("%d %d", &w, &num); 84 while(num --) { 85 scanf("%d", &v); 86 addedge(u, m + v, w); 87 } 88 } 89 dijkstra(1 + m, diss); 90 dijkstra(n + m, dist); 91 ll Min = inf; 92 for(int i = 1 + m; i <= n + m; i ++) { 93 if(diss[i] < inf && dist[i] < inf) { 94 dis[i] = max(diss[i], dist[i]); 95 if(dis[i] < Min) Min = dis[i]; 96 } 97 } 98 printf("Case #%d: ", ++ _case); 99 if(Min != inf) {100 printf("%lld\n", Min / 2);101 for(int i = 1 + m; i <= n + m; i ++) {102 if(dis[i] == Min) {103 ans[cnt ++] = i - m;104 }105 }106 for(int i = 0; i < cnt; i ++) {107 if(i) printf(" ");108 printf("%d", ans[i]);109 }110 printf("\n");111 } else printf("Evil John\n");112 }113 return 0;114 }

 

转载于:https://www.cnblogs.com/bianjunting/p/11562832.html

你可能感兴趣的文章
Mysql的DATE_FORMAT()日期格式转换
查看>>
SparkStreaming入门及例子
查看>>
Web应用增加struts2支持
查看>>
java程序——凯撒加密
查看>>
Windows Store App之数据存储
查看>>
English class 82 The Importance of traveling
查看>>
python用递归函数解汉诺塔游戏
查看>>
Redis与Python交互
查看>>
Maximum-SubsequenceSum
查看>>
常用的一些shell变量
查看>>
Android无法删除项目+导入项目报错
查看>>
poj 2349(最小生成树应用)
查看>>
python接口自动化测试二十五:执行所有用例,并生成HTML测试报告
查看>>
c# 指定的存储区提供程序在配置中找不到,或者无效
查看>>
最简陋的python数据
查看>>
第一堂java web课
查看>>
操作系统简介
查看>>
第1周小组博客作业--1703班06组
查看>>
vue项目中icon图标的完美引入
查看>>
C语言指针
查看>>