hdu4738 Caocao's Bridges(双连通分量割边/桥)
来源:互联网 发布:什么漫画软件好 编辑:程序博客网 时间:2024/05/17 16:56
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.
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 31 2 72 3 43 1 43 21 2 72 3 40 0
Sample Output
-14
题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥,使得这n座岛不连通,求最少要派多少人去。
分析:只需要用Tarjan算法求出图中权值最小的那条桥就行了。注意:
(1)如果图不连通,不用派人去炸桥,直接输出0
(2)可能会有重边(有两种处理方法,一种是先把所有的边存下,发现两点有重边的时候就只给这两个点连一条权值为无穷大的边。或者是在tarjan算法里处理重边,即使之求u或u的子树能够追溯到的最早的栈中节点的次序号时可访问父节点的次序号。)
(3)如果桥上没有士兵守着,那至少要派一个人去炸桥。
#include <iostream>#include <string.h>using namespace std;#define maxn 1100#define maxe 1000100struct Edge{ int v,w,next;}edge[maxe];int head[maxn], dfn[maxn],low[maxn],counter,len,cnt,ans;bool vis[maxn];void AddEdge(int u,int v,int w) //添加边{ edge[len].v=v; edge[len].w=w; edge[len].next=head[u]; head[u]=len++;}void dfs(int u) //联通性判断{ if (vis[u]) { return; } vis[u]=true; cnt++; for (int i=head[u]; i!=-1; i=edge[i].next) { dfs(edge[i].v); }}void Tarjan(int u,int pre){ int v; int cnt2=0; low[u]=dfn[u]=++counter; for (int i=head[u]; i!=-1; i=edge[i].next) { v=edge[i].v; if(!dfn[v]) { Tarjan(v,u); low[u]=min(low[u],low[v]); if(low[v]>dfn[u]) { if(edge[i].w<ans) { ans=edge[i].w; } } } else if(pre == v) { if (cnt2) { low[u]=min(low[u], dfn[v]); //重边 } cnt2++; } else { low[u]=min(low[u], dfn[v]); } }}int main(){ int n,m,u,v,w; while (scanf("%d%d",&n,&m) && (n||m)) { len=0; for (int i=0; i<=n; i++) { dfn[i]=0; vis[i]=false; } memset(head, -1, sizeof(head)); for (int i=0; i<m; i++) { scanf("%d%d%d",&u,&v,&w); AddEdge(u,v,w); AddEdge(v,u,w); } counter=0; cnt=0; dfs(1); if (cnt<n) { printf("0\n"); continue; } ans=1000000; Tarjan(1,-1); if (ans==1000000) { printf("-1\n"); } else { if (ans==0) { ans=1; } printf("%d\n",ans); } } return 0; }
0 0
- hdu4738 Caocao's Bridges(双连通分量割边/桥)
- HDU4738 Caocao's Bridges(桥)
- HDU4738-Caocao's Bridges
- HDU4738 Caocao's Bridges
- HDU4738 Caocao's Bridges (求桥)
- 【桥】HDU4738[Caocao's Bridges]题
- 【HDU】4738 Caocao's Bridges 双连通分量
- hdu4738 Caocao's Bridges 无向图 tarjan 求桥
- 无向图求桥 hdu4738 Caocao's Bridges
- HDU4738——Caocao's Bridges
- HDU4738 Caocao's Bridges【强连通】
- hdu 4738 Caocao's Bridges 2013 ACM-ICPC杭州赛区网络赛 1001 双连通分量
- Caocao's Bridges hdu4738 (网络赛 杭州赛区) hdu 4738
- hdu4738(双连通分量)
- HDU4738【边双连通分量】
- HDU - 4738 Caocao's Bridges(桥)
- hdu4638-tarjan求桥-Caocao's Bridges
- Caocao's Bridges HDOJ4738
- QT---字幕滚动效果,检测鼠标进入和离开控制字幕滚动
- Premiere工程移植到其他电脑上打不开的解决方案
- 学习之学籍管理系统
- Mobiscroll 2.15 去除Trial
- gcc: error trying to exec 'cc1': execvp: No such file or directory
- hdu4738 Caocao's Bridges(双连通分量割边/桥)
- python实现栅格计算器功能
- java线程锁 synchronized
- spring AOP 前置增强,后置增强小Demo
- Fault管理与自动化
- p123.30
- linux上 rlwrap安装
- C#记忆游戏的简单实现
- NYOJ_41 三个数从小到大排序