您的位置:首页 > 其它

HDU 4738 Caocao's Bridges(找无向图的桥 双联通)

2015-09-05 22:22 471 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738

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

2013 ACM/ICPC Asia Regional Hangzhou Online

题意:

  赤壁之战曹操失败了,然后他想恢复势力。曹操在长江上建立了一些岛,岛之间通过桥连着。如果这些岛构成的无向图变成了连通图,那么曹操就可能恢复势力。周瑜为了防止曹操恢复,就打算去摧毁连接曹操的岛的桥。但诸葛亮只留下一枚***给周瑜。所以周瑜只能炸一条桥。

  给出 n,m。表示有n个点,m条桥。

  m行每行给出a,b,c,表示a岛和b岛之间有一条桥,并且曹操派了 c 个士兵去守卫这条桥。

  现在问周瑜最少派多少士兵去炸桥。

  如果无法使曹操的岛成为多个连通图,则输出 -1。
PS:

①. 有重边。

②. 如果无向图本身已经有两个连通图了,就无需派士兵去炸桥,这时候输出 0


③. 如果求出来的最小权值桥的守卫人数为 0 时,也需要派出一个士兵去炸桥。
代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define maxn 1100
#define INF 0x3f3f3f3f

int dfn[maxn], low[maxn], head[maxn], f[maxn];
int n, m;
int minn, tol, cnt;
struct Edge
{
    int u, v, w;
    int next;
} edge[maxn*maxn];
void init()
{
    cnt = tol = 0;
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(f, 0, sizeof(f));
}

void addEdge(int u, int v, int w)
{
    edge[tol].u = u;
    edge[tol].v = v;
    edge[tol].w = w;
    edge[tol].next = head[u];
    head[u] = tol++;
}

void Tarjan(int u, int fa)
{
    int i, v, flag = 0;
    low[u] = dfn[u] = ++cnt;
    f[u] = fa;
    for(i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].v;
        if(v == fa && !flag)//去重
        {
            flag = 1;
            continue;
        }
        if(!dfn[v])
        {
            Tarjan(v, u);
            low[u] = min(low[u], low[v]);
            if(dfn[u] < low[v])
                minn = min(minn, edge[i].w);
        }
        else
            low[u] = min(low[u], dfn[v]);
    }
}

int main()
{
    int u, v, w;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0 && m==0)
            break;
        init();
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addEdge(u, v, w);
            addEdge(v, u, w);
        }
        minn = INF;
        int k = 0;
        for(int i = 1 ; i <= n; i++)
        {
            if(!dfn[i])
            {
                Tarjan(i, -1);
                k++;
            }
        }
        if(k > 1)//不连通
        {
            printf("0\n");
            continue;
        }
        if(minn == 0)//最少0守卫
            printf("1\n");
        else if(minn == INF)//无解
            printf("-1\n");
        else
            printf("%d\n", minn);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: