标签:sizeof ges ring mes pre mat efi == cstring
#include <iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdio>
#define ll long long
using namespace std;
const int N = 1e5+10;
const int M = 1e6+10;
int n,m;
int ans;
struct Edge{
int fr,to,nxt,w;
}edge[M];
int tot,head[N];
void init(){
tot = 0;
ans = 1<<30;
memset(head,-1,sizeof head);
memset(dfn,0,sizeof dfn);
memset(low,0,sizeof low);
}
void add(int u,int v,int w){
edge[tot].fr = u;
edge[tot].to = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
int dfn[N],low[N];
int ind;
void tarjan(int u,int fa){
int v;
dfn[u] = low[u] = ++ind;
int k = 0;
for(int i =head[u];i!=-1;i=edge[i].nxt){
v = edge[i].to;
if(v== fa && !k){ // 处理重边
k++;
continue;
}
if(!dfn[v]){
tarjan(v,u);
low[u] = min(low[u],low[v]);
if(low[v]>dfn[u]){ // 割桥
ans = min(ans,edge[i].w);
}
}
else {
low[u] = min(low[u],dfn[v]);
}
}
}
void solve(){
init();
ind = 0 ;
int u,v,w;
for(int i=1;i<=m;++i){
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
tarjan(1,-1);
for(int i=1;i<=n;++i){
if(!dfn[i]){
printf("0\n");
return ;
}
}
if(ans == (1<<30)) ans = -1;
if(ans == 0 ) ans++;
printf("%d\n",ans);
}
int main()
{
while(scanf("%d%d",&n,&m)){
if(n==0 && m==0) break;
solve();
}
return 0;
}
多条割边求最小值,割边不存在则输出-1,割边权为0要输出1,不连通输出0
hdu 4738 Caocao's Bridges (tarjan求桥)
标签:sizeof ges ring mes pre mat efi == cstring
原文地址:https://www.cnblogs.com/xxrlz/p/11367538.html