HDU 4009 Transfer Water: Min tree

Source: Internet
Author: User
Tags abs int size integer min time limit

Link:
http://acm.hdu.edu.cn/showproblem.php?pid=4009

Topic:

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 of the whole village to the mountain of this year. There is no spring in the mountain and so each household could only dig a A-a-a-a-a or build a-water line from the other household. If the household decide to dig a so, the money for the "the" the "the" is the "the" of the "their house multiplies X dollar per meter. If the household decide to builds a water line from other household, and if the height of which supply water are not lower t Han the one which get water and the money of a water line is the Manhattan distance of the two households multiplies Y doll Ar per meter. Or if the height of which supply water is lower than the one which get water, a water pump are needed except the water line . Z dollar should is paid for one water pump. In addition,therelation of the households must is considered. Some households may don't allow Some other households builds 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 Every household has water, or tell the leader if it can ' t is done.

Input
Multiple cases.
The "a" 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 would 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 would begin with an integer k, and the next k integers are the household numbers which 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 of the minimal whole village need so, every household has. If The plan does isn't 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 spaces 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 smallest tree problem, but this question has a more self loop, that is, it can be the I point and he itself I can also connect and have a weight value.
The key to this question is to solve the problem.
One way to do this is to set up a virtual node new, to turn all the self-loop i into edges (new, I).
After that, we take the new as the root node and calculate the minimum tree.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Code:

#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&LT;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 is int n;         The number of nodes int size;      Number of sides int PRE[VN];   
    The precursor of the smallest weight value int id[vn];     int VIS[VN];  
is in the ring or outside the ring Type IN[VN];  
    
};  
directed_mst<int>g;  
int X[VN],Y[VN],Z[VN];  
    
int x,y,z;  
    inline int (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%d%d%d", &n,&x,&y,&z) &&x+y+z) {g.init (n+1);  
            for (int i=1; i<=n; ++i) {scanf ("%d%d%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.