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 <= N 2 )
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
曹操在赤壁战役中被诸葛亮和周瑜击败。但他不会放弃的曹操的军队还不擅长水战,所以他想出了另一个主意。他在长江上建了许多岛屿,以这些岛屿为基础,曹操的军队很容易攻击周瑜的军队。曹操还修建了连接岛屿的桥梁。如果所有岛屿都通过桥梁相连,曹操的军队就可以在这些岛屿之间部署得非常方便。周瑜受不了,所以他想摧毁一些曹操的桥梁,这样一个或多个岛屿就会与其他岛屿分开。但周瑜只有一颗原子弹是诸葛亮留下的,所以他只能摧毁一座桥。周宇必须派人携带炸弹去摧毁这座桥。桥上可能有警卫轰炸队的士兵人数不能少于一座桥的警卫人数,否则任务就会失败。请算出周瑜要派多少战士来完成海岛分岛任务。?
?输入?
?测试用例不超过 12 个。在每个测试用例中:?
?第一行包含两个整数,N 和 M,这意味着有 N 个孤岛和 M 桥。 ?
?所有岛屿的编号从 1 到 N ( 2 <= N <= 1000, 0 < M <= N ??2?? )?
?下一个 M 线描述 M 网桥。每行包含三个整数 U、V 和 W,这意味着有一个连接您岛和岛屿 V 的桥,并且桥上有 W 防护装置。(U = V 和 0 <= W <= 10,000)?
?输入以 N = 0 和 M = 0??结束。?
?输出?
?对于每个测试用例,打印周瑜为完成任务而必须发送的最低士兵编号。如果周瑜不能成功,请改为打印-1。
这个题有几个坑点:
1.要判断重边
2.如果士兵为0要输出1
3.如果图不连通就输出0(这个没想到一直wa)
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<queue>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
const int N=1e5+10;
const int prime=2333317;
const int INF=0x3f3f3f3f3f;
int dfn[N],low[N],head[N],su[N];
int dfs_clock,k,bridge,n,m,block;
struct node
{int v,next,w;
}edge[N*100];
void add(int u,int v,int w)
{edge[k].v=v;edge[k].w=w;edge[k].next=head[u];head[u]=k++;
}
void init()
{memset(head,-1,sizeof(head));memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));memset(su,0,sizeof(su));dfs_clock=bridge=k=0;block=1;
}
void tarjan(int u,int pre)
{low[u]=dfn[u]=++dfs_clock;int ok=0;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].v;if(v==pre&&ok==0){ok++;continue;}if(!dfn[v]){block++;tarjan(v,u);low[u]=min(low[u],low[v]);if(low[v]>dfn[u]){su[bridge++]=edge[i].w;}}elselow[u]=min(low[u],dfn[v]);}
}
int main()
{while(scanf("%d%d",&n,&m)!=EOF&&n+m){init();for(int i=1;i<=m;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);add(u,v,w);add(v,u,w);}tarjan(1,1);if(bridge==0){printf("-1\n");continue;}if(block<n){printf("0\n");continue;}sort(su,su+bridge);if(su[0]==0){printf("1\n");continue;}printf("%d\n",su[0]);}return 0;
}