hdu 4738 Caocao's Bridges 2013 ACM/ICPC Asia Regional Hangzhou Online tarjan 模板

Caocao's Bridges

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3871    Accepted Submission(s): 457


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
 
比赛时交WA了后,重新读题,特别注意了下面三个条件:
1.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.
2.Zhou Yu must send someone carrying the bomb to destroy the bridge.
3.If Zhou Yu couldn't succeed any way, print -1 instead.
先给个不能AC的理解: 如果给定图连通且有割边,那么根据条件2  ZY肯定派人(!0)炸桥。
但AC输出是0。。。这次跪在题意上。。。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<stack>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define MAX_LL 0x7fffffffffffffff
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MAXN 1111
#define MAXM 6666666
#define INF 1000000000

struct edge{
    int u, v, w, nxt;
    int re;
}e[MAXM];
int cc, h[MAXN], tsp;
int dfn[MAXN], low[MAXN];
bool vis[MAXN], ins[MAXN], ve[MAXM];
int n, m;

stack<int> s;
int ans;

void add(int u, int v, int w, int f){
    e[cc]=(edge){u, v, w, h[u], f+cc};
    h[u]=cc++;
}

void dfs(int u){
    dfn[u]=low[u]=++tsp;
    vis[u]=ins[u]=true;         s.push(u);
    for(int i=h[u]; i!=-1; i=e[i].nxt){
        int v=e[i].v;
        if(!vis[v]){
            ve[e[i].re]=true;
            dfs(v), low[u]=MIN(low[v], low[u]);
            if(dfn[v]==low[v]) ans=MIN(ans, e[i].w);    //割点,割边
        }
        else if(ins[v] && !ve[i]){
            ve[e[i].re]=true;
            low[u]=MIN(dfn[v], low[u]);
        }
    }
    if(dfn[u]==low[u]){             //割点
        int v;
        do{
            v=s.top();      s.pop();
            ins[v]=false;
        }while(v!=u);
    }
}

void tarjan(){
    int i, j;
    memset(vis, 0, sizeof(vis));
    memset(ve, 0, sizeof(ve));
    memset(ins, 0, sizeof(ins));
    int f=tsp=0;
    for(i=1; i<=n; i++) if(!vis[i])
        dfs(i), f++;
    if(f>1) ans=0;
    if(ans==INF)  ans=-1;
}

int main(){
    while(scanf(" %d %d", &n, &m)==2 && n){
        int i,j;
        int u, v, w;
        memset(h, -1, sizeof(h));       cc=0;
        for(i=0; i<m; i++){
            scanf(" %d %d %d", &u, &v, &w);
            if(!w) w=1;
            if(u!=v){
                add(u, v, w, 1);
                add(v, u, w, -1);
            }
        }
        ans=INF;
        tarjan();
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2013-09-16 01:11  Ramanujan  阅读(409)  评论(0编辑  收藏  举报