HDU4738(Bridges)

Caocao’s Bridges

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 <= N*N )

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

題意

曹操在長江建造了許多島嶼,在這些島嶼的基礎上,曹操的軍隊很容易攻擊周瑜的部隊。曹操還建造了連接島嶼的橋樑。如果所有島嶼都通過橋樑相連,那麼曹操的軍隊可以在這些島嶼中非常方便地部署。周瑜看的眼紅,所以他想要摧毀一些曹操的橋樑,這樣一個或多個島嶼就會與其他島嶼分開。但周瑜只有一枚由諸葛亮留下的炸彈,所以他只能摧毀一座橋。周瑜必須派人攜帶炸彈來摧毀這座橋。橋上可能有守衛。轟炸隊的士兵數量不能低於橋樑的守衛數量,否則任務就會失敗。請弄清楚周瑜至少需要多少士兵。

思路

  1. 如果這個圖不是一個連通圖,那麼周瑜不用派兵去炸,直接輸出0
  2. 如果這個圖是個連通圖,並且是邊雙連通(沒有割邊)一顆炸彈不夠,輸出-1。
  3. 最坑的是如果橋上沒有士兵把守,邊權值是0。至少要派一個人扛炸藥,答案是1。
  4. 重邊不能用鄰接矩陣,有重邊就不是橋。tarjan中忽略一次父節點,第二次訪問說明有重邊。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
struct edge{
	int from;
	int to;
	int w;
	int next;
}e[maxn*maxn];
int head[maxn];
int low[maxn];
int dfn[maxn];
int cnt,tot,res,dfs;
void clear_set()
{
	cnt = tot = dfs = 0;
	res = inf;
	memset(head,-1,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
}
void addedge(int x,int y,int z)
{
	e[tot].from = x;
	e[tot].to = y;
	e[tot].w = z;
	e[tot].next = head[x];
	head[x] = tot++;
} 
void tarjan(int x,int fx)
{
	dfn[x] = low[x] = ++cnt;
	int k = 0;
	for(int i = head[x];~i;i = e[i].next){
		int y = e[i].to;
		if(y == fx && !k){			//忽略一次訪問父節點的反向邊,以後訪問就是重邊訪問了
			k++;
			continue;
		}		
		if(!dfn[y]){
			tarjan(y,x);
			low[x] = min(low[x],low[y]);
			if(low[y] > dfn[x]){
				dfs = 1;
				res = min(res,e[i].w);				//更新最小的橋權值
			}
		}
		else if(dfn[y] < dfn[x]){
			low[x] = min(low[x],dfn[y]);
		}
	}
}
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		if(n == 0 && m == 0)	break;
		clear_set();
		int x,y,z;
		for(int i = 0;i < m;i++){
			scanf("%d%d%d",&x,&y,&z);
			addedge(x,y,z);		addedge(y,x,z);
		}
		int k = 0,ans = 0;
		for(int i = 1;i <= n;i++){
			if(!dfn[i]){
				k++;
				if(k > 1)	break;
				tarjan(i,-1);
			}
		}
		if(k > 1){
			printf("0\n");
			continue;
		}
		if(dfs == 0){
			printf("-1\n");
		}
		else{
			if(res == 0){
				printf("1\n");
			}
			else{
				printf("%d\n",res);
			}
		}
	}
	return 0;
} 

願你走出半生,歸來仍是少年~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章