HDU 4009 Transfer water(最小树形图)

来源:互联网 发布:郑州软件开发工资 编辑:程序博客网 时间:2024/06/06 05:27

Transfer water

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 5373    Accepted Submission(s): 1947


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. 
 

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 301 3 22 4 11 22 1 20 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 
 

题意:
在山里有n个人家,坐标为(a,b,c)。每个家庭有两种选择,一种自己打井,一种连个水道到有水的人家。
打井花费为c*x。设两家的曼哈顿距离为m,连水道如果水源家的c比要水的低,那么需要一个水泵(很合理),那么花费为m*y+z。不然则只需要m*y。
求最小让全村通水的花费。

POINT:
0当需根,给每个人家连边,边权值为打井花费,然后在根据题目连边。
跑一个最小树形图就ok。

#include <iostream>#include <cmath>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <string>typedef long long LL;using namespace std;const int maxn = 1111;const int maxm = 1111*1111;const int inf = 0x3f3f3f3f;//0虚根,1-n人家struct edge{    int u,v,w;}len[maxm];int edgenum,pre[maxn],vis[maxn],id[maxn],in[maxn];struct dian{    int x,y,z;}d[maxn];void init(){    edgenum=0;}void add(int u,int v,int w){    int a=++edgenum;    len[a].u=u,len[a].v=v,len[a].w=w;    while(edgenum>=maxm)    {        printf("1");    }        }int zhuliu(int root,int n,int m){    int res=0;    int u,v;    while(1)    {        int cn=0;        for(int i=0;i<=n;i++) in[i]=inf;        for(int i=1;i<=m;i++)        {            u=len[i].u;            v=len[i].v;            if(u!=v&&in[v]>len[i].w)            {                pre[v]=u;                in[v]=len[i].w;            }        }        in[root]=0;        for(int i=0;i<=n;i++) if(in[i]==inf) return -1;        memset(vis,-1,sizeof vis);        memset(id,-1,sizeof id);        for(int i=0;i<=n;i++)        {            res+=in[i];            v=i;            while(vis[v]!=i&&id[v]==-1&&v!=root)            {                vis[v]=i;                v=pre[v];            }            if(id[v]==-1&&v!=root)            {                for(u=pre[v];u!=v;u=pre[u])                {                    id[u]=cn;                }                id[v]=cn;                cn++;            }        }        if(cn==0) break;        for(int i=0;i<=n;i++) if(id[i]==-1) id[i]=cn++;        for(int i=1;i<=m;)        {            v=len[i].v;            len[i].v=id[len[i].v];            len[i].u=id[len[i].u];            if(len[i].v==len[i].u)            {                swap(len[i],len[m--]);            }            else            {                len[i++].w-=in[v];            }        }        n=cn-1;        root=id[root];    }        return res;}int main(){    int n,x,y,z;    while (~scanf("%d %d %d %d",&n,&x,&y,&z))    {        if(n==0&&x==0&&y==0&&z==0) break;        init();        for(int i=1;i<=n;i++)        {            scanf("%d %d %d",&d[i].x,&d[i].y,&d[i].z);        }        for(int i=1;i<=n;i++)        {            int k;scanf("%d",&k);            while(k--)            {                int to;scanf("%d",&to);                if(i==to) continue;                int w=abs(d[to].x-d[i].x)+abs(d[to].y-d[i].y)+abs(d[to].z-d[i].z);                if(d[to].z>d[i].z)                {                    w=w*y+z;                    add(i,to,w);                }                else                {                    w=w*y;                    add(i,to,w);                }            }        }        for(int i=1;i<=n;i++)        {            add(0,i,d[i].z*x);        }        int ans=zhuliu(0,n,edgenum);        if(ans==-1)            printf("poor XiaoA\n");        else printf("%d\n",ans);    }}



阅读全文
0 0