HDU 5521 Meeting

Meeting

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3    Accepted Submission(s): 1

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 $t_i$ minutes to travel from a block in $E_i$ to another block
in $E_i$ where $E_i (1leq ileq m)$ 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 (1leq Tleq 6)$, the number of test cases. Then T test cases
follow.

The first line of input contains n and m. $2leq n leq 10^5$. The following m lines describe the sets $E_i (1leq ileq m)$. Each line will contain two integers $t_i(1leq t_ileq 10^9)$and $Si (S_i>0)$ firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that $sum_{i=1}^{m}S_ileq 10^6$.

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

 解题:最短路,建图有些许技巧,就那么回事

 1 #include <stdio.h>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <queue>
 6 using namespace std;
 7 const int maxn = 2000010;
 8 using LL = long long;
 9 using PLI = pair<LL,int>;
10 const LL INF = 0x3f3f3f3f3f3f3f3f;
11 struct arc {
12     int to,next;
13     LL w;
14     arc(int x = 0,LL y = 0,int z = -1) {
15         to = x;
16         w = y;
17         next = z;
18     }
19 } e[8000010];
20 int head[maxn],n,m,tot;
21 void add(int u,int v,LL w) {
22     e[tot] = arc(v,w,head[u]);
23     head[u] = tot++;
24 }
25 priority_queue<PLI,vector<PLI>,greater<PLI>>q;
26 bool done[maxn];
27 LL d[2][maxn];
28 void dijkstra(LL d[maxn],int s) {
29     while(!q.empty()) q.pop();
30     q.push(PLI(0,s));
31     d[s] = 0;
32     memset(done,false,sizeof done);
33     while(!q.empty()) {
34         int u = q.top().second;
35         q.pop();
36         if(done[u]) continue;
37         done[u] = true;
38         for(int i = head[u]; ~i; i = e[i].next) {
39             if(d[e[i].to] > d[u] + e[i].w) {
40                 d[e[i].to] = d[u] + e[i].w;
41                 q.push(PLI(d[e[i].to],e[i].to));
42             }
43         }
44     }
45 }
46 vector<int>ans;
47 int main() {
48     int kase,cs = 1;
49     scanf("%d",&kase);
50     while(kase--) {
51         scanf("%d%d",&n,&m);
52         memset(head,-1,sizeof head);
53         tot = 0;
54         for(int i = 1,t,s,tmp; i <= m; ++i) {
55             scanf("%d%d",&t,&s);
56             while(s--) {
57                 scanf("%d",&tmp);
58                 add(n + i,tmp,t);
59                 add(tmp,n + i,0);
60             }
61         }
62         memset(d,0x3f,sizeof d);
63         dijkstra(d[0],1);
64         dijkstra(d[1],n);
65         LL ret = INF;
66         ans.clear();
67         for(int i = 1; i <= n; ++i) {
68             if(max(d[0][i],d[1][i]) < ret) {
69                 ans.clear();
70                 ans.push_back(i);
71                 ret = max(d[0][i],d[1][i]);
72             } else if(max(d[0][i],d[1][i]) == ret) ans.push_back(i);
73         }
74         if(ret < INF) {
75             printf("Case #%d: %I64d
",cs++,ret);
76             bool flag = false;
77             for(auto it:ans) {
78                 if(flag) putchar(' ');
79                 printf("%d",it);
80                 flag = true;
81             }
82             putchar('
');
83         } else printf("Case #%d: Evil John
",cs++);
84     }
85     return 0;
86 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4925710.html