poj 3228 Gold Transportation 并查集

题意:

有n和城市和m条路,每个城市都有产生金量和收集金量,现在要把所有黄金收集,求经过的最短边是多少。

分析:

二分+最大流或用并查集合并等价类。

//poj 3228
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
const int maxN=256;
const int maxM=10024;
int p[maxN],sum[maxN];
struct Edge
{
	int u,v,w;
}e[maxM];

int cmp(Edge a,Edge b)
{
	return a.w<b.w;
}

int find(int u)
{
	if(p[u]==u)
		return u;
	int x=find(p[u]);
	sum[x]+=sum[u];
	sum[u]=0;
	return p[u]=x;;
}

int main()
{
	int n,m,x;
	while(scanf("%d",&n)==1&&n){
		for(int i=1;i<=n;++i){
			sum[i]=0;
			p[i]=i;
		}
		for(int i=1;i<=n;++i){
			scanf("%d",&x);
			sum[i]-=x;
		}
		for(int i=1;i<=n;++i){
			scanf("%d",&x);
			sum[i]+=x;
		}
		scanf("%d",&m);
		for(int i=0;i<m;++i)
			scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
		sort(e,e+m,cmp);
		int ok=0,ans=-1;
		for(int i=0;i<m;++i){
			int u=e[i].u;
			int v=e[i].v;
			int pu=find(u);
			int pv=find(v);
			if(pu!=pv){
				p[pu]=pv;
				sum[pv]+=sum[pu];
				sum[pu]=0;
			}
			int ok=1;
			for(int j=1;j<=n;++j)
				if(p[j]==j&&sum[j]<0){
					ok=0;
					break;
				}
			if(ok==1){
				ans=i;
				break;
			}
		}
		if(ans==-1) puts("No Solution");
		else printf("%d\n",e[ans].w);
	}
	return 0;
}

时间: 2024-08-25 13:23:38

poj 3228 Gold Transportation 并查集的相关文章

POJ 3228 Gold Transportation

Gold Transportation Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 322864-bit integer IO format: %lld      Java class name: Main Recently, a number of gold mines have been discovered in Zorroming State. To p

POJ 3228 -- Gold Transportation【二分 &amp;&amp; 最大流】

Gold Transportation Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3079   Accepted: 1101 Description Recently, a number of gold mines have been discovered in Zorroming State. To protect this treasure, we must transport this gold to the

POJ 3228 网络流+二分&amp;并查集

点击打开链接 题意:有n个城镇,第一行是金矿和金子数量,然后第二行是装金子的地方和能装的数量,在下面是m条道路,问你选择的道路中最大值最小,使得所有金子运到装金子的地方 思路:最大值最小,根本不用考虑一看就是二分,然后想了想就是个网络流的模型嘛,很简单,被坑了几次道路是双向的,改过之后A掉,然后看了看讨论还可以用并查集写,这里两种方法都写了,先是网络流的直接二分最大值,然后满足条件的边建模型,跑一边就行了 #include <queue> #include <vector> #in

poj 3228 Gold Transportation 二分+最大流

http://poj.org/problem?id=3228 题意:有n个城市,每个城市有一定量的金子,我们需要把所有的金子都存到城市中的仓库中,有一些道路连通这些城市,每条道路都有自己的花费,要求的是,把所有的金子都运到仓库中,所走的那些道路,其中花费的最大值最小是多少. 思路,二分答案,把花费比答案小的那些道路建成一个图,再建立一个源点和所有城市相连,流量是每个城市的金子数量,再建立一个汇点,连接所有的城市,流量是每个城市金子的最大存储量.再跑一遍最大流,如果流量等于所有的金子和,那么就可以

POJ 2524 Ubiquitous Religions (幷查集)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23090   Accepted: 11378 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in findi

[ACM] POJ 3295 Ubiquitous Religions (并查集)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23093   Accepted: 11379 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in findi

poj 1456 Supermarket (贪心+并查集)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]

[ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21586   Accepted: 10456 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断

点击打开链接题目链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 30273   Accepted: 8002 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sti