HDU 4009 Transfer water (minimum tree structure)

Source: Internet
Author: User

Question:
Transfer water
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission (s): 2508 Accepted Submission (s): 934


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 cocould 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 into t the water line. Z dollar shoshould 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‐dimen1_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‐dimen1_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
 


Analysis and Summary:

The minimum tree structure problem, but this question has an extra self-ring, that is, the I point can be connected with its own I, and there is a weight value.
The key to this question is to solve this problem.
One method is to set a virtual node new and change all self-ring I to an edge (new, I ).
The new node is used as the root node to calculate the minimum tree structure.

 

Code:
[Cpp]
# Include <cstdio>
# Include <iostream>
# Include <cstring>
# Include <cmath>
Using namespace std;
 
Const int VN = 1005;
Const int INF = 0x7fffffff;
 
Template <typename Type>
Class Directed_MST {
Public:
Void init (int _ n ){
N = _ n + 1; size = 0; ans = 0;
}
Void insert (int u, int v, Type _ w ){
E [size ++]. set (u, v, _ w );
}
Type directed_mst (int root ){
While (true ){
For (int I = 1; I <n; ++ I)
In [I] = INF, id [I] =-1, vis [I] =-1;
For (int I = 0; I <size; ++ I ){
Int u = E [I]. u, v = E [I]. v;
If (E [I]. w <in [v] & u! = V ){
Pre [v] = u;
In [v] = E [I]. w;
}
}
In [root] = 0;
For (int I = 1; I <n; ++ I) if (I! = Root ){
If (in [I] = INF) return-1;
}
Int MXid = 1;
For (int I = 1; I <n; ++ I ){
Ans + = in [I];
Int 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] = MXid;
}
Id [v] = MXid ++;
}
}
If (MXid = 1) break;
For (int I = 1; I <n; ++ I)
If (id [I] =-1) id [I] = MXid ++;
For (int I = 0; I <size; ++ I ){
Int u = E [I]. u, v = E [I]. v;
E [I]. u = id [u];
E [I]. v = id [v];
If (id [u]! = Id [v]) E [I]. w-= in [v];
}
N = MXid;
Root = id [root];
}
Return ans;
}
 
Private:
Struct Edge {
Int u, v;
Type w;
Void set (int _ u, int _ v, Type _ w ){
U = _ u, v = _ v, w = _ w;
}
} E [VN * VN/2];
 
Type ans; // the answer
Int n; // number of nodes
Int size; // Number of Edges
Int pre [VN]; // the front edge with the smallest weight
Int id [VN];
Int vis [VN]; // whether it is in or out of the ring
Type in [VN];
};
 
Directed_MST <int> G;
Int X [VN], Y [VN], Z [VN];
Int x, y, z;
 
Inline int Price (int I, int j ){
If (I = j) return Z [I] * x;
Int mht = abs (X [I]-X [j]) + abs (Y [I]-Y [j]) + abs (Z [I]-Z [j]);
If (Z [I]> = Z [j]) return mht * y;
Return mht * y + z;
}
 
Int main (){
Int n, m, k, u, v, w;
While (~ Scanf ("% d", & n, & x, & y, & z) & x + y + z ){
G. init (n + 1 );
For (int I = 1; I <= n; ++ I ){
Scanf ("% d", & X [I], & Y [I], & Z [I]);
G. insert (n + 1, I, Z [I] * x );
}
For (int u = 1; u <= n; ++ u ){
Scanf ("% d", & k );
For (int j = 1; j <= k; ++ j ){
Scanf ("% d", & v );
If (u = v) continue;
G. insert (u, v, Price (u, v ));
}
}
Int ans = G. directed_mst (n + 1 );
If (ans <0) puts ("poor XiaoA ");
Else printf ("% d \ n", ans );
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.