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

The war

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2815    Accepted Submission(s): 673


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
 

     题意: 给一幅图 敌人会加一条边,你要删一条边,使得图不连通且花费费用最少

    分析: 缩点之后,从最小边的两个端点 dfs  找到里面的次小边就是答案啦~~

AC 代码:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<stack> 
#include<algorithm>
#define inf 1e9
using namespace std;
struct edge
{
	int u,v,w;
	edge(){}
	edge(int uu,int vv,int ww)
	{
		u=uu,v=vv,w=ww;
	}
};
vector<edge>G,Gs;
vector<int>v[20020];
int low[20010],viss[20010],dfn[20010];
int cnt,index;
void init()
{
	G.clear();
	Gs.clear();
	for(int i=0;i<=15010;i++)
	v[i].clear();
	memset(dfn,0,sizeof(dfn));
	memset(viss,0,sizeof(viss));
	cnt=0,index=0;
}
void addedge(int from,int to,int val)
{
	G.push_back(edge(from,to,val));
	G.push_back(edge(to,from,val));
	int m=G.size();
	v[from].push_back(m-2);
	v[to].push_back(m-1);
}
stack<int>S;
vector<int>vs[20010];
void tarjan(int x,int pre)
{
	dfn[x]=low[x]=++index;
	S.push(x);
	int flag=0;
	for(int i=0;i<v[x].size();i++)
	{
		int y=G[v[x][i]].v;
		if(y==pre&&!flag)
		{
			flag=1;
			continue;
		}
		if(!dfn[y])
		{
			tarjan(y,x);
			low[x]=min(low[x],low[y]);
		}
		else if(!viss[y])
		low[x]=min(low[x],dfn[y]);
	}
	if(low[x]==dfn[x])
	{
		cnt++;
		while(1)
		{
			int y=S.top();
			S.pop();
			viss[y]=cnt;
			if(x==y)
			break;
		}
	}
}
int ans;
int dfs(int x,int pre)
{
	int fi=inf,ci=inf;
	for(int i=0;i<vs[x].size();i++)
	{
		if(Gs[vs[x][i]].v==pre)
		continue;
		ci=min(min(dfs(Gs[vs[x][i]].v,x),Gs[vs[x][i]].w),ci);
		if(fi>ci)
		{
			int t=fi;
			fi=ci;
			ci=t;
		}
	}
	ans=min(ans,ci);
	return fi;
}
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)==2)
	{
		int mina,minb,minw;
		init();
		minw=inf;
		for(int i=0;i<m;i++)
		{
			int a,b,w;
			scanf("%d%d%d",&a,&b,&w);
			addedge(a,b,w);
			
		}
		for(int i=1;i<=n;i++)
		{
			if(!dfn[i])
			tarjan(i,-1);
		}
		for(int i=0;i<=cnt;i++)
		vs[i].clear();
		mina=-1,minb=-1;
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<v[i].size();j++)
			{
				int y=G[v[i][j]].v;
				if(viss[i]!=viss[y])
				{
					Gs.push_back(edge(viss[i],viss[y],G[v[i][j]].w));
					vs[viss[i]].push_back(Gs.size()-1);
					if(G[v[i][j]].w<minw)
					{
						minw=G[v[i][j]].w;
						mina=viss[i];
						minb=viss[y];
					}
				}
			}
		}
		if(mina==-1||minb==-1)
		{
			printf("-1\n");
			continue;
		}
		ans=inf;
		dfs(minb,mina);
		dfs(mina,minb);
		if(ans!=inf)
		printf("%d\n",ans);
		else
		printf("-1\n");
	} 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值