HDU4009 Transfer water —— 最小树形图 + 超级点
Posted h_z_cong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU4009 Transfer water —— 最小树形图 + 超级点相关的知识,希望对你有一定的参考价值。
题目链接:https://vjudge.net/problem/HDU-4009
Transfer water
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 5612 Accepted Submission(s): 1997
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.
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
代码一:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 typedef long long LL; 8 const double EPS = 1e-6; 9 const int INF = 2e9; 10 const LL LNF = 9e18; 11 const int MOD = 1e9+7; 12 const int MAXM = 1e6+10; 13 const int MAXN = 1e3+10; 14 15 struct Edge 16 { 17 int u, v, w; 18 }edge[MAXM]; 19 20 int x[MAXN], y[MAXN], z[MAXN]; 21 int pre[MAXN], id[MAXN], vis[MAXN], in[MAXN]; 22 23 LL zhuliu(int root, int n, int m) 24 { 25 LL res = 0; 26 while(true) 27 { 28 for(int i = 0; i<n; i++) 29 in[i] = INF; 30 for(int i = 0; i<m; i++) 31 if(edge[i].u!=edge[i].v && edge[i].w<in[edge[i].v]) 32 { 33 pre[edge[i].v] = edge[i].u; 34 in[edge[i].v] = edge[i].w; 35 } 36 37 for(int i = 0; i<n; i++) 38 if(i!=root && in[i]==INF) 39 return -1; 40 41 int tn = 0; 42 memset(id, -1, sizeof(id)); 43 memset(vis, -1, sizeof(vis)); 44 in[root] = 0; 45 for(int i = 0; i<n; i++) 46 { 47 res += in[i]; 48 int v = i; 49 while(vis[v]!=i && id[v]==-1 && v!=root) 50 { 51 vis[v] = i; 52 v = pre[v]; 53 } 54 if(v!=root && id[v]==-1) 55 { 56 for(int u = pre[v]; u!=v; u = pre[u]) 57 id[u] = tn; 58 id[v] = tn++; 59 } 60 } 61 if(tn==0) break; 62 for(int i = 0; i<n; i++) 63 if(id[i]==-1) 64 id[i] = tn++; 65 66 for(int i = 0; i<m; ) 67 { 68 int v = edge[i].v; 69 edge[i].u = id[edge[i].u]; 70 edge[i].v = id[edge[i].v]; 71 if(edge[i].u!=edge[i].v) 72 edge[i++].w -= in[v]; 73 else 74 swap(edge[i], edge[--m]); 75 } 76 n = tn; 77 root = id[root]; 78 } 79 return res; 80 } 81 82 int main() 83 { 84 int n, m, X, Y, Z; 85 while(scanf("%d%d%d%d", &n, &X, &Y, &Z)!=EOF) 86 { 87 if(!n && !X && !Y && !Z) break; 88 89 for(int i = 0; i<n; i++) 90 scanf("%d%d%d", &x[i], &y[i], &z[i]); 91 92 m = 0; 93 for(int i = 0; i<n; i++) 94 { 95 int num, v; 96 scanf("%d", &num); 97 while(num--) 98 { 99 scanf("%d", &v); v--; 100 edge[m].u = i; 101 edge[m].v = v; 102 edge[m].w = Y*(abs(x[i]-x[v])+abs(y[i]-y[v])+abs(z[i]-z[v])); 103 if(z[i]<z[v]) edge[m].w += Z; 104 m++; 105 } 106 107 edge[m].u = n; 108 edge[m].v = i; 109 edge[m++].w = z[i]*X; 110 } 111 112 LL ans = zhuliu(n, n+1, m); 113 printf("%I64d\n", ans); 114 } 115 }
代码二:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 typedef long long LL; 8 const double EPS = 1e-6; 9 const int INF = 2e9; 10 const LL LNF = 9e18; 11 const int MOD = 1e9+7; 12 const int MAXM = 1e6+10; 13 const int MAXN = 1e3+10; 14 15 struct Edge 16 { 17 int u, v, w; 18 }edge[MAXM]; 19 20 int x[MAXN], y[MAXN], z[MAXN]; 21 int pre[MAXN], id[MAXN], vis[MAXN], in[MAXN]; 22 23 LL zhuliu(int root, int n, int m) 24 { 25 LL res = 0; 26 while(true) 27 { 28 for(int i = 0; i<n; i++) 29 in[i] = INF; 30 for(int i = 0; i<m; i++) 31 if(edge[i].u!=edge[i].v && edge[i].w<in[edge[i].v]) 32 { 33 pre[edge[i].v] = edge[i].u; 34 in[edge[i].v] = edge[i].w; 35 } 36 37 for(int i = 0; i<n; i++) 38 if(i!=root && in[i]==INF) 39 return -1; 40 41 int tn = 0; 42 memset(id, -1, sizeof(id)); 43 memset(vis, -1, sizeof(vis)); 44 in[root] = 0; 45 for(int i = 0; i<n; i++) 46 { 47 res += in[i]; 48 int v = i; 49 while(vis[v]!=i && id[v]==-1 && v!=root) 50 { 51 vis[v] = i; 52 v = pre[v]; 53 } 54 if(v!=root && id[v]==-1) 55 { 56 for(int u = pre[v]; u!=v; u = pre[u]) 57 id[u] = tn; 58 id[v] = tn++; 59 } 60 } 61 if(tn==0) break; 62 for(int i = 0; i<n; i++) 63 if(id[i]==-1) 64 id[i] = tn++; 65 66 for(int i = 0; i<m; i++) 67 { 68 int v = edge[i].v; 69 edge[i].u = id[edge[i].u]; 70 edge[i].v = id[edge[i].v]; 71 if(edge[i].u!=edge[i].v) 72 edge[i].w -= in[v]; 73 } 74 n = tn; 75 root = id[root]; 76 } 77 return res; 78 } 79 80 int main() 81 { 82 int n, m, X, Y, Z; 83 while(scanf("%d%d%d%d", &n, &X, &Y, &Z)!=EOF) 84 { 85 if(!n && !X && !Y && !Z) break; 86 87 for(int i = 0; i<n; i++) 88 scanf("%d%d%d", &x[i], &y[i], &z[i]); 89 90 m = 0; 91 for(int i = 0; i<n; i++) 92 { 93 int num, v; 94 scanf("%d", &num); 95 while(num--) 96 { 97 scanf("%d", &v); v--; 98 edge[m].u = i; 99 edge[m].v = v; 100 edge[m].w = Y*(abs(x[i]-x[v])+abs(y[i]-y[v])+abs(z[i]-z[v])); 101 if(z[i]<z[v]) edge[m].w += Z; 102 m++; 103 } 104 105 edge[m].u = n; 106 edge[m].v = i; 107 edge[m++].w = z[i]*X; 108 } 109 110 LL ans = zhuliu(n, n+1, m); 111 printf("%I64d\n", ans); 112 } 113 }
以上是关于HDU4009 Transfer water —— 最小树形图 + 超级点的主要内容,如果未能解决你的问题,请参考以下文章
HDU4009 Transfer water —— 最小树形图 + 超级点