2015
09-17

# Caocao’s Bridges

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.

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.

3 3
1 2 7
2 3 4
3 1 4
3 2
1 2 7
2 3 4
0 0

-1
4

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<string>
#include<map>
#include<cmath>
#define clr(a,b) memset((a),b,sizeof((a)))
#define PB(x) push_back(x)
#define INF 0xfffffff
using namespace std;

const int maxn=1010;
const int maxm=maxn*maxn*2;

int num,tot;
int ans;

struct node{
int v,c,next,id;
}e[maxm];

int n,m;

void tarjan(int u,int fa){
dfn[u]=low[u]=++num;
int v=e[i].v;
int id=e[i].id;
if(fa==id) continue;
if(!dfn[v]){
tarjan(v,id);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v]){
ans=min(ans,e[i].c);
}
}
else{
low[u]=min(low[u],dfn[v]);
}
}
}

void add(int u,int v,int c,int id){
e[tot].c=c;
e[tot].v=v;
e[tot].id=id;
}

int main()
{
while(cin>>n>>m,n||m){
int u,v,c;
tot=0;
for(int i=1;i<=m;++i){
scanf("%d%d%d",&u,&v,&c);
}
memset(dfn,0,sizeof(dfn));
num=0;
int ok=0;
ans=INF;
for(int i=1;i<=n;++i)
if(!dfn[i]){
ok++;
tarjan(i,0);
}
if(ans==0) ans=1;
if(ans==INF) ans=-1;
if(ok>1) ans=0;
for(int i=1;i<=n;++i) cout<<dfn[i]<<"    "<<low[i]<<endl;
cout<<ans<<endl;
}
return 0;
}

,