Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3361 Accepted Submission(s): 1073
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<set> #include<bitset> #include<queue> #include<stack> #include<map> #include<vector> using namespace std; #define eps 0.0000001 typedef long long ll; typedef pair<int,int> P; const int maxn=2e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7; const ll INF=1e18+7; struct edge { int from,to; ll w; }; vector<edge>G[maxn]; priority_queue<P,vector<P>,greater<P> >q; ll dist[2][maxn]; void addedge(int u,int v,ll w) { G[u].push_back((edge) { u,v,w }); G[v].push_back((edge) { v,u,w }); } void dij(int t,int s) { dist[t][s]=0LL; q.push(P(dist[t][s],s)); while(!q.empty()) { P p=q.top(); q.pop(); int u=p.second; for(int i=0; i<G[u].size(); i++) { edge e=G[u][i]; if(dist[t][e.to]>dist[t][u]+e.w) { dist[t][e.to]=dist[t][u]+e.w; q.push(P(dist[t][e.to],e.to)); } } } } void init(int n) { for(int i=0; i<=2*n+10; i++) G[i].clear(); } int main() { int T; scanf("%d",&T); for(int Case=1; Case<=T; Case++) { int n,m; scanf("%d%d",&n,&m); for(int i=1; i<=m; i++) { int val; scanf("%lld",&val); int t; scanf("%d",&t); while(t--) { int s; scanf("%d",&s); addedge(s,n+i,val); } } for(int i=0; i<=2*n+10; i++) dist[0][i]=dist[1][i]=INF; dij(0,1); dij(1,n); ll ans=INF; for(int i=1; i<=n; i++) { //printf("%lld %lld\n",dist[0][i],dist[1][i]); ans=min(ans,max(dist[0][i],dist[1][i])); } printf("Case #%d: ",Case); if(ans>=INF) puts("Evil John"); else { printf("%lld\n",ans/2); int cou=0; for(int i=1; i<=n; i++) { if(!cou&&max(dist[0][i],dist[1][i])==ans) printf("%d",i),cou++; else if(cou&&max(dist[0][i],dist[1][i])==ans) printf(" %d",i),cou++; } printf("\n"); } init(n); } return 0; }
原文:http://www.cnblogs.com/GeekZRF/p/7476156.html