标签:
Description
Input
Output
Sample Input
Sample Output
#include<stdio.h> #include<algorithm> #include<string.h> #include<queue> #include<vector> using namespace std; const int maxn = 1010; const int INF = 0x3f3f3f3f; struct Edge{ int from,to,dist,next; Edge(){} Edge(int _to,int _next,int _dist):to(_to),next(_next),dist(_dist){} }edges[maxn*maxn*2]; int tot , head[maxn]; int dfn[maxn], bridge[maxn], dfs_clock, low[maxn], brinum; int Mins; void init(){ tot = 0; dfs_clock = 0; brinum = 0; Mins = INF; memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); memset(bridge,0,sizeof(bridge)); } void AddEdge(int _u,int _v,int _w){ edges[tot] = Edge(_v,head[_u],_w); head[_u] = tot++; } int dfs(int u,int fa){ int lowu = dfn[u] = ++dfs_clock; for(int i = head[u]; i != -1; i = edges[i].next){ int v = edges[i].to; if(!dfn[v]){ int lowv = dfs(v,i); lowu = min(lowu,lowv); if(lowv > dfn[u]){ bridge[v] = 1; brinum++; Mins = min(Mins , edges[i].dist); } }else if(dfn[v] < dfn[u] && (fa^1) != i){ lowu = min(dfn[v],lowu); } } low[u] = lowu; return lowu; } int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){ int a,b,c; init(); for(int i = 0; i <m ;i++){ scanf("%d%d%d",&a,&b,&c); AddEdge(a,b,c); AddEdge(b,a,c); } int times = 0; for(int i = 1; i <= n; i++){ if(!dfn[i]){ times++; dfs(i,-1); } } if(times > 1){ puts("0"); continue; } if(brinum == 0){ puts("-1"); continue; } printf("%d\n",Mins == 0? 1:Mins); } return 0; }
HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4943421.html