题目描述
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.
输入格式
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.输出格式
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.
输入样例
3 3
1 2 7
2 3 4
3 1 4
3 2
1 2 7
2 3 4
0 0输出样例
-1
4
分析
题目大意是给定一个可能含有重边的无向图,求桥的最小边权,如果图本身不连通,则输出0;图连通且最小边权为0,输出1(至少要派出一个人);图连通且不存在桥,则输出-1。
由于图中可能存在重边,因此需要给边一个编号,将tarjan算法中判断v不是u的父节点,改为判断v不是u同边的父节点。
源程序
tarjan算法
#include <bits/stdc++.h>
#define MAXN 1005
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
int v,w,id,next; //id为边的编号
Edge(){}
Edge(int v,int w,int next,int id):v(v),w(w),next(next),id(id){}
}edge[MAXN*MAXN*2];
int EdgeCount,head[MAXN];
int n,m,cnt,ans;
int dfn[MAXN],low[MAXN];
void addEdge(int u,int v,int w,int id)
{
edge[++EdgeCount]=Edge(v,w,head[u],id);
head[u]=EdgeCount;
}
void init()
{
memset(head,0,sizeof(head));
memset(dfn,0,sizeof(dfn));
EdgeCount=cnt=0;
ans=INF;
}
int read()
{
int sum=0;
char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9'){
sum=sum*10+c-'0';
c=getchar();
}
return sum;
}
void tarjan(int u,int pre)
{
dfn[u]=low[u]=++cnt;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v,w=edge[i].w,id=edge[i].id;
if(!dfn[v]){ //还没访问过
tarjan(v,id);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]) //满足桥的条件
ans=min(ans,w);
}
else if(id!=pre) //已经访问过且v不是u的同边的父节点
low[u]=min(low[u],dfn[v]);
}
}
int main()
{
while(1){
init();
n=read(),m=read();
if(!n&&!m)break;
for(int i=1;i<=m;i++){
int u=read(),v=read(),w=read();
addEdge(u,v,w,i);
addEdge(v,u,w,i);
}
tarjan(1,-1);
bool flag=false;
for(int i=1;i<=n;i++){ //判断连通性
if(!dfn[i]){
flag=true;
break;
}
}
if(flag)ans=0; //图不连通
else if(ans==0) ans=1; //桥最小守卫人数为0
else if(ans==INF) ans=-1; //不存在桥
printf("%d\n",ans);
}
}