The war
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1588 Accepted Submission(s): 328
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
第一道题目额~~ 思路别人的~~ http://blog.csdn.net/fp_hzq/article/details/6769733
#include <iostream>
#include <cstring>
#include <cstdio>
#define INF 0x3f3f3f3f
#define BUG printf("here!\n")
using namespace std;
const int MAXN=15000;
const int MAXM=150000;
struct node
{
int u,v,w;
};
node edge[MAXN];
int first[MAXN],next[MAXM];
int low[MAXN],dfn[MAXN];
int stack[MAXN],ti,top,id;
int belong[MAXN],cc;
node arc[MAXN];
int arcid,n,m;;
int first1[MAXN],next1[MAXM],cc1;
node edge1[MAXM];
int vis[MAXN],ans;
inline void add_edge(int u,int v,int w)
{
edge[cc].u=u;
edge[cc].v=v;
edge[cc].w=w;
next[cc]=first[u];
first[u]=cc;
cc++;
}
inline void add_edge1(int u,int v,int w)
{
edge1[cc1].u=u;
edge1[cc1].v=v;
edge1[cc1].w=w;
next1[cc1]=first1[u];
first1[u]=cc1;
cc1++;
}
void tardfs(int u,int p)
{
low[u]=dfn[u]=ti++;
stack[top++]=u;
int i;
for(i=first[u];i!=-1;i=next[i])
{
int v=edge[i].v;
if(v==p)
continue;
if(dfn[v]==-1)
{
tardfs(v,u);
if(low[u]>low[v])
low[u]=low[v];
else if(low[v]>dfn[u])
{
arc[arcid].u=u;
arc[arcid].v=v;
arc[arcid].w=edge[i].w;
arcid++;
for(stack[top]=-1;stack[top]!=v;)
{
top--;
belong[stack[top]]=id;
}
id++;
}
}
else if(low[u]>dfn[v])
low[u]=dfn[v];
}
}
int tarjan()
{
memset(low,0,sizeof(low));
memset(dfn,-1,sizeof(dfn));
memset(belong,0,sizeof(belong));
ti=1;
top=0;
id=1;
arcid=0;
int i;
for(i=1;i<=n;i++)
{
if(dfn[i]==-1)
{
tardfs(i,-1);
while(top!=0)
{
top--;
belong[stack[top]]=id;
}
id++;
}
}
return id;
}
void find(int u)
{
int i;
low[u]=INF;
for(i=first1[u];i!=-1;i=next1[i])
{
int v=edge1[i].v;
if(!vis[v])
{
vis[v]=1;
find(v);
low[v]=min(low[v],edge1[i].w);
if(low[v]<low[u])
{
ans=min(ans,low[u]);
low[u]=low[v];
}
else
{
ans=min(ans,low[v]);
}
}
}
}
int build()
{
memset(first1,-1,sizeof(first1));
cc1=0;
int i;
int k=0;
for(i=0;i<arcid;i++)
{
add_edge1(belong[arc[i].u],belong[arc[i].v],arc[i].w);
add_edge1(belong[arc[i].v],belong[arc[i].u],arc[i].w);
if(arc[i].w<arc[k].w)
k=i;
}
memset(vis,0,sizeof(vis));
vis[belong[arc[k].u]]=vis[belong[arc[k].v]]=1;
ans=INF;
find(belong[arc[k].u]);
find(belong[arc[k].v]);
if(ans==INF)
return -1;
else
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int u,v,w;
memset(first,-1,sizeof(first));
memset(next,-1,sizeof(next));
cc=0;
int i;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
add_edge(v,u,w);
}
tarjan();
int oo=build();
printf("%d\n",oo);
}
return 0;
}