hdu4738(边双连通分量,桥)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu4738(边双连通分量,桥)相关的知识,希望对你有一定的参考价值。

Caocao‘s Bridges

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5595    Accepted Submission(s): 1757


Problem Description
Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn‘t give up. Caocao‘s army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and based on those islands, Caocao‘s army could easily attack Zhou Yu‘s troop. Caocao also built bridges connecting islands. If all islands were connected by bridges, Caocao‘s army could be deployed very conveniently among those islands. Zhou Yu couldn‘t stand with that, so he wanted to destroy some Caocao‘s bridges so one or more islands would be seperated from other islands. But Zhou Yu had only one bomb which was left by Zhuge Liang, so he could only destroy one bridge. Zhou Yu must send someone carrying the bomb to destroy the bridge. There might be guards on bridges. The soldier number of the bombing team couldn‘t be less than the guard number of a bridge, or the mission would fail. Please figure out as least how many soldiers Zhou Yu have to sent to complete the island seperating mission.
 

 

Input
There are no more than 12 test cases.

In each test case:

The first line contains two integers, N and M, meaning that there are N islands and M bridges. All the islands are numbered from 1 to N. ( 2 <= N <= 1000, 0 < M <= N2 )

Next M lines describes M bridges. Each line contains three integers U,V and W, meaning that there is a bridge connecting island U and island V, and there are W guards on that bridge. ( U ≠ V and 0 <= W <= 10,000 )

The input ends with N = 0 and M = 0.
 

 

Output
For each test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn‘t succeed any way, print -1 instead.
 

 

Sample Input
3 3 1 2 7 2 3 4 3 1 4 3 2 1 2 7 2 3 4 0 0
 

 

Sample Output
-1 4
 

 

Source
 
 
/*
hdu4738
1.原图可能不联通   这时不需要派人去炸桥 直接输出 0
2.有重边
3.可能有权值为0的桥  但我们必须要有一个人去带炸弹啊 所以这是输出 1
*/
#include<iostream>
#include<cstdio>
#include<cstring>

#define N 1001

using namespace std;
int n,m,cnt,ans,flag;
int head[N],dfn[N],low[N],fa[N];
struct edge{
    int u,v,w,net;
}e[N*N*2];

inline int read()
{
    int x=0,f=1;char c=getchar();
    while(c>9||c<0){if(c==-)f=-1;c=getchar();}
    while(c>=0&&c<=9){x=x*10+c-0;c=getchar();}
    return x*f;
}

inline void add(int u,int v,int w)
{
    e[++cnt].v=v;e[cnt].w=w;e[cnt].net=head[u];head[u]=cnt;
}

void init()
{
    memset(low,0,sizeof low);memset(dfn,0,sizeof dfn);
    memset(fa,0,sizeof fa);memset(head,0,sizeof head);
    memset(e,0,sizeof e);cnt=flag=0;
}

void Tarjan(int u,int father)
{
    dfn[u]=low[u]=++cnt;
    for(int i=head[u];i;i=e[i].net)
    {
        int v=e[i].v;
        if(i==father+1) continue;
        if(!dfn[v])
        {
            Tarjan(v,i);low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u]) ans=min(ans,e[i].w);
        }
        low[u]=min(low[u],dfn[v]);//注意 
    }
}

int main()
{
    freopen("ly.txt","r",stdin);
    int x,y,z;
    while(1)
    {
        init();
        n=read();m=read();
        if(!n && !m) break;
        for(int i=1;i<=m;i++)
        {
            x=read();y=read();z=read();
            add(x,y,z);add(y,x,z);
        }cnt=0;
        ans=0x3f3f3f3f;
        for(int i=1;i<=n;i++) if(!dfn[i]) flag++,Tarjan(i,-1);
        if(flag>1){printf("0\n");continue;}
        ans=ans==0x3f3f3f3f?-1:ans;ans=ans==0?1:ans;
        printf("%d\n",ans);
    }
    return 0;
}

 

 

以上是关于hdu4738(边双连通分量,桥)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 4738 Caocao's Bridges(双连通分量求桥)

双连通分量

[HDOJ4738]Caocao's Bridges(双联通分量,割边,tarjan)

HDOJ2242解题报告边双连通分量

hdu3394Railway双连通分量+模板详细解释

割点桥模板以及点双连通边双连通