标签:
题目链接:
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2024 Accepted Submission(s): 628
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); #define lson o<<1 #define rson o<<1|1 typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar()); for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + ‘0‘); putchar(‘\n‘); } const LL mod=1e9+7; const double PI=acos(-1.0); const LL inf=1e18; const int N=1e6+10; const int maxn=2e6+5; const double eps=1e-12; int n,m,cnt,s,e,vis[maxn],c[maxn],head[maxn]; LL dis[2][maxn]; struct Edge { int to,next,val; }edge[maxn]; queue<int>qu; inline void add_edge(int from,int to,int va) { edge[cnt].to=to; edge[cnt].next=head[from]; edge[cnt].val=va; head[from]=cnt++; } void solve(int s,int e,int f) { while(!qu.empty())qu.pop(); mst(vis,0); for(int i=0;i<maxn;i++)dis[f][i]=inf; dis[f][s]=0; qu.push(s); vis[s]=1; while(!qu.empty()) { int fr=qu.front();qu.pop(); for(int i=head[fr];i!=-1;i=edge[i].next) { int x=edge[i].to; if(dis[f][x]>dis[f][fr]+edge[i].val) { dis[f][x]=dis[f][fr]+edge[i].val; if(!vis[x])qu.push(x),vis[x]=1; } } vis[fr]=0; } } int main() { int t,Case=0; read(t); while(t--) { printf("Case #%d: ",++Case); mst(head,-1); cnt=0; read(n);read(m); int x,h,t; for(int i=1;i<=m;i++) { read(t);read(h); for(int j=1;j<=h;j++) { read(x); add_edge(x,n+i,t); add_edge(n+i,x,t); } } solve(1,n,0); solve(n,1,1); LL ans=inf; for(int i=1;i<=n;i++)ans=min(ans,max(dis[0][i],dis[1][i])); if(ans==inf)printf("Evil John\n"); else { printf("%lld\n",ans/2); int num=0; for(int i=1;i<=n;i++)if(max(dis[0][i],dis[1][i])==ans)c[++num]=i; for(int i=1;i<num;i++)printf("%d ",c[i]); printf("%d\n",c[num]); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5928373.html