Caocao's Bridges
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 194 Accepted Submission(s): 89
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 2 )
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.
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 2 )
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
Recommend
liuyiding
這題的意思就是求出所有的橋,然后輸出橋的權值的最小值。
但是坑點比較多。
如果一開始是不連通的,輸出0.
圖有重邊,需要處理。
還有如果取到的最小值是0的話,要輸出1,表示要派一個人過去。
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013/9/15 星期日 12:11:49 4 File Name :2013杭州網絡賽\1001.cpp 5 ************************************************ */ 6 7 #pragma comment(linker, "/STACK:1024000000,1024000000") 8 #include <stdio.h> 9 #include <string.h> 10 #include <iostream> 11 #include <algorithm> 12 #include <vector> 13 #include <queue> 14 #include <set> 15 #include <map> 16 #include <string> 17 #include <math.h> 18 #include <stdlib.h> 19 #include <time.h> 20 using namespace std; 21 const int INF = 0x3f3f3f3f; 22 /* 23 * 求 無向圖的割點和橋 24 * 可以找出割點和橋,求刪掉每個點后增加的連通塊。 25 * 需要注意重邊的處理,可以先用矩陣存,再轉鄰接表,或者進行判重 26 */ 27 const int MAXN = 10010; 28 const int MAXM = 2000010; 29 struct Edge 30 { 31 int to,next; 32 int w; 33 bool cut;//是否為橋的標記 34 }edge[MAXM]; 35 int head[MAXN],tot; 36 int Low[MAXN],DFN[MAXN],Stack[MAXN]; 37 int Index,top; 38 bool Instack[MAXN]; 39 bool cut[MAXN]; 40 int add_block[MAXN];//刪除一個點后增加的連通塊 41 int bridge; 42 43 void addedge(int u,int v,int w) 44 { 45 edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut = false; 46 edge[tot].w = w; 47 head[u] = tot++; 48 } 49 50 51 void Tarjan(int u,int pre) 52 { 53 int v; 54 Low[u] = DFN[u] = ++Index; 55 Stack[top++] = u; 56 Instack[u] = true; 57 int son = 0; 58 int pre_num = 0; 59 for(int i = head[u];i != -1;i = edge[i].next) 60 { 61 v = edge[i].to; 62 if(v == pre && pre_num == 0) 63 { 64 pre_num++; 65 continue; 66 67 } 68 if( !DFN[v] ) 69 { 70 son++; 71 Tarjan(v,u); 72 if(Low[u] > Low[v])Low[u] = Low[v]; 73 //橋 74 //一條無向邊(u,v)是橋,當且僅當(u,v)為樹枝邊,且滿足DFS(u)<Low(v)。 75 if(Low[v] > DFN[u]) 76 { 77 bridge++; 78 edge[i].cut = true; 79 edge[i^1].cut = true; 80 } 81 //割點 82 //一個頂點u是割點,當且僅當滿足(1)或(2) (1) u為樹根,且u有多於一個子樹。 83 //(2) u不為樹根,且滿足存在(u,v)為樹枝邊(或稱父子邊, 84 //即u為v在搜索樹中的父親),使得DFS(u)<=Low(v) 85 if(u != pre && Low[v] >= DFN[u])//不是樹根 86 { 87 cut[u] = true; 88 add_block[u]++; 89 } 90 } 91 else if( Low[u] > DFN[v]) 92 Low[u] = DFN[v]; 93 } 94 //樹根,分支數大於1 95 if(u == pre && son > 1)cut[u] = true; 96 if(u == pre)add_block[u] = son - 1; 97 Instack[u] = false; 98 top--; 99 } 100 int solve(int N) 101 { 102 memset(DFN,0,sizeof(DFN)); 103 memset(Instack,false,sizeof(Instack)); 104 memset(add_block,0,sizeof(add_block)); 105 memset(cut,false,sizeof(cut)); 106 Index = top = 0; 107 bridge = 0; 108 for(int i = 1;i <= N;i++) 109 if( !DFN[i] ) 110 Tarjan(i,i); 111 int ret = INF; 112 for(int u = 1; u <= N;u++) 113 for(int i = head[u]; i != -1;i = edge[i].next) 114 if(edge[i].cut) 115 ret = min(ret,edge[i].w); 116 if(ret == INF)ret = -1; 117 if(ret == 0)ret++; 118 return ret; 119 } 120 int F[MAXN]; 121 int find(int x) 122 { 123 if(F[x] == -1)return x; 124 else return F[x] = find(F[x]); 125 } 126 void init() 127 { 128 memset(F,-1,sizeof(F)); 129 tot = 0; 130 memset(head,-1,sizeof(head)); 131 } 132 void bing(int u,int v) 133 { 134 int t1 = find(u); 135 int t2 = find(v); 136 if(t1 != t2)F[t1] = t2; 137 } 138 int main() 139 { 140 //freopen("in.txt","r",stdin); 141 //freopen("out.txt","w",stdout); 142 int n,m; 143 while(scanf("%d%d",&n,&m) == 2) 144 { 145 if(n == 0 && m == 0)break; 146 int u,v,w; 147 init(); 148 while(m--) 149 { 150 scanf("%d%d%d",&u,&v,&w); 151 if(u == v)continue; 152 addedge(u,v,w); 153 addedge(v,u,w); 154 bing(u,v); 155 } 156 bool flag = true; 157 for(int i = 1; i <= n;i++) 158 if(find(i) != find(1)) 159 flag = false; 160 if(!flag) 161 { 162 printf("0\n"); 163 continue; 164 } 165 printf("%d\n",solve(n)); 166 } 167 return 0; 168 }