1. 程式人生 > >HDU4009 Transfer water 【最小樹形圖】

HDU4009 Transfer water 【最小樹形圖】

mount reg fin multiple inpu add plan printf pro

Transfer water

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

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

HintIn  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 題意:給定n個點的三維坐標,以及根節點到每一個點的單向權值。再給定n個節點間相互單向連接的成本,求最小樹形圖。

題解:水源能夠看作從虛擬根節點引出來的。這道題必然有解。由於大不了每一個實際點都跟根節點相連嘛,所以ZL_MST函數裏的推斷非根無入邊節點能夠忽略掉。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#define maxn 1002
#define maxm 1000002

int X, Y, Z;
struct Node{
	int x, y, z;
} ver[maxn];
struct Node2{
	int u, v, cost;
} E[maxm];
int in[maxn], hash[maxn], vis[maxn], pre[maxn];

int calDist(Node a, Node b){
	return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z);
}

__int64 ZL_MST(int root, int nv, int ne)
{
	__int64 ans = 0;
	int u, v, i, cnt;
	while(true){
		//0.初始化
		for(i = 0; i < nv; ++i) in[i] = INT_MAX;
		//1.找最小入邊集
		for(i = 0; i < ne; ++i){
			u = E[i].u; v = E[i].v;
			if(E[i].cost < in[v] && u != v){
				in[v] = E[i].cost; pre[v] = u;
			}
		}
		//2.找非根無入邊點(略)。由於必然有解
		//3.找環。加權,又一次標號
		memset(hash, -1, sizeof(hash));
		memset(vis, -1, sizeof(vis));
		cnt = in[root] = 0;
		for(i = 0; i < nv; ++i){
			ans += in[i]; v = i;
			while(vis[v] != i && v != root && hash[v] == -1){
				vis[v] = i; v = pre[v];
			}
			if(v != root && hash[v] == -1){
				for(u = pre[v]; u != v; u = pre[u])
					hash[u] = cnt;
				hash[v] = cnt++;
			}
		}
		if(cnt == 0) return ans; //無環,算法完畢
		for(i = 0; i < nv; ++i) 
			if(hash[i] == -1) hash[i] = cnt++;
		//4.縮點,遍歷每一條邊,又一次構圖
		for(i = 0; i < ne; ++i){
			v = E[i].v;
			E[i].u = hash[E[i].u];
			E[i].v = hash[E[i].v];
			if(E[i].u != E[i].v) E[i].cost -= in[v];
		}
		//頂點數降低
		nv = cnt; root = hash[root];
	}
	return ans;
}

int main()
{
	int n, i, a, b, id;
	while(scanf("%d%d%d%d", &n, &X, &Y, &Z) != EOF && (n||X||Y||Z)){
		for(i = 0; i < n; ++i)
			scanf("%d%d%d", &ver[i].x, &ver[i].y, &ver[i].z);
		for(i = id = 0; i < n; ++i){
			scanf("%d", &a);
			while(a--){
				scanf("%d", &b);
				E[id].cost = calDist(ver[i], ver[--b]) * Y;
				if(ver[b].z > ver[i].z) E[id].cost += Z;
				E[id].u = i; E[id++].v = b;
			}
		}
		for(i = 0; i < n; ++i){
			E[id].u = n; E[id].v = i;
			E[id++].cost = ver[i].z * X;
		}
		printf("%I64d\n", ZL_MST(n, n + 1, id));
	}
	return 0;
}


HDU4009 Transfer water 【最小樹形圖】