# The war

Problem Description

In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situation of the enemy's war zones, and known that these war zones can communicate to each other directly or indirectly through the network. We also know the enemy
is going to build a new communication line to strengthen their communication network. Our task is to destroy their communication network, so that some of their war zones can't communicate. Each line has its "cost of destroy". If we want to destroy a line,
we must spend the "cost of destroy" of this line. We want to finish this task using the least cost, but our enemy is very clever. Now, we know the network they have already built, but we know nothing about the new line which our enemy is going to build. In
this condition, your task is to find the minimum cost that no matter where our enemy builds the new line, you can destroy it using the fixed money. Please give the minimum cost. For efficiency, we can only destroy one communication line.

Input

The input contains several cases. For each cases, the first line contains two positive integers n, m (1<=n<=10000, 0<=m<=100000) standing for the number of the enemy's war zones (numbered from 1 to n), and the number of lines that our enemy has already build.
Then m lines follow. For each line there are three positive integer a, b, c (1<=a, b<=n, 1<=c<=100000), meaning between war zone A and war zone B there is a communication line with the "cost of destroy " c.

Output

For each case, if the task can be finished output the minimum cost, or output ‐1.

Sample Input

3 2
1 2 1
2 3 2
4 3
1 2 1
1 3 2
1 4 3

Sample Output

-1
3

Hint

For the second sample input: our enemy may build line 2 to 3, 2 to 4,

3 to 4. If they build line 2 to 3, we will destroy line 1 to 4, cost 3. If they

build line 2 to 4, we will destroy line 1 to 3, cost 2. If they build line 3 to 4,

we will destroy line 1 to 2, cost 1. So, if we want to make sure that we can

destroy successfully, the minimum cost is 3.

Source

The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

Recommend

lcy   |   We have carefully selected several similar problems for you:  4007 4010 4003 4008 4009

#include
#include
#include
#include
#include
using namespace std;

const int maxn=11000;
const int maxm=210000;

struct edge{
int u,v,w,next;
edge(int u0=0,int v0=0,int w0=0){
u=u0;v=v0;w=w0;
}
friend bool operator <(edge x,edge y){
return x.w<y.w;
}
}e[maxm];

vector vec;
bool mark[maxn];
vector > dfsmap;

}

void input(){
nc=0;
cnt=0;
index=1;
vec.clear();
for(int i=0;i<maxn;i++){
dfn[i]=0;
mark[i]=false;
}
int u,v,w;
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
}
}

void tarjan(int s,int father){
vec.push_back(s);
dfn[s]=low[s]=index++;
mark[s]=true;
bool flag=true;
int d=e[i].v;
if(d==father && flag){flag=false;continue;}
if(!dfn[d]){
tarjan(d,s);
low[s]=min(low[s],low[d]);
}else if(mark[d]){
low[s]=min(low[s],dfn[d]);
}
}
if(low[s]==dfn[s]){
int d;
nc++;
do{
d=vec.back();
vec.pop_back();
color[d]=nc;
mark[d]=false;
}while(d!=s);
}
}

pair dfs(int s,int father){
int first=(1<<30),second=(1<<30); for(int i=0;i tmp=dfs(d,s);
if(w"<<p1.first<<endl;
return make_pair(first,second);
}

void solve(){
for(int i=1;i<=n;i++){ if(!dfn[i]) tarjan(i,-1); } dfsmap.clear(); dfsmap.resize(n+4); edge mine=edge(0,0,(1<<30)); for(int i=0;i"< p1=dfs(mine.u,mine.v);
pair p2=dfs(mine.v,mine.u);
int ans=min(p1.second,p2.second);
if(ans>=(1<<30) ) cout<<"-1"<<endl;
else cout<<ans<<endl;
}

int main(){
while(scanf("%d%d",&n,&m)!=EOF){
input();
solve();
}
return 0;
}