• HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】


     Caocao's Bridges
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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 <= N 2 ) 

    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
     
    题目大意:曹操占领了n个岛,同时修建了m条大桥,每个桥上有wi个士兵守卫。周瑜要炸掉一个大桥,让这n个岛不再成为连通的的,如果要炸掉某个大桥,则必须最少派遣人数应大于等于该大桥上的守卫个数。问你需要最少派去多少人。
     
    解题思路:很明显的是无向图求割边/桥,更准确得说要求的是割边的边权最小。模板题难度。但是这个题目比较坑:1.如果这n个岛本身就是不连通的  2.如果某条大桥上的士兵为0,但是仍需要派1人去炸毁
     
    #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
    ",Mins == 0? 1:Mins);
        }
        return 0;
    }
    

      

     
     
     
  • 相关阅读:
    JavaScript——BOM和DOM
    css-2
    Css-1
    storage size of 'xxx' isn't known问题出现的可能原因之一
    解决VS2010中winsock.h与winsock2.h冲突(重复定义)——转载
    SQLite : 解决“找不到请求的 .Net Framework 数据提供程序。可能没有安装”的问题
    使用 VirtualBox 虚拟机在电脑上运行 Android 4.0 系统,让电脑瞬间变安卓平板
    C#连接ACCESS的一个问题
    对硅谷和硅谷科技公司的十四问,全程干货
    nginx源码学习资源
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4943421.html
Copyright © 2020-2023  润新知