The war HDU - 4005 (双联通分量+缩点)

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. 
     
 

题意:

有一幅图,现在要加一条边,加边后要你删除一条边,费用为边的费用,要求你费用固定后,

无论边加在哪里,你用该费用都可以删去一条边,使得图不连通,求此费用的最小值

思路:

要想不连通,删去的一定是图上的桥,

首先要进行缩点,把图缩成一棵树,因此,加入一条边后图就会出现一个环,环中任意一条边删去

都不会使得图不连通

之后找到一条最小边,这条边一定在加边后的联通快里,如果不在,则可以删除这条最小边,而达不到目的。

找到最小边后,从边的两端点遍历一条路径,

并且边上的权值要尽可能的小,因为这样才能让不在环中的边尽可能的大,然后,答案就是每个节点的次小儿子的最小值,如果没有次小儿子就不能算(就是说只有一个儿子,即节点不是三叉的),因为我完全可以把它和最小的边放到一个连通块中,那样答案就应该更大了。

终上所述:先进行无向图的缩点,再在树上找最小的边,最后分别从边的两点出发,遍历树,找节点的次小儿子节点中的最小值

举个简单的例子(括号内的数字代表边上的权值)1和8间的权值为1,是最小的

                     1---8

                  /           \(3) 

        (2)/               \

             2                  3

    (4) /       \(5)    (6)/      \(7)

       /           \         /          \

     4              5     6              7

左子树中2的子节点有次小值5,右子树中3的子节点次小值为7,两个次小值间的最小值是5,即答案

现在,比如所你要把3、4连起来。我可以去掉2、5之间的边让图不连通,花费为5,把3、5连起来,我自然可以删掉2、4,花费为4,一个节点的次小值和最小值(比如说4、5两点)不可能被同时连进一个连通块(或环)中(因为必须把最小的那条边加进环中),正是利用这个性质,不管把那两个点连起来,我们都可以找到一个最小值或次小值来删掉使图不连通,注意:再重复一遍,同一个节点的最小值和次小值不会被加进同一个环,因此,这些次小值中的最小的那条边的权值就是答案。(这时你如果把次小的边加进环中,如2--5,自然可以删掉一条更小的边 如2--4 使图不连通,相反,如果没有把次小的边加进去,那次小的就是答案)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#define Inf 0x3f3f3f3f
using namespace std;
const int N = 10005;
struct node{
	int u,v;
	int w;
	int next;
}edge[100005],temp;
stack<int>p;
int head[N],id;
int LOW[N],DFN[N],index;
int vis[N],include[N],type;
int mp[N][3],cnt;
int ans;
int n,m;
void init1(){
	id=0;
	temp.w=Inf;
	memset(head,-1,sizeof(head));
}
void init2(){
	type=index=0;
	memset(DFN,0,sizeof(DFN));
	memset(LOW,0,sizeof(LOW));
	memset(vis,0,sizeof(vis));
	while(!p.empty()) p.pop();
}
void add(int u,int v,int w){
	edge[id].u=u;
	edge[id].v=v;
	edge[id].w=w;
	edge[id].next=head[u];
	head[u]=id++;
}
void tarjan(int u,int fa){  //缩点,求所有的桥 
	LOW[u]=DFN[u]=++index;
	vis[u]=1;
	p.push(u);
	for(int i=head[u];i!=-1;i=edge[i].next){
		int v=edge[i].v;
		int w=edge[i].w;
		if(v==fa) continue;
		if(!DFN[v]){
			tarjan(v,u);
			LOW[u]=min(LOW[v],LOW[u]);
			//printf("lu:%d  lv:%d\n",LOW[u],LOW[v]);
			if(LOW[v]>LOW[u]){  //桥的判断条件 
				mp[cnt][0]=u;
				mp[cnt][1]=v;
				mp[cnt][2]=w;
				cnt++;
			}
		}
		else LOW[u]=min(DFN[v],LOW[u]); 
	}
	if(LOW[u]==DFN[u]){
		int v;
		include[u]=++type;
		do{
			v=p.top();
			p.pop();
			vis[v]=0;
			include[v]=type;
		}while(v!=u);
	}
}
void CSS(){
	init2();
	for(int i=1;i<=n;i++)
	   if(!DFN[i]) tarjan(i,0);
	init1();
    for(int i=0;i<cnt;i++){
    	int u=mp[i][0];
    	int v=mp[i][1];
    	int w=mp[i][2];
    	int x=include[u];
    	int y=include[v];
    	if(x!=y){
    		add(x,y,w);
    		add(y,x,w);
    		if(temp.w>w){   //花费最小的桥 
    			temp.w=w;
    			temp.u=x;
    			temp.v=y;
			}
		}
	}
}
int Find(int s,int t){  //以s为根节点的最小值 
	int fi=Inf,sc=Inf;
	for(int i=head[s];i!=-1;i=edge[i].next){
		int v=edge[i].v;
		int w=edge[i].w;
		if(v==t) continue;
		int x=Find(v,s);
		int mint=min(w,x); 
		if(mint<=fi){
			sc=fi;
			fi=mint;
		}
		else sc=min(sc,mint);
	}
	ans=min(ans,sc);
	return fi;
}
int main(){
	while(~scanf("%d%d",&n,&m)){
		 init1();
		 cnt=0;
		 for(int i=0;i<m;i++){
		 	int u,v,w;
		 	scanf("%d%d%d",&u,&v,&w);
		 	add(u,v,w);
		 	add(v,u,w);
		 }
		 CSS();
		 ans=Inf;
		 Find(temp.u,temp.v);
		 Find(temp.v,temp.u);
		 if(ans==Inf) printf("-1\n"); 
		 else printf("%d\n",ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值