MENU

HDU4738 Caocao's Bridges (Tarjan求有重边的割边/桥)

2018 年 07 月 17 日 • 阅读: 1901 • 图论阅读设置

Caocao's Bridges

  • Time Limit: 2000/1000 MS (Java/Others)
  • Memory Limit: 32768/32768 K (Java/Others)
  • Total Submission(s): 7395
  • Accepted Submission(s): 2270

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


链接

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

题意

给定n个点m条边的无向图,现要找出该图中权值最小的桥。

当删除图中的某条边e后,可将图分割成两个或两个以上的连通分量,则称边e为割边或者桥。

题解

求桥使用tajan算法,然后判断low[v] > dfn[u]即可。但是本题有三个需要注意的地方:

  • 如果原图本来就是不连通的,那么输出0
  • 如果求出所有桥中的最小权值为0,那么输出1,因为至少需要派一个人去炸桥
  • 有重边,这个需要进行特殊处理

如何处理无向图求桥出现重边呢?可以参考这篇文章:求有重边的无向图的割边算法。自己画个图琢磨一下就出来了。

如果有重边(u,v), 那么边(u,v)肯定被存了两次,所以我们只要让它第二次访问时通过就可以了。

值得注意的是,边(u, fa)不是反向边,而是树边(fa, u)的第二次访问。—小白书P314。


代码

StatusAccepted
Time93ms
Memory6012kB
Length1716
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxn = 1010;
const int inf = 0x3f3f3f3f;

int n, m, tot, cnt, ans, cc_cnt;
int dfn[maxn], low[maxn], head[maxn];

struct Edge
{
    int next, to, w;
}edge[2*maxn*maxn];

void init()
{
    for (int i = 0; i <= n; ++i)
    {
        low[i] = dfn[i] = 0;
        head[i] = -1;
    }
    tot = n;
    cc_cnt = cnt = 1;
    ans = inf;
}

void addedge(int u, int v, int w)
{
    edge[cnt].to = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

void tarjan(int u, int fa)
{
    low[u] = dfn[u] = ++tot;
    bool flag = true;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        int w = edge[i].w;
        if (fa == v && flag) // 重边判断,只需过滤一次
        {
            flag = false;
            continue;
        }
        if (!dfn[v])
        {
            cc_cnt++;
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
            if (low[v] > dfn[u]) // 找到最小权值
                ans = min(ans, w);
        }
        else
            low[u] = min(low[u], dfn[v]);
    }
}

int main()
{
    int u, v, w;
    while (scanf("%d%d", &n, &m) == 2 && n && m)
    {
        init();
        for (int i = 0; i < m; ++i)
        {
            scanf("%d%d%d", &u, &v, &w);
            addedge(u, v, w);
            addedge(v, u, w);
        }
        tarjan(1, 0);
        if (cc_cnt < n) // 原图不连通
            printf("0\n");
        else
        {
            if (ans == 0) // “桥”上士兵为0,至少派一个人去炸桥
                printf("1\n");
            else
                printf("%d\n", (ans == inf) ? -1 : ans); // ans = inf 说明计划失败
        }

    }
    return 0;
}

主要还是无向边是通过两条有向边来表示的,所以在要区分一下是不是反向边(回边)。

测试数据

来源于HDU 4738 双连通模版题

  • data.in
3 3
1 2 7 
2 3 4 
3 1 4 
 
3 2 
1 2 7 
2 3 4 
 
3 4 
1 2 7 
2 1 7 
2 3 4 
3 2 4 
 
4 3 
1 2 1  
1 2 3 
3 4 4 
 
4 4 
1 2 1  
1 2 3 
3 4 4 
2 4 6 
 
4 5 
1 2 1  
1 2 3 
3 4 4 
2 4 6 
1 3 0 
 
4 5 
4 3 0 
3 4 4 
2 4 6 
2 3 0 
1 3 0 
 
2 1 
1 2 0 
 
4 2 
1 2 3 
1 3 5 
 
4 5 
1 2 3 
2 3 4 
1 3 5 
4 3 7 
4 3 6 
 
4 4 
1 2 3 
2 3 4 
1 3 5 
4 3 6 
 
4 4 
1 2 4 
1 3 5 
4 3 6 
3 4 7 
 
6 7 
1 2 1 
1 3 2 
2 3 3 
3 4 4 
4 6 5 
4 5 6 
5 6 7 
 
5 6 
1 2 3 
1 3 4 
2 3 5 
3 4 7 
3 5 8 
5 4 9 
 
8 9 
1 8 1 
5 1 2 
1 7 3 
1 4 4 
1 2 5 
4 3 6 
3 2 7 
5 6 8 
6 7 9 
 
6 6 
1 4 1 
1 2 4 
2 5 4 
5 6 4 
3 6 4 
4 3 2 
 
6 5 
1 2 4 
2 5 4 
5 6 4 
3 6 4 
4 3 2 
 
8 10 
1 8 1 
5 1 2 
1 7 3 
1 4 4 
1 2 5 
4 3 6 
3 2 7 
5 6 8 
6 7 9 
8 1 2 
 
3 3 
1 2 7 
1 3 1 
1 3 4 
 
0 0
  • data.out
-1
4
-1
0
4
-1
1
1
0
-1
6
4
4
-1
1
-1
2
-1
7

顺便附上邝斌聚聚的板子

/* ***********************************************
Author        :kuangbin
Created Time  :2013/9/15 星期日 12:11:49
File Name     :2013杭州网络赛\1001.cpp
************************************************ */

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int INF = 0x3f3f3f3f;
/*
*  求 无向图的割点和桥
*  可以找出割点和桥,求删掉每个点后增加的连通块。
*  需要注意重边的处理,可以先用矩阵存,再转邻接表,或者进行判重
*/
const int MAXN = 10010;
const int MAXM = 2000010;
struct Edge
{
    int to,next;
    int w;
    bool cut;//是否为桥的标记
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN];
int Index,top;
bool Instack[MAXN];
bool cut[MAXN];
int add_block[MAXN];//删除一个点后增加的连通块
int bridge;

void addedge(int u,int v,int w)
{
    edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut = false;
    edge[tot].w = w;
    head[u] = tot++;
}


void Tarjan(int u,int pre)
{
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    int son = 0;
    int pre_num = 0;
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        v = edge[i].to;
        if(v == pre && pre_num == 0)
        {
            pre_num++;
            continue;

        }
        if( !DFN[v] )
        {
            son++;
            Tarjan(v,u);
            if(Low[u] > Low[v])Low[u] = Low[v];
            //桥
            //一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<Low(v)。
            if(Low[v] > DFN[u])
            {
                bridge++;
                edge[i].cut = true;
                edge[i^1].cut = true;
            }
            //割点
            //一个顶点u是割点,当且仅当满足(1)或(2) (1) u为树根,且u有多于一个子树。
            //(2) u不为树根,且满足存在(u,v)为树枝边(或称父子边,
            //即u为v在搜索树中的父亲),使得DFS(u)<=Low(v)
            if(u != pre && Low[v] >= DFN[u])//不是树根
            {
                cut[u] = true;
                add_block[u]++;
            }
        }
        else if( Low[u] > DFN[v])
             Low[u] = DFN[v];
    }
    //树根,分支数大于1
    if(u == pre && son > 1)cut[u] = true;
    if(u == pre)add_block[u] = son - 1;
    Instack[u] = false;
    top--;
}
int  solve(int N)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,false,sizeof(Instack));
    memset(add_block,0,sizeof(add_block));
    memset(cut,false,sizeof(cut));
    Index = top = 0;
    bridge = 0;
    for(int i = 1;i <= N;i++)
        if( !DFN[i] )
            Tarjan(i,i);
    int ret = INF;
    for(int u = 1; u <= N;u++)
        for(int i = head[u]; i != -1;i = edge[i].next)
            if(edge[i].cut)
                ret = min(ret,edge[i].w);
    if(ret == INF)ret = -1;
    if(ret == 0)ret++;
    return ret;
}
int F[MAXN];
int find(int x)
{
    if(F[x] == -1)return x;
    else return F[x] = find(F[x]);
}
void init()
{
    memset(F,-1,sizeof(F));
    tot = 0;
    memset(head,-1,sizeof(head));
}
void bing(int u,int v)
{
    int t1 = find(u);
    int t2 = find(v);
    if(t1 != t2)F[t1] = t2;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m;
    while(scanf("%d%d",&n,&m) == 2)
    {
        if(n == 0 && m == 0)break;
        int u,v,w;
        init();
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(u == v)continue;
            addedge(u,v,w);
            addedge(v,u,w);
            bing(u,v);
        }
        bool flag = true;
        for(int i = 1; i <= n;i++)
            if(find(i) != find(1))
                flag = false;
        if(!flag)
        {
            printf("0\n");
            continue;
        }
        printf("%d\n",solve(n));
    }
    return 0;
}

The end.
2018-07-17 星期二