2015沈阳区域赛Meeting(最短路 + 建图)

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:  6730 6729 6728 6727 6726 
 
  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 <cstdio>
 13 #include <cstring>
 14 #include <queue>
 15 #include <algorithm>
 16 #include <climits>
 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 <ll> 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
", 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("
");
111         } else printf("Evil John
");
112     }
113     return 0;
114 }
原文地址:https://www.cnblogs.com/bianjunting/p/11562832.html