标签:limits ase arch guarantee long wan cst set queue
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3704 Accepted Submission(s): 1187
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <climits> 7 #include <cmath> 8 #include <vector> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 using namespace std; 14 typedef long long LL ; 15 typedef unsigned long long ULL ; 16 const int maxn = 1e6 + 10 ; 17 const LL inf = 0x3f3f3f3f3f; 18 const int npos = -1 ; 19 const int mod = 1e9 + 7 ; 20 const int mxx = 100 + 5 ; 21 const double eps = 1e-6 ; 22 const double PI = acos(-1.0) ; 23 24 struct node{ 25 LL len; 26 int i; 27 bool operator < (const node &r)const{ 28 if(len!=r.len) 29 return len<r.len; 30 else 31 return i<r.i; 32 } 33 }; 34 struct enode{ 35 int v; 36 LL w; 37 int next; 38 }; 39 struct qnode{ 40 int u; 41 LL dis; 42 bool operator < (const qnode &r)const{ 43 return dis>r.dis; 44 } 45 }; 46 enode edge[maxn<<1]; 47 int head[maxn<<1], cnt; 48 void addedge(int x, int y, LL z){ 49 edge[cnt]=(enode){y,z,head[x]}; 50 head[x]=cnt++; 51 } 52 std::vector< node > buf; 53 int T, n, m, u, v, s, no1, non; 54 LL w, dis1[maxn], disn[maxn], ans; 55 bool vis[maxn]; 56 void DIJ(LL *dis, int source){ 57 LL now; 58 priority_queue< qnode > Q; 59 qnode temp; 60 for(int i=0;i<=n+m;i++){ 61 vis[i]=false; 62 dis[i]=inf; 63 } 64 dis[source]=0LL; 65 Q.push((qnode){source,dis[source]}); 66 while(!Q.empty()){ 67 temp=Q.top(); 68 Q.pop(); 69 if(!vis[temp.u]){ 70 u=temp.u; 71 now=temp.dis; 72 vis[u]=true; 73 for(int i=head[u];i!=-1;i=edge[i].next){ 74 v=edge[i].v; 75 w=edge[i].w; 76 if(now+w<=dis[v]){ 77 dis[v]=now+w; 78 Q.push((qnode){v,dis[v]}); 79 } 80 } 81 } 82 } 83 } 84 int main(){ 85 // freopen("in.txt","r",stdin); 86 // freopen("out.txt","w",stdout); 87 while(~scanf("%d",&T)){ 88 for(int kase=1;kase<=T;kase++){ 89 no1=0; 90 non=0; 91 scanf("%d %d",&n,&m); 92 cnt=0; 93 memset(head,-1,sizeof(head)); 94 for(int i=1;i<=m;i++){ 95 scanf("%lld %d",&w,&s); 96 for(int j=1;j<=s;j++){ 97 scanf("%d",&u); 98 if(u==1)no1=1; 99 if(u==n)non=1; 100 v=n+i; 101 addedge(u,v,0LL); 102 addedge(v,u,w); 103 } 104 } 105 printf("Case #%d: ",kase); 106 if(!(no1&&non)){ 107 printf("Evil John\n"); 108 continue; 109 } 110 DIJ(dis1,1); 111 DIJ(disn,n); 112 buf.clear(); 113 for(int i=1;i<=n;i++) 114 buf.push_back((node){max(dis1[i],disn[i]),i}); 115 sort(buf.begin(),buf.end()); 116 ans=buf[0].len; 117 if(ans==inf){ 118 printf("Evil John\n"); 119 }else{ 120 printf("%lld\n",ans); 121 printf("%d",buf[0].i); 122 int i=1; 123 while(i<buf.size() && buf[i].len==ans) 124 printf(" %d",buf[i++].i); 125 cout<<endl; 126 } 127 } 128 } 129 return 0; 130 }
标签:limits ase arch guarantee long wan cst set queue
原文地址:http://www.cnblogs.com/edward108/p/7642717.html