Caocao's Bridges

Caocao‘s Bridges

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

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

2013 ACM/ICPC Asia Regional Hangzhou Online

这题的意思就是求出所有的桥,然后输出桥的权值的最小值。

但是坑点比较多。

如果一开始是不连通的,输出0.

图有重边,需要处理。

还有如果取到的最小值是0的话,要输出1,表示要派一个人过去。

  1 #include <iostream>
  2 #include <queue>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <algorithm>
  6 #include <cmath>
  7 #include <cstdlib>
  8 #include <limits>
  9 #include <stack>
 10 #include <vector>
 11 #include <map>
 12
 13 using namespace std;
 14
 15 #define N 1350
 16 #define INF 0xfffffff
 17 #define PI acos (-1.0)
 18 #define EPS 1e-83
 19
 20 struct node
 21 {
 22     int v, next, flow;
 23 }edge[N*N];
 24
 25 int n, m, Time, minx, cnt;
 26 int low[N], dfn[N], f[N], head[N];
 27
 28 vector<vector<int> > G;
 29
 30 void init()
 31 {
 32     cnt = Time = 0;
 33     memset(low, 0, sizeof(low));
 34     memset(dfn, 0, sizeof(dfn));
 35     memset(f, 0, sizeof(f));
 36     memset(head, -1, sizeof(head));
 37 }
 38
 39 void addedge(int u, int v, int flow)   // 邻接表存边
 40 {
 41     edge[cnt].v = v;
 42     edge[cnt].flow = flow;
 43     edge[cnt].next = head[u];
 44     head[u] = cnt;
 45     cnt++;
 46 }
 47
 48 void Tarjan(int u, int fa)
 49 {
 50     f[u] = fa;
 51     low[u] = dfn[u] = ++Time;
 52     int k = 0;     // 去重边
 53
 54     for(int i = head[u]; i != -1; i = edge[i].next)
 55     {
 56         int v = edge[i].v;
 57         if(fa == v && !k)
 58         {
 59             k++;
 60             continue;
 61         }
 62         if(!low[v])
 63         {
 64             Tarjan(v, u);
 65             low[u] = min(low[u], low[v]);
 66             if(low[v] > dfn[u])
 67                 minx = min(minx, edge[i].flow);  // 求桥的最小值
 68         }
 69         else
 70             low[u] = min(low[u], dfn[v]);
 71     }
 72 }
 73
 74 int main()
 75 {
 76     int u, v, flow, k;
 77
 78     while(scanf("%d%d", &n, &m), n+m)
 79     {
 80         init();
 81
 82         while(m--)
 83         {
 84             scanf("%d%d%d", &u, &v, &flow);
 85             addedge(u, v, flow);
 86             addedge(v, u, flow);
 87         }
 88         k = 0, minx = INF;
 89
 90         for(int i = 1; i <= n; i++)
 91         {
 92             if(!low[i])
 93             {
 94                 Tarjan(i, -1);
 95                 k++;   // 判断有几个连通图,
 96             }
 97         }
 98         if(k > 1) // 如果连通图大于1,输出0
 99         {
100             puts("0");
101             continue;
102         }
103         if(!minx)  // 桥最小值是0,需要派一个区攻击
104             minx++;
105         else if(minx == INF)  // 没有桥
106             minx = -1;
107         printf("%d\n", minx);
108     }
109     return 0;
110 }

Caocao's Bridges

时间: 2024-10-14 05:14:54

Caocao's Bridges的相关文章

HDU 4738 Caocao&#39;s Bridges(割边)

乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<algorithm> #include<stack> #include<cstring> using namespace std; #define maxn

HDU4738 Caocao&#39;s Bridges 无向图的桥

一眼题:找所有的桥,然后求最小权值 但是有很多坑点 1:如果本来不联通 输出0,(这个坑我知道) 2:但是还有一个坑,就是当整个连通,最小桥的权值是0时,也必须派一个人去,wa了无数遍(还是太年轻) #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <string> #include

HDU 4738 Caocao&#39;s Bridges(找割边)

HDU 4738 Caocao's Bridges 题目链接 注意几个坑,可能重边,至少要派一个人去炸,没有连通的时候就不用炸了 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 1005; const int INF = 0x3f3f3f3f; int pre[N], low[N

[HDOJ4738]Caocao&#39;s Bridges(双联通分量,割边,tarjan)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 给一张无向图,每一条边都有权值.找一条割边,使得删掉这条边双连通分量数量增加,求权值最小那条. 注意有重边,ACEveryDay里群巨给的意见是tarjan的时候记录当前点是从哪条边来的. 注意假如桥的权值是0的时候也得有一个人去炸…… 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7

HDU 4738 Caocao&#39;s Bridges tarjan求桥

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 Chan

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

hdu 4738 Caocao&#39;s Bridges tarjan

Caocao's Bridges Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4738 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

HDU 4738 Caocao&#39;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

Hdu 4738 Caocao&#39;s Bridges (连通图+桥)

题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这个边一定是一个桥,所以我们只需要求出来所有的桥,然后比较每个桥的花费,选取最小的那个就好. 看起来很简单的样子哦!但是这个题目有很多的细节: A:题目中有重边,以后写Tarjan还是清一色判断重边吧.(除非题目特别要求) B:m个桥有可能连通不了这n个桥,这个时候不需要花费. C:当最小花费桥的花费

HDU4738 Caocao&#39;s Bridges(桥)

http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:给定一张无向图,求其中权值最小的一座桥,派最少的士兵去炸掉它!! 思路:直接用tarjan计算出桥并且取其中权值最小者. 此题坑点甚多,1.有可能桥本来就不联通,输出-1.2.桥最小者为0,输出1(至少排一个人去炸桥).3.不要去重边,两个岛之间允许有多座桥,tarjan忽略返回边只忽略一次,加pre_num处理下. 代码: #include<iostream> #include<cstd