hdu 4005 The war

The war

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1786    Accepted Submission(s): 377


Problem Description
In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situation of the enemy's war zones, and known that these war zones can communicate to each other directly or indirectly through the network. We also know the enemy is going to build a new communication line to strengthen their communication network. Our task is to destroy their communication network, so that some of their war zones can't communicate. Each line has its "cost of destroy". If we want to destroy a line, we must spend the "cost of destroy" of this line. We want to finish this task using the least cost, but our enemy is very clever. Now, we know the network they have already built, but we know nothing about the new line which our enemy is going to build. In this condition, your task is to find the minimum cost that no matter where our enemy builds the new line, you can destroy it using the fixed money. Please give the minimum cost. For efficiency, we can only destroy one communication line.
 

 
Input
The input contains several cases. For each cases, the first line contains two positive integers n, m (1<=n<=10000, 0<=m<=100000) standing for the number of the enemy's war zones (numbered from 1 to n), and the number of lines that our enemy has already build. Then m lines follow. For each line there are three positive integer a, b, c (1<=a, b<=n, 1<=c<=100000), meaning between war zone A and war zone B there is a communication line with the "cost of destroy " c.
 

 
Output
For each case, if the task can be finished output the minimum cost, or output ‐1.
 

 
Sample Input
3 2
1 2 1
2 3 2
4 3
1 2 1
1 3 2
1 4 3
 

 
Sample Output
-1
3
 
Hint
For the second sample input: our enemy may build line 2 to 3, 2 to 4,
 
3 to 4. If they build line 2 to 3, we will destroy line 1 to 4, cost 3. If they
 
build line 2 to 4, we will destroy line 1 to 3, cost 2. If they build line 3 to 4,
 
we will destroy line 1 to 2, cost 1. So, if we want to make sure that we can
 
destroy successfully, the minimum cost is 3.
 
双联通。。。
题意:给出一个无向图,求一个最小的边权,使得不管在这个图上添加一条什么样的边,
        都能拆掉一条小于等于选定边权的边,使得图不连通。
 
先tarjan缩点成一棵树,然后在树上操作
 
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define maxn 11111
#define maxm 222222
#define INF 1000000000
#define min(a, b) a < b ? a : b
using namespace std;
 
struct Edge{
    int v;
    int w;
    int next;
}e[maxm];
 
int pre[maxm];
int dfn[maxn];
int low[maxn];
int scc[maxn];
int sta[maxn];
int ed[maxm][3], ecnt;
bool vis[maxn];
int top, index, cnt, node, ans, n;
 
void init(){
    index = top = cnt = node = ecnt = 0;
    memset( pre, -1, sizeof(pre) );
    memset( dfn, 0, sizeof(dfn) );
    memset( vis, false, sizeof(vis) );
}
 
void Add( int u, int v, int w ){
    e[index].v = v;
    e[index].w = w;
    e[index].next = pre[u];
    pre[u] = index++;
 
    e[index].v = u;
    e[index].w = w;
    e[index].next = pre[v];
    pre[v] = index++;
}
 
void tarjan( int u, int fa ){
    dfn[u] = low[u] = ++node;
    sta[++top] = u;
    for ( int i = pre[u]; i != -1; i = e[i].next ){
        int v = e[i].v;
        if ( !dfn[v] ){
            tarjan(v, u);
            low[u] = min( low[u], low[v] );
            if ( dfn[u] < low[v] ){                        //求桥
                ed[++ecnt][0] = u;
                ed[ecnt][1] = v;
                ed[ecnt][2] = e[i].w;
            }
        }
        else if ( v != fa ){                            //防止按原路返回
            low[u] = min( low[u], dfn[v] );
        }   
    }
    if ( low[u] == dfn[u] ){
        cnt++;
        int v;
        do{
            v = sta[top--];
            scc[v] = cnt;
        }while( v != u );
    }
}
 
void find( int u ){
    low[u] = INF;
    for ( int i = pre[u]; i != -1; i = e[i].next ){
        int v = e[i].v;
        if ( !vis[v] ){
            vis[v] = 1;
            find( v );
            low[v] = min( low[v], e[i].w );
            if ( low[v] < low[u] ){       
                ans = min(ans, low[u]);
                low[u] = low[v];
            }
            else{
                ans = min( ans, low[v] );
            }
        }
    }
}
 
void solve(){
    for ( int i = 1; i <= n; i++ ){
        if ( !dfn[i] ){
            tarjan(i, i);
        }
    }
    memset( pre, -1, sizeof(pre) );
    memset( vis, false, sizeof(false));
    index = 0;
    int mid = 0;
    ed[mid][2] = INF;
    for ( i = 1; i <= ecnt; i++ ){
        Add(scc[ed[i][0]], scc[ed[i][1]], ed[i][2]);
        if ( ed[mid][2] > ed[i][2] ){
            mid = i;
        }
    }
    ans = INF;
    if ( mid ){
        int u = scc[ed[mid][0]];
        int v = scc[ed[mid][1]];
        vis[u] = vis[v] = 1;
        find( u );
        find( v );
    }
    if ( ans == INF ){
        ans = -1;
    }
    printf("%d\n", ans);
}
 
int main(){
    int m;
    while ( ~scanf("%d%d", &n, &m) ){
        init();
        for ( int i = 0; i < m; i++){
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            Add( u, v, w );
        }
        solve();
 
    }
    return 0;
}
/*
7 6
1 2 2
1 3 6
2 4 3
2 5 4
3 6 5
3 7 7
4
15 14
1 2 7
2 4 5
2 5 6
4 8 1
4 9 2
5 10 3
5 11 4
1 3 8
3 6 9
3 7 1
6 12 11
6 13 12
7 14 13
7 15 14
2
16 15
1 2 7
2 4 5
2 5 6
4 8 1
4 9 2
5 10 3
5 11 4
1 16 1
16 3 6
3 6 9
3 7 1
6 12 11
6 13 12
7 14 13
7 15 14
2
6 5
1 2 1
1 3 2
1 4 3
2 5 4
2 6 5
3
9 8
1 2 1
2 6 5
6 7 7
6 8 4
6 9 6
1 3 2
1 4 5
1 5 6
5
9 8
1 2 1
2 6 5
6 7 7
6 8 4
6 9 4
1 3 2
1 4 5
1 5 6
4
*/
 
 
posted @ 2013-10-26 20:30  /bin  阅读(216)  评论(0编辑  收藏  举报