poj-3228并查集

Gold Transportation

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3823 Accepted: 1366

Description
Recently, a number of gold mines have been discovered in Zorroming State. To protect this treasure, we must transport this gold to the storehouses as quickly as possible. Suppose that the Zorroming State consists of N towns and there are M bidirectional roads among these towns. The gold mines are only discovered in parts of the towns, while the storehouses are also owned by parts of the towns. The storage of the gold mine and storehouse for each town is finite. The truck drivers in the Zorroming State are famous for their bad temper that they would not like to drive all the time and they need a bar and an inn available in the trip for a good rest. Therefore, your task is to minimize the maximum adjacent distance among all the possible transport routes on the condition that all the gold is safely transported to the storehouses.

Input
The input contains several test cases. For each case, the first line is integer N(1<=N<=200). The second line is N integers associated with the storage of the gold mine in every towns .The third line is also N integers associated with the storage of the storehouses in every towns .Next is integer M(0<=M<=(n-1)*n/2).Then M lines follow. Each line is three integers x y and d(1<=x,y<=N,0<d<=10000), means that there is a road between x and y for distance of d. N=0 means end of the input.

Output
For each case, output the minimum of the maximum adjacent distance on the condition that all the gold has been transported to the storehouses or “No Solution”.

Sample Input

4
3 2 0 0
0 0 3 3
6
1 2 4
1 3 10
1 4 12
2 3 6
2 4 8
3 4 5
0

Sample Output

6
思路:
1、题目大意是求最长路的最小值,一开始一直不理解,以为要一个人跑完全图,后来发现应该可以同时许多司机一起开,然后开完一段路就需要休息,求把所有金矿都运输完后最短路的最大边是多少。
2、使用并查集,每次两个点不在同一个集合,就合并,然后把所有的金矿数量,仓库储存量都放到根节点,其他节点归0,最后判定条件结束就是每个集合中的仓库储存量大于金矿数量,然后记录其中边的最大值。
3、把边按照距离从小到大排序,每次选出最小的边,加入。

#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=250;
struct Edge{
	int x,y,d;
	Edge(int x,int y,int d):x(x),y(y),d(d){}
	bool operator< (const Edge& e) const{
		return d<e.d;
	}
} ;
int father[MAXN];
vector<Edge> vec;
int g[MAXN],s[MAXN];
int n;
void Initial(int n)
{
	for(int i=1;i<=n;i++)
	{
		father[i]=i;
	}
	vec.clear();
}
int find(int x)
{
	if(x!=father[x])
		father[x]=find(father[x]);
	return father[x];
}


bool check()
{
	for(int i=1;i<=n;i++)
	{
		int u=find(i);
		if(g[u]>s[u])
		return false;
	}
	return true;
}
int main()
{
	int m;
	while(scanf("%d",&n)!=EOF)
	{
		if(!n) break;
		int sum1=0,sum2=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&g[i]);
			sum1+=g[i];
		}
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&s[i]);
			sum2+=s[i];
		}
		if(sum1>sum2)
		{
			printf("No Solution\n");
			continue;
		}
		Initial(n);	
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			int x,y,d;
			scanf("%d%d%d",&x,&y,&d);
			vec.push_back(Edge(x,y,d));
		}
		
		int ans=0;
		if(check()) 
		{
			printf("%d\n",ans);
			continue;
		}
		sort(vec.begin(),vec.end());
		for(int i=0;i<m;i++)
		{
			int u=find(vec[i].x);
			int v=find(vec[i].y);
			if(u!=v)
			{
				father[u]=v;
				g[v]+=g[u];
				g[u]=0;
				s[v]+=s[u];
				s[u]=0;
				ans=vec[i].d;
			}
			if(check()) break;
		}
		if(check()) printf("%d\n", ans);
        else printf("No Solution\n");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值