【HDU】4005 The war 边双连通

The war

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


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.
 

Source
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

传送门:【HDU】4005 The war

题目大意:一副无向图,给你一个权值,问能否使得添加一条边后删除一条边权不超过给定权值的边使得图不连通。有解输出最小权值,无解输出-1。

题目分析:首先双连通缩点得到一棵树(森林?),找到度为一的叶子结点,dfs出从该节点到分叉口之间的边权的最小值,取第三小的值就是结果。因为敌人如果将第一小的和第二小的边通过添加边使得在同一个环中,则可以选择第三小的边,否则,我们可以选择更小的边。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REPV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define clear( a , x ) memset ( a , x , sizeof a )

const int MAXN = 10005 ;
const int MAXE = 100005 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
	int v , n , w ;
	Edge ( int var = 0 , int next = 0 , int cost = 0 ) :
		v ( var ) , n ( next ) , w ( cost ) {}
} ;

struct Line {
	int u , v , w ;
	void input () {
		scanf ( "%d%d%d" , &u , &v , &w ) ;
	}
} ;

struct BCC {
	Edge edge[MAXE << 1] ;
	Line line[MAXE] ;
	int adj[MAXN] , cntE ;
	int bcc[MAXN] , bcc_cnt ;
	int low[MAXN] , dfn[MAXN] , dfs_clock ;
	int ins[MAXN] , S[MAXN] , top ;
	int deg[MAXN] ;
	int n , m ;
	
	void init () {
		top = cntE = dfs_clock = bcc_cnt = 0 ;
		clear ( dfn , 0 ) ;
		clear ( ins , 0 ) ;
		clear ( deg , 0 ) ;
		clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v , int w = 0 ) {
		edge[cntE] = Edge ( v , adj[u] , w ) ;
		adj[u] = cntE ++ ;
		edge[cntE] = Edge ( u , adj[v] , w ) ;
		adj[v] = cntE ++ ;
	}
	
	void tarjan ( int u , int fa ) {
		low[u] = dfn[u] = ++ dfs_clock ;
		ins[u] = 1 ;
		S[top ++] = u ;
		int flag = 1 ;
		for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
			int v = edge[i].v ;
			if ( v == fa && flag ) {
				flag = 0 ;
				continue ;
			}
			if ( !dfn[v] ) {
				tarjan ( v , u ) ;
				low[u] = min ( low[u] , low[v] ) ;
			}
			else if ( ins[v] )
				low[u] = min ( low[u] , dfn[v] ) ;
		}
		if ( low[u] == dfn[u] ) {
			++ bcc_cnt ;
			while ( 1 ) {
				int v = S[-- top] ;
				ins[v] = 0 ;
				bcc[v] = bcc_cnt ;
				if ( v == u )
					break ;
			}
		}
	}
	
	void input () {
		REP ( i , m ) {
			line[i].input () ;
			addedge ( line[i].u , line[i].v ) ;
		}
	}
	
	int dfs ( int u , int fa ) {
		int cnt = 0 , j ;
		for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
			int v = edge[i].v ;
			if ( v != fa )
				++ cnt , j = i ;
		}
		if ( cnt > 1 || !cnt )
			return INF ;
		return min ( edge[j].w , dfs ( edge[j].v , u ) ) ;
	}
	
	void solve () {
		REPF ( i , 1 , n )
			if ( !dfn[i] )
				tarjan ( i , -1 ) ;
		cntE = 0 ;
		clear ( adj , -1 ) ;
		REP ( i , m ) {
			int u = bcc[line[i].u] ;
			int v = bcc[line[i].v] ;
			if ( u != v ) {
				++ deg[u] , ++ deg[v] ;
				addedge ( u , v , line[i].w ) ;
			}
		}
		int min1 = INF , min2 = INF , min3 = INF ;
		REPF ( i , 1 , bcc_cnt )
			if ( deg[i] == 1 ) {
				int tmp = dfs ( i , -1 ) ;
				if ( min1 >= tmp )
					min3 = min2 , min2 = min1 , min1 = tmp ;
				else
				if ( min2 >= tmp )
					min3 = min2 , min2 = tmp ;
				else
				if ( min3 >= tmp )
					min3 = tmp ;
			}
		if ( min3 == INF )
			printf ( "-1\n" ) ;
		else
			printf ( "%d\n" , min3 ) ;
	}
} ;

BCC c ;

void work () {
	while ( ~scanf ( "%d%d" , &c.n , &c.m ) ) {
		c.init () ;
		c.input () ;
		c.solve () ;
	}
}

int main () {
	work () ;
	return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值