标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1876 Accepted Submission(s): 679
注意:
1.有重边;
2.至少要去一个士兵;
3.图不连通就不用去了;
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cstdlib> 6 #include<vector> 7 #include<map> 8 using namespace std; 9 const int maxn=1007; 10 const int inf=0x3f3f3f3; 11 struct node 12 { 13 int to,cost,next; 14 }; 15 node edge[maxn*maxn]; 16 int head[maxn],n,m,num,ans,sum,pre[maxn],child,low[maxn]; 17 void addedge(int u,int v,int c) 18 { 19 edge[num].to=v;edge[num].cost=c;edge[num].next=head[u]; 20 head[u]=num++; 21 edge[num].to=u;edge[num].cost=c;edge[num].next=head[v]; 22 head[v]=num++; 23 } 24 void tarjan(int u,int fa) 25 { 26 child++; 27 low[u]=pre[u]=sum++; 28 for(int i=head[u];i!=-1;i=edge[i].next) 29 { 30 int v=edge[i].to; 31 if(i==(fa^1)) continue; 32 if(!pre[v]){ 33 tarjan(v,i); 34 low[u]=min(low[u],low[v]); 35 if(pre[u]<low[v]){ 36 ans=min(ans,edge[i].cost); 37 } 38 } 39 else low[u]=min(low[u],low[v]); 40 } 41 } 42 int main() 43 { 44 int a,b,c; 45 while(~scanf("%d%d",&n,&m)){ 46 if(n==0&&m==0) break; 47 memset(head,-1,sizeof(head)); 48 memset(pre,0,sizeof(pre)); 49 memset(low,0,sizeof(low)); 50 ans=inf;child=0;sum=1; 51 for(int i=1;i<=m;i++) 52 { 53 scanf("%d%d%d",&a,&b,&c); 54 addedge(a,b,c); 55 } 56 tarjan(1,-1); 57 if(child<n) printf("0\n"); 58 else if(ans==inf) printf("-1\n"); 59 else if(ans==0) printf("1\n"); 60 else printf("%d\n",ans); 61 } 62 return 0; 63 }
标签:
原文地址:http://www.cnblogs.com/codeyuan/p/4385504.html