HDU 4009 Transfer Water (smallest tree chart)

Source: Internet
Author: User

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 Descriptionxiaoa 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 the their house multiplies X dollar per meter. If the household decide to build a water line from other household, and if the height of the which supply water are not lower t Han the one which get water, the money of one water line is the Manhattan distance of the and 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 does 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 vil Lage need so every household have water, or tell the leader if it can ' t be done. 

Inputmultiple cases.
First line of all 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 is 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.

Outputone integer in one line for each case, the minimal money the whole village need so that every household had water. If The plan does not exist, print "poor Xiaoa" in one line.

Sample Input2 10 20 301 3 22 4 11 22 1 20 0 0 0

Sample Output30 HintIn 3‐dimensional space Manhattan distance of point A (x1, Y1, Z1) and B (x2, y2, Z2) are |x2‐x1|+|y2‐y1|+|z2‐z1|.

Sourcethe 36th ACM/ICPC Asia Regional Dalian site--online Contest

------------encountered an algorithm invented by the Chinese (algorithm)--------

Let's start with a simple analysis of the following sections! Look at the picture

----------ac~~~~~~~~~~~~~--------------~~~~~~~~~~~~~~~ Magic Board

Code:

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#defineMAXN 1010#definetype intConst intINF = ~0u>>1;structnode{intu,v;    Type cost; Node () {} node (int_u,int_v,type _c): U (_u), V (_v), cost (_c) {}}E[MAXN*MAXN];intPre[maxn],id[maxn],vis[maxn];typeinch[Maxn];type dirmst (intRootintNvintNE) {type RET=0;  while(1)    {        //Find the smallest in-arcFillinch,inch+Nv,inf);  for(inti =0; I < ne;i++)        {            intU =e[i].u; intv =e[i].v; if(E[i].cost <inch[v] && u! =v) {Pre[v]=u; inch[V] =E[i].cost; }        }         for(inti =0; I < nv;i++)        {            if(i = =root)Continue; if(inch[I] = =inf)return-1;//There is some nodes other than root with no in-arc connected to it        }        //Find the dir circle        intCntnode =0; Fill (Id,id+ nv,-1); Fill (Vis,vis+ nv,-1); inch[Root] =0;  for(inti =0; I < nv;i++) {ret+=inch[i]; intv =i;  while(Vis[v]! = i && id[v] = =-1&& V! =root) {Vis[v]=i; V=Pre[v]; }            if(v! = root && id[v] = =-1)            {                 for(intU = pre[v]; U! = V;u =Pre[u]) Id[u]=Cntnode; ID[V]= cntnode++; }        }        if(Cntnode = =0)             Break;//No Circle         for(inti =0; I < nv;i++)            if(Id[i] = =-1) Id[i]= cntnode++; //Compress the Nodes         for(inti =0; I < ne;i++)        {            intv =e[i].v; E[I].U=ID[E[I].U]; E[I].V=ID[E[I].V]; if(E[i].u! =e[i].v) E[i].cost-=inch[v]; } NV=Cntnode; Root=Id[root]; }    returnret;}intn,tot,x,y,z;intAbintx) {    returnX >=0? x:-x;}structpoint{intx, Y, Z Point () {}, point (intAintBintc): X (a), Y (b), Z (c) {} pointoperator- (ConstPoint p) {        returnPoint (X-p.x,y-p.y,z-p.z); }    intdis () {returnAB (x) + AB (y) +AB (z); }}P[MAXN];intMain () { while(SCANF ("%d %d%d%d", &n,&x,&y,&z) = =4&& (n | | X | | Y | |Z)) {tot=0;  for(inti =1; I <= n;i++)        {            intA,b,c; scanf (" %d%d%d",&a,&b,&c); P[i]=Point (A,B,C); E[tot+ +] = node (0, I,ab (P[I].Z) *X); }         for(inti =1; I <= n;i++)        {            intopt; scanf ("%d",&opt);  for(intj =0; J < opt;j++)            {                intA; scanf ("%d",&a); if(A = =i)Continue; inttemp = Y * (P[i]-p[a]). DIS (); if(P[i].z <p[a].z) Temp+=Z; E[tot++] =node (i,a,temp); }        }        intAns = dirmst (0, n +1, tot); if(ans = =-1) puts ("Poor Xiaoa"); Elseprintf ("%d\n", ans); }}
View Code

HDU 4009 Transfer Water (smallest tree chart)

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.