hdu4009---Transfer water

62 篇文章 0 订阅

Transfer water

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


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
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   4007  4003  4004  4005  4006 
 

最小树形图,其实也很简单,按题意村庄和村庄之间建边,然后设置一个共同的虚拟根,从这个根向村庄建边,表示这个村庄打井,代价就是打井的代价,然后跑一遍朱刘算法就行了,而且本题一定有解

/*************************************************************************
    > File Name: hdu4009.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2015年01月26日 星期一 15时49分37秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int inf = 0x3f3f3f3f;
const int N = 1200;

struct CITY
{
	int x, y, z;
}city[N];

struct node
{
	int u, v, w;
}edge[N * N + 10];

int pre[N], in[N], vis[N], id[N];

int zhuliu (int root, int n, int m)
{
	int res = 0, u, v;
	while (1)
	{
		memset (in, inf, sizeof(in));
		for (int i = 0; i < m; ++i)
		{
			if (edge[i].u != edge[i].v && edge[i].w < in[edge[i].v]) //忽略自环,寻找当前图每个点最短入边
			{
				in[edge[i].v] = edge[i].w;
				pre[edge[i].v] = edge[i].u;
			}
		}
		for (int i = 0; i < n; ++i)
		{
			if (i != root && in[i] == inf)
			{
				return -1; //某点不可达(非root)
			}
		}
		int tn = 0;
		memset (id, -1, sizeof(id));
		memset (vis, -1, sizeof(vis));
		in[root] = 0;
		for (int i = 0; i < n; ++i)
		{
			res += in[i];
			v = i;
			while (vis[v] != i && id[v] == -1 && v != root) //找有向环
			{
				vis[v] = i;
				v = pre[v];
			}
			if (v != root && id[v] == -1) //最后不回到根的话,一定在环里
			{
				for (int u = pre[v]; u != v; u = pre[u])
				{
					id[u] = tn;
				}
				id[v] = tn++;
			}
		}
		if (tn == 0) //没有有向环,则已经找到最小树形图
		{
			break;
		}
		for (int i = 0; i < n; ++i)
		{
			if (id[i] == -1)
			{
				id[i] = tn++;
			}
		}
		for (int i = 0; i < m; ++i) //重新构图
		{
			int v = edge[i].v;
			edge[i].u = id[edge[i].u];
			edge[i].v = id[edge[i].v];
			if (edge[i].u != edge[i].v)
			{
				edge[i].w -= in[v]; //权值变为增量(“破环付出的代价”)
			}
		}
		n = tn;
		root = id[root];
	}
	return res;
}

int dist (int i, int j)
{
	return abs (city[i].x - city[j].x) + abs(city[i].y - city[j].y) + abs(city[i].z - city[j].z);
}

int main ()
{
	int n, x, y, z, num, cnt;
	while (~scanf("%d%d%d%d", &n, &x, &y, &z))
	{
		if (n == 0 && x == 0 && y == 0 && z == 0)
		{
			break;
		}
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d%d%d", &city[i].x, &city[i].y, &city[i].z);
		}
		num = cnt = 0;
		int j;
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &num);
			while (num--)
			{
				scanf("%d", &j);
				edge[cnt].u = i;
				edge[cnt].v = j;
				edge[cnt].w = dist (i, j) * y;
				if (city[i].z < city[j].z)
				{
					edge[cnt].w += z;
				}
				++cnt;
			}
		}
		for (int i = 1; i <= n; ++i)
		{
			edge[cnt].u = 0;
			edge[cnt].v = i;
			edge[cnt++].w = city[i].z * x;
		}
		int ans = zhuliu (0, n + 1, cnt);
		if (ans == -1)
		{
			printf("poor XiaoA\n");
		}
		else
		{
			printf("%d\n", ans);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值