Caocao's Bridges HDU - 4738 (割边)题解

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.


题目大意:炸桥,只有一颗炸弹,要炸掉一座桥使得至少有一个岛被孤立,每座桥都有士兵驻守,要求找出最少需要几个士兵完成任务。

这道题目的细节还是挺多的,首先n个点,边的数量最多可以达到n^2条,所以会有多重边,所以对于连向父节点的边,如果多于1条就可以拿来修改当前结点的low值;第二,有的桥的驻守士兵为0,但是炸桥最少需要一个人,所以ans=0时应该修改ans=1;第三,题目给出的图可能出现本身连通块就不唯一的情况,就不需要炸桥了。

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define ll long long
#define N 100005
#define inf 0x3f3f3f3f
int n, m;
vectorint, int> > e[1005];
int pre[1005];
int low[1005];
int ans;
int clock;
int dfs(int u, int fa){
    int lowu = pre[u] = ++clock;
    bool k = 1;
    for (int i = 0; i < e[u].size(); i++){
        int v = e[u][i].first;
        if (!pre[v]){
            int lowv = dfs(v, u);
            lowu = min(lowu, lowv);
            if (lowv > pre[u]){
                ans = min(ans, e[u][i].second);
            }
        }
        else if (k && v == fa){
            k = 0;
        }
        else{
            lowu = min(lowu, pre[v]);
        }
    }
    low[u] = lowu;
    return lowu;
}

int main(){
    int x, y, z;
    while (scanf("%d%d", &n, &m), m + n){
        ans = inf;
        clock = 0;
        memset(pre, 0, sizeof(pre));
        for (int i = 1; i <= n; i++){ e[i].clear(); }
        for (int i = 0; i < m; i++){
            scanf("%d%d%d", &x, &y, &z);
            e[x].push_back({ y, z });
            e[y].push_back({ x, z });
        }
        int num = 0;
        for (int i = 1; i <= n; i++){
            if (pre[i] == 0){
                dfs(i, -1);
                num++;
            }
        }
        if (ans == 0){ ans = 1; }
        else if (ans == inf){ ans = -1; }
        if (num > 1){ ans = 0; }
        printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(图论)