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 vector<int>belong[100010]; 22 void add(int u,int v,LL w) { 23 e[tot] = arc(v,w,head[u]); 24 head[u] = tot++; 25 } 26 priority_queue<PLI,vector<PLI>,greater<PLI>>q; 27 bool done[maxn]; 28 LL d[2][maxn]; 29 void dijkstra(LL d[maxn],int s) { 30 while(!q.empty()) q.pop(); 31 q.push(PLI(0,s)); 32 d[s] = 0; 33 for(int i = belong[s].size()-1; i >= 0; --i) { 34 d[belong[s][i] + n] = 0; 35 q.push(PLI(0,belong[s][i] + n)); 36 } 37 memset(done,false,sizeof done); 38 while(!q.empty()) { 39 int u = q.top().second; 40 q.pop(); 41 if(done[u]) continue; 42 done[u] = true; 43 for(int i = head[u]; ~i; i = e[i].next) { 44 if(d[e[i].to] > d[u] + e[i].w) { 45 d[e[i].to] = d[u] + e[i].w; 46 q.push(PLI(d[e[i].to],e[i].to)); 47 } 48 } 49 } 50 } 51 vector<int>ans; 52 int main() { 53 int kase,cs = 1; 54 scanf("%d",&kase); 55 while(kase--) { 56 scanf("%d%d",&n,&m); 57 memset(head,-1,sizeof head); 58 tot = 0; 59 for(int i = 1; i <= n; ++i) belong[i].clear(); 60 for(int i = 1,t,s,tmp; i <= m; ++i) { 61 scanf("%d%d",&t,&s); 62 while(s--) { 63 scanf("%d",&tmp); 64 add(n + i,tmp,t); 65 belong[tmp].push_back(i); 66 } 67 } 68 for(int i = 1; i <= n; ++i) 69 for(auto it:belong[i]) add(i,it + n,0); 70 memset(d,0x3f,sizeof d); 71 dijkstra(d[0],1); 72 dijkstra(d[1],n); 73 LL ret = INF; 74 ans.clear(); 75 for(int i = 1; i <= n; ++i) { 76 if(max(d[0][i],d[1][i]) < ret) { 77 ans.clear(); 78 ans.push_back(i); 79 ret = max(d[0][i],d[1][i]); 80 } else if(max(d[0][i],d[1][i]) == ret) ans.push_back(i); 81 } 82 if(ret < INF) { 83 printf("Case #%d: %I64d\n",cs++,ret); 84 bool flag = false; 85 for(auto it:ans) { 86 if(flag) putchar(‘ ‘); 87 printf("%d",it); 88 flag = true; 89 } 90 putchar(‘\n‘); 91 } else printf("Case #%d: Evil John\n",cs++); 92 } 93 return 0; 94 }
时间: 2024-10-22 00:14:25