HDU4738(割边)

Caocao's Bridges

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


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
 

Source

 

题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥,使得这n座岛不连通,求最少要派多少人去。

分析:只需要用Tarjan算法求出图中权值最小的那条桥就行了。但是这题有神坑。

第一坑:如果图不连通,不用派人去炸桥,直接输出0

第二坑:可能会有重边,即两座岛之间有多座桥(坑T_T)

第三坑:如果桥上没有士兵守着,那至少要派一个人去炸桥。

 1 //2016.9.16
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <cstring>
 6 #define N 1005
 7 
 8 using namespace std;
 9 
10 const int inf = 0x3f3f3f3f;
11 int edge[N][N], vis[N], num[N], low[N], n, m, Index, ans, root;//vis表示连通的集合。
12 
13 void tarjan(int cur, int fa)
14 {
15     Index++;
16     num[cur] = low[cur] = Index;
17     vis[cur] = 1;
18     for(int i = 1; i <= n; i++)
19     {
20         if(edge[cur][i]!=inf)
21         {
22             if(num[i] == 0)
23             {
24                 tarjan(i, cur);
25                 low[cur] = min(low[cur], low[i]);
26                 if(low[i]>num[cur])
27                       if(ans>edge[cur][i])
28                           ans = edge[cur][i];
29             }else if(i != fa)
30             {
31                 low[cur] = min(low[cur], num[i]);
32             }
33         }
34     }
35     return;
36 }
37 
38 int main()
39 {
40     int u, v, w;
41     while(scanf("%d%d", &n, &m)!=EOF)
42     {
43         if(!n && !m)break;
44         memset(edge, inf, sizeof(edge));//图权值为inf表示断开
45         memset(num, 0, sizeof(num));
46         memset(low, 0, sizeof(low));
47         memset(vis, 0, sizeof(vis));
48         Index = 0, root = 1, ans = inf;
49         for(int i = 0; i < m; i++)
50         {
51             scanf("%d%d%d", &u, &v, &w);
52             if(edge[u][v] == inf)
53             {
54                 edge[u][v] = w;
55                 edge[v][u] = w;
56             }else//如果重边,必不为割边,但也不为断开,把权值设为inf一半
57             {
58                 edge[u][v] = inf/2;
59                 edge[v][u] = inf/2;
60             }
61         }
62         tarjan(1, root);
63         bool fg = true;//判断图是否连通
64         for(int i = 1; i <= n; i++)
65               if(!vis[i])
66             {
67                 printf("0
");
68                 fg = false;
69                 break;
70             }
71         if(!fg)continue;
72         if(ans == inf || ans == inf/2)printf("-1
");
73         else if(ans == 0)printf("1
");//若无守兵,至少派一人去
74         else printf("%d
", ans);
75     }
76 
77     return 0;
78 }
原文地址:https://www.cnblogs.com/Penn000/p/5877353.html