hdu 4738 Caocao's Bridges 【求最小代价的割边(桥)】
来源:互联网 发布:asp 网页访问数据库 编辑:程序博客网 时间:2024/05/22 06:14
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
题目大意:曹操在赤壁之战中失利,但他并没有放弃,他想到一个好主意:填江造陆。在长江上建造人工陆地,并且陆地之间建桥方便到达。这时候,周瑜看不下去了,他决定炸桥,使得不同陆地之间分离,但只有一个炸弹,他想以最小的死伤数完成炸桥任务。请你帮他计算。。。。
知识点:割边(桥)
AC代码:
# include <cstdio># include <cstring># include <algorithm>using namespace std;# define MAXN 1005# define INF 1000000000struct EDGE{ int v; int cut; int w; int next;}edge[MAXN * MAXN];int tot;int top;int scc;int index;int bridge;int head[MAXN];int dfn[MAXN];int low[MAXN];int belong[MAXN];int stack[MAXN];int instack[MAXN];int minsoldiers;void Init(){ tot = 0; top = 0; scc = 0; index = 0; minsoldiers = INF; memset(head, -1, sizeof(head)); memset(dfn, 0, sizeof(dfn)); memset(instack, 0, sizeof(instack));}void Addedge(int u, int v, int w){ edge[tot].v = v; edge[tot].cut = 0; edge[tot].w = w; edge[tot].next = head[u]; head[u] = tot++;}void Tarjan(int u, int pre){ int v; dfn[u] = low[u] = ++index; int flag = 1; for (int i = head[u]; i != -1; i = edge[i].next) { v = edge[i].v; if (flag && v == pre) //去重 { flag = 0; continue; } if (!dfn[v]) { Tarjan(v, u); if (low[v] < low[u]) { low[u] = low[v]; } if (low[v] > dfn[u]) { bridge++; edge[i].cut = 1; if (edge[i].w < minsoldiers) { minsoldiers = edge[i].w; } } } else if (dfn[v] < low[u]) { low[u] = dfn[v]; } }}void Solve(int n){ int i; int mark = 1; Tarjan(1, 1); for (i = 1; i <= n; i++) { if (!dfn[i]) { mark = 0; } } if (!mark) //当一开始就有孤立的陆地时 { printf("0\n"); return; } if (INF == minsoldiers) //当所建的桥没有割边时 { printf("-1\n"); return; } if (!minsoldiers) //当是割边,并且该桥上没有守卫时 { printf("1\n"); return ; } if (minsoldiers) //有损伤,但能完成任务时 { printf("%d\n", minsoldiers); return ; }}int main(void){ int n, m; while (~scanf("%d %d", &n, &m), n && m) { int i; Init(); for (i = 1; i <= m; i++) { int u, v, w; scanf("%d %d %d", &u, &v, &w); Addedge(u, v, w); Addedge(v, u, w); } Solve(n); } return 0;}
- hdu 4738 Caocao's Bridges 【求最小代价的割边(桥)】
- hdu - 4738 Caocao's Bridges 割边
- HDU 4738 Caocao's Bridges(求价值最小的桥)
- hdu 4738 Caocao's Bridges 图的割边
- hdu 4738 Caocao's Bridges (割边/桥)
- hdu 4738 Caocao's Bridges 割边无敌(trick)
- HDU-4738-Caocao's Bridges(割边)
- HDU 4738 Caocao's Bridges (割边)
- hdu 4738 Caocao's Bridges【求最小权值的桥】
- HDU 4738Caocao's Bridges 邻接表tarjan 割桥
- hdu 4738 Caocao's Bridges(Tarjan求无向图的桥)
- 【割桥】 HDU 4738 Caocao's Bridges 重边 割桥权值最小值
- [HDU 4738] Caocao's Bridges Tarjan算法求桥
- hdoj 4738 Caocao's Bridges 【无向图边-双联通 求所有桥中权值最小的】
- HDU4738 Caocao's Bridges (求桥)
- HDU 4738 --Caocao's Bridges 【无向图边双联通 && 求权值最小的桥 && 模板】
- HDU - 4738 Caocao's Bridges(桥)
- HDU 4738 Caocao's Bridges(找割边)
- CodeForces
- ants run 题意理解
- 移动零
- Follow me 挑战程序与设计
- hdu 1875 畅通工程再续(MST)
- hdu 4738 Caocao's Bridges 【求最小代价的割边(桥)】
- 读书笔记:Hyperbolic reductions for Einstein's equations(Helmut Friedrich 1996)
- CS231n系列课程Lecture 5:Train Neural Networks, Part 1
- js输出选中的多选框的个数
- 第三十五讲项目一 另类求和
- git merge
- 迭代模式(Iterator Pattern 游标模式 CursorPattern,对象行为型模式)
- 警车声音
- java学习路线图