HDU - 4009 Transfer water (最小树形图)

Transfer water

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 5902    Accepted Submission(s): 2098


 

Problem Description

XiaoA lives in a village. Last year flood rained the village. So they decide to move the whole village to the mountain nearby this year. There is no spring in the mountain, so each household could only dig a well or build a water line from other household. If the household decide to dig a well, the money for the well is the height of their house multiplies X dollar per meter. If the household decide to build a water line from other household, and if the height of which supply water is not lower than the one which get water, the money of one water line is the Manhattan distance of the two households multiplies Y dollar per meter. Or if the height of which supply water is lower than the one which get water, a water pump is needed except the water line. Z dollar should be paid for one water pump. In addition,therelation of the households must be considered. Some households may do not allow some other households build a water line from there house. Now given the 3‐dimensional position (a, b, c) of every household the c of which means height, can you calculate the minimal money the whole village need so that every household has water, or tell the leader if it can’t be done.

 

Input

Multiple cases.
First line of each case contains 4 integers n (1<=n<=1000), the number of the households, X (1<=X<=1000), Y (1<=Y<=1000), Z (1<=Z<=1000).
Each of the next n lines contains 3 integers a, b, c means the position of the i‐th households, none of them will exceeded 1000.
Then next n lines describe the relation between the households. The n+i+1‐th line describes the relation of the i‐th household. The line will begin with an integer k, and the next k integers are the household numbers that can build a water line from the i‐th household.
If n=X=Y=Z=0, the input ends, and no output for that.

 

Output

One integer in one line for each case, the minimal money the whole village need so that every household has water. If the plan does not exist, print “poor XiaoA” in one line.

 

Sample Input

 

2 10 20 30 1 3 2 2 4 1 1 2 2 1 2 0 0 0 0

 

Sample Output

 
30

Hint

In 3‐dimensional space Manhattan distance of point A (x1, y1, z1) and B(x2, y2, z2) is |x2‐x1|+|y2‐y1|+|z2‐z1|.  

Source

The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

 

Recommend

lcy

      山上有一些村民, 每家可以自己挖井取水, 有一个花费(和高度有关), 也可以选择从比他们高的村民家中引水, 建造管道也有一个花费, 那么问最少的花费是多少. 

      这道题的话,其实是一个最小树形图的裸题,不过建图的时候需要思考一下,把水源建为0,分别与各个家建立边(就是打井这步操作)就好。然后以0为root跑一边最小树形图,不过要注意的地方是即使两个家的高度一样,在建水管的时候也是需要z这个额外费用的~

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
struct fuck
{
	int from, to, len;
}ed[1500000];
struct fucker
{
	int x, y, z;
}spot[1005];
int pre[1005];
int in[1005];
int vis[1005];
int id[1005];
int zhuliu(int root, int n, int m)
{
	int res = 0;
	while (1){
		for (int s = 0; s < n; s++)
			in[s] = inf;
		for (int s = 0; s < m; s++)
			if (ed[s].from != ed[s].to&&in[ed[s].to] > ed[s].len){
				in[ed[s].to] = ed[s].len;
				pre[ed[s].to] = ed[s].from;
			}
		for (int s = 0; s < n; s++) 
			if (in[s] == inf&&s != root)
				return -1;
		int sum_huan = 0;
		in[root] = 0;
		memset(id, -1, sizeof(id));
		memset(vis, -1, sizeof(vis));
		for (int s = 0; s < n; s++) {
			res += in[s];
			int v = s;
			while(vis[v]!=s&&id[v]==-1&&v!=root){
				vis[v] = s;
				v = pre[v];
			}
			if (v != root&&id[v] == -1){
				for (int w = pre[v]; w != v; w = pre[w]) 
					id[w] = sum_huan;
				id[v] = sum_huan++;
			}
		}
		if (sum_huan == 0) {
			break;
		}
		for (int s = 0; s < n; s++) {
			if (id[s] == -1) {
				id[s] = sum_huan++;
			}
		}
		for (int s = 0; s < m; s++) {
			int v = ed[s].to;
			ed[s].from = id[ed[s].from];
			ed[s].to = id[ed[s].to];
			if (ed[s].from != ed[s].to)
				ed[s].len -= in[v];
		}
		n = sum_huan;
		root = id[root];
	}
	return res;
}
int main()
{
	int n, x, y, z;
	while (~scanf("%d%d%d%d", &n, &x, &y, &z) && n && x && y && z)
	{
		int cnt = 0, sum, t;
		for (int s = 1; s <= n; s++)
		{
			scanf("%d%d%d", &spot[s].x, &spot[s].y, &spot[s].z);
		}
		for (int s = 1; s <= n; s++)
		{
			scanf("%d", &sum);
			while (sum--)
			{
				scanf("%d", &t);
				ed[cnt].from = s;
				ed[cnt].to = t;
				int len = abs(spot[s].x - spot[t].x) + abs(spot[s].y - spot[t].y) + abs(spot[s].z - spot[t].z);
				ed[cnt++].len = spot[s].z >= spot[t].z ? (len)*y : (len)*y + z;
			}
		}
		for (int s = 1; s <= n; s++)
		{
			ed[cnt].from = 0;
			ed[cnt].to = s;
			ed[cnt++].len = x*spot[s].z;
		}
		int ans = zhuliu(0, n + 1, cnt);
		cout << ans << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/chenshibo17/article/details/81287030
今日推荐