Caocao's Bridges (最小的割边)

Caocao's Bridges (最小的割边)

题目

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

题意

周瑜只有一个炸弹,他要去炸曹操的桥。要求该桥是割边,而且桥上有多少人,周瑜就要至少带一样的人过去。求最少带多少人。

思路

割边算法求出最小割边。

  1. 如果没有割边输出 -1
  2. 如果本来就不是联通的输出 0
  3. 如果桥上没人输出 1(总得有人去)。
  4. 正常的话输出人数就好。

题解

#include <iostream>
#include <cmath>
#include <stack>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cstdio>
#define N 1004
using namespace std;
vector<int> eg[N];
int e[N][N], idx;
int dfn[N], low[N], parent[N], res;

void tarjan(int u)
{
    int child = 0;
    low[u] = dfn[u] = ++idx;
    for (int v : eg[u])
    {
        if (!dfn[v])
        {
            parent[v] = u;
            tarjan(v);
            low[u] = min(low[u], low[v]);
            if (low[v] > dfn[u])
            {
                res = min(res, e[u][v]);
            }
        }
        else if (v != parent[u])
            low[u] = min(low[u], dfn[v]);
    }
}
void init()
{
    res = 0x3f3f3f;
    idx = 0;
    memset(dfn, 0, sizeof dfn);
    memset(low, 0, sizeof low);
    memset(parent, -1, sizeof parent);
    memset(e, 0, sizeof e);
}
int main()
{
    int n, m, x, y, w;
    while (scanf("%d%d", &n, &m), n != 0)
    {
        init();
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &x, &y, &w);
            eg[x].push_back(y);
            eg[y].push_back(x);
            if (e[x][y] != 0)
                e[x][y] = e[y][x] = 0x3f3f3f;
            else
                e[x][y] = e[y][x] = w;
        }
        tarjan(1);
        int i;
        for (i = 2; i <= n; i++)
            if (!dfn[i])
            {
                cout << 0 << endl;
                break;
            }
        if (i == n + 1)
        {
            if (res == 0x3f3f3f)
                cout << -1 << endl;

            else if (res == 0)
                cout << 1 << endl;
            else
                cout << res << endl;
        }

        for (int ii = 1; ii <= n; ii++)
            eg[ii].clear();
    }
}

转载于:https://www.cnblogs.com/tttfu/p/11274233.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值