HDU 4009 Transfer water

Transfer water

Time Limit: 3000ms

Memory Limit: 65768KB

This problem will be judged on HDU. Original ID: 4009
64-bit integer IO format: %I64d      Java class name: Main

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

解题:这个虚拟根其实是有用的,因为至少要挖一个井,不然,光有管子有个卵用啊

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1010;
 4 const int INF = 0x3f3f3f3f;
 5 struct arc {
 6     int u,v,w;
 7     arc(int x = 0,int y = 0,int z = 0) {
 8         u = x;
 9         v = y;
10         w = z;
11     }
12 } e[500000];
13 struct Point {
14     int x,y,z;
15 } p[maxn];
16 int pre[maxn],hs[maxn],vis[maxn],in[maxn];
17 int calc(int a,int b) {
18     return abs(p[a].x - p[b].x) + abs(p[a].y - p[b].y) + abs(p[a].z - p[b].z);
19 }
20 int DMST(int root,int n,int m,int ret = 0) {
21     while(true) {
22         for(int i = 0; i < n; ++i) {
23             in[i] = INF;
24             vis[i] = hs[i] = -1;
25         }
26         for(int i = 0; i < m; ++i) {
27             if(e[i].u != e[i].v && e[i].w < in[e[i].v]) {
28                 in[e[i].v] = e[i].w;
29                 pre[e[i].v] = e[i].u;
30             }
31         }
32         for(int i = 0; i < n; ++i)
33             if(i != root && in[i] == INF) return -1;
34         int cnt = in[root] = 0;
35         for(int i = 0; i < n; ++i) {
36             ret += in[i];
37             int v = i;
38             while(vis[v] != i && v != root && hs[v] == -1) {
39                 vis[v] = i;
40                 v = pre[v];
41             }
42             if(v != root && hs[v] == -1) {
43                 for(int u = pre[v]; u != v; u = pre[u]) hs[u] = cnt;
44                 hs[v] = cnt++;
45             }
46         }
47         if(!cnt) break;
48         for(int i = 0; i < n; ++i)
49             if(hs[i] == -1) hs[i] = cnt++;
50         for(int i = 0; i < m; ++i) {
51             int u = e[i].u;
52             int v = e[i].v;
53             e[i].u = hs[u];
54             e[i].v = hs[v];
55             if(e[i].u != e[i].v) e[i].w -= in[v];
56         }
57         n = cnt;
58         root = hs[root];
59     }
60     return ret;
61 }
62 int main() {
63     int n,m,X,Y,Z;
64     while(scanf("%d%d%d%d",&n,&X,&Y,&Z),n||X||Y||Z) {
65         for(int i = 1; i <= n; ++i)
66             scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
67         m = 0;
68         for(int i = 1,k,u; i <= n; ++i) {
69             scanf("%d",&k);
70             while(k--) {
71                 scanf("%d",&u);
72                 int tmp = calc(i,u)*Y;
73                 if(p[i].z < p[u].z) tmp += Z;
74                 e[m++] = arc(i,u,tmp);
75             }
76         }
77         for(int i = 1; i <= n; ++i)
78             e[m++] = arc(0,i,p[i].z*X);
79         printf("%d\n",DMST(0,n + 1,m));
80     }
81     return 0;
82 }

时间: 2024-10-19 14:25:33

HDU 4009 Transfer water的相关文章

hdu 4009 Transfer water(最小树形图:有向图的最小生成树模板)

题目: 链接:点击打开链接 题意: 有n个村庄,要求使得每个村庄都能得到水的最小费用.每个村庄可以通过挖井或从其他村庄修水路获得水.挖井的费用是房子的高度乘以X,修水道的费用和有向图边的起点和终点的高度有关. 思路: 代码: #include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define inf 0x3f3f3f3f

hdu 4009 Transfer water(最小型树图)

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 3995    Accepted Submission(s): 1438 Problem Description XiaoA lives in a village. Last year flood rained the village. So they deci

Hdu 4009 Transfer water【最小树形图】

Transfer water Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 4702 Accepted Submission(s): 1665 Problem Description XiaoA lives in a village. Last year flood rained the village. So they decide to

HDU 4009 Transfer water 最小树形图

分析:建一个远点,往每个点连建井的价值(单向边),其它输水线按照题意建单向边 然后以源点为根的权值最小的有向树就是答案,套最小树形图模板 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <stack> #include <

hdoj 4009 Transfer water 【无源点最小树形图】【好题】

题目:hdoj 4009 Transfer water 题意:题目有点长,说是有个村子,有 n 户人家要用水,他们加的位置用三维坐标来表示(x,y,z),他们有两种选择: 1:自己挖一口井,花费为 z * cost_x 2:从别人家接个水管引过来,化为为距离 * cost_y,如果要引的地方比当前地方低的话,还要买一个水泵,花费cost_z. 距离算法|x2‐x1|+|y2‐y1|+|z2‐z1|. 然后求让所有的人都有用的水的最小花费. 分析:发现它是一个求最小生成树的题目,但是关键点有二 1

HDOJ 4009 Transfer water 最小树形图

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 4216    Accepted Submission(s): 1499 Problem Description XiaoA lives in a village. Last year flood rained the village. So they dec

HDU 4183Pahom on Water(网络流之最大流)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4183 这题题目意思很难看懂..我看了好长时间也没看懂..最终是从网上找的翻译..我就在这翻译一下吧. 意思大约是:有多个点,每个点给出坐标与半径,加入两个点相交,就可以从这两个点走.题目要求先从起点到终点,再从终点回到起点.从起点到终点的过程中,只能从频率小的走到频率大的点(前提是两点相交),从终点到起点的过程中,只能从频率大的走到频率小的.在走的过程中,除了起点与终点,别的只要走过就会消失,就是说

HDU4009 Transfer water【最小树形图】【不定根】

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 3943    Accepted Submission(s): 1415 Problem Description XiaoA lives in a village. Last year flood rained the village. So they dec

HDU 5832 A water problem(某水题)

HDU 5832 A water problem(某水题) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Two planets named Haha and Xixi in the universe and they were created with the universe beginning. There is