• hdu 4738 Caocao's Bridges tarjan


     Caocao's Bridges

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

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

    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

    HINT

    题意

    每一条边有一个边权,要去炸桥,问你最少派多少人去,才能使得这个图不连通

    题解:

    裸的tarjan找桥

    1.有重边

    2.一开始这个图不连通

    3.桥边权为0,需要派一个人去送炸弹

    代码

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)
    const int maxn=1100;
    const int maxm=2100000;
    #define mod 1000000009
    #define eps 1e-9
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    struct EDGE{
        int v,next,w;
    }E[maxm];
    int head[maxn],tot;
    int low[maxn],dfn[maxn];
    int kiss;
    int ans;
    int n,m,u,v,w;
    void Init()
    {
        memset(head,-1,sizeof head);
        tot=0;
        memset(low,0,sizeof low);
        memset(dfn,0,sizeof dfn);
        kiss=0;
        ans=inf;
    }
    
    void add_edge(int u,int v,int w){
        E[tot].w=w;
        E[tot].v=v;
        E[tot].next=head[u];
        head[u]=tot++;
    }
    
    void Tarjan(int u,int pre)
    {
        dfn[u]=low[u]=++kiss;
        int v;
        for(int i=head[u];i!=-1;i=E[i].next)
        {
            if(i==(pre^1))continue;
            v=E[i].v;
            if(!dfn[v])
                {
                Tarjan(v,i);
                if(low[v]<low[u])
                    low[u]=low[v];
                if(low[v]>dfn[u])
                    ans=min(ans,E[i].w);
            }else if(dfn[v]<low[u])
                low[u]=dfn[v];
        }
    }
    
    int main(){
    
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            if(n==0&&m==0)break;
            Init();
            for(int i=1;i<=m;i++)
            {
                scanf("%d %d %d",&u,&v,&w);
                add_edge(u,v,w);
                add_edge(v,u,w);
            }
            int cnt=0;
            for(int i=1;i<=n;i++)
                if(!dfn[i]){
                    cnt++;
                    Tarjan(i,-1);
                }
            if(cnt>1)
                ans=0;
            else if(ans==inf)
                ans=-1;
            else if(ans==0)
                ans=1;
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Mysql存储过程和函数
    python反编译chm文件并生成pdf文件
    python转换html到pdf文件
    python获取系统开机时间
    OpenSL ES: 利用OpenSL ES实现录音功能
    android: 根据文件uri 获取文件名
    Java: InputStream转化为byte数组
    Linux: 查看二进制文件
    Vim: 回到上次编辑的位置
    LayoutInflate: Avoid passing null as the view root
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4659907.html
Copyright © 2020-2023  润新知