[HDU4738]Caocao's Bridges(求割边最小权值)

http://acm.hdu.edu.cn/showproblem.php?pid=4738

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e3+4,maxm=1e6+4;
int dfn[maxn],low[maxn],head[maxn];
int n,m,cnt=0,rt,ans,tim;
struct node{
    int to,next,w;
}e[maxm<<1];
inline void add(int u,int v,int w){
    e[cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].w=w;cnt++;
}
inline void tarjan(int u,int pre){
    dfn[u]=low[u]=++tim;
    for(int i=head[u];i!=-1;i=e[i].next){
        if(i==(pre^1)) continue;
        int v=e[i].to;
        if(!dfn[v]) {
            tarjan(v,i);
            low[u]=min(low[u],low[v]);
            if(low[v]==dfn[v]){
                ans=min(ans,e[i].w);
            }
        }else low[u]=min(low[u],dfn[v]);
    }
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        if(n==0&&m==0) return 0;
        cnt=0;tim=0;ans=0x3f3f3f3f;
        memset(head,-1,sizeof(head));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(e,0,sizeof(e));
        for(int i=1;i<=m;i++){
            int u,v,w;scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);add(v,u,w);
        }
        int flag=0;
        for(int i=1;i<=n;i++){
            if(!dfn[i]) {
                if(flag) {ans=-1;break;}
                flag=1;
                tarjan(i,0x3f3f3f3f);
            }
        }
        if(ans==-1) printf("0\n");
        else if(ans==0) printf("1\n");
        else if(ans==0x3f3f3f3f) printf("-1\n");
        else printf("%d\n",ans);
    }
}

 

posted @ 2019-01-05 22:53  WiFiMonster  阅读(102)  评论(0编辑  收藏  举报