HDU 4009 Transfer water(最小树形图)

本文探讨了在没有天然水源的山区村庄中,如何通过最优路径选择来实现全村供水的问题。考虑到每户家庭可以选择自行打井或是从其他家庭引入水源,并考虑了地形高度及家庭间关系等复杂因素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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
 

题意:
在山里有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);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值