hdu 4009 Transfer water(不定根的最小树形图)

17 篇文章 0 订阅

Transfer water

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


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个家庭,每个家庭都有一个空间坐标。要使每个家庭都有水用,有两种选择:
(1)自己建造一口井,费用为家庭的高度*X;
(2)建造一条引水线从别的家庭引水过来,且要根据给出的关系选择别的家庭,若提供水的家庭高度不低于要引水的家庭,则建造引水线费用            为两者的曼哈顿距离*Y,否则,费用为建造引水线费用为两者的曼哈顿距离*Y+Z;
要求使总的费用最小。
思路:求不定根的最小树形图。加入一个虚根,每个家庭挖井的费用可构造为虚根到每个点的虚边的权值,建造引水线的费用可构造为原图中的有向边。最后求出的最小树形图权值即为答案。

AC代码:
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iostream>
#define max2(a,b) ((a) > (b) ? (a) : (b))
#define min2(a,b) ((a) < (b) ? (a) : (b))

using namespace std;
const double INF=10000000;
const int N=1005;
struct point
{
    int x,y,z;
} p[1005];
struct node
{
    int u,v;
    int w;
} edge[1000000];
int in[N];
int pre[N],hash[N],vis[N];
int n,X,Y,Z,cntedge;
int get_dis(point a,point b)
{
    return (abs(a.x-b.x)+abs(a.y-b.y)+abs(a.z-b.z))*Y;
}
void add(int u,int v,int w)
{
    edge[cntedge].u=u;
    edge[cntedge].v=v;
    edge[cntedge].w=w;
    if(u!=0&&p[u].z<p[v].z)
        edge[cntedge].w+=Z;
    cntedge++;
}
int Directed_MST(int root)
{
    int sum=0;
    while(1)
    {
        for(int i=0; i<=n; i++)
            in[i]=INF;
        for(int i=0; i<cntedge; i++) //找最小入边
        {
            int u=edge[i].u;
            int v=edge[i].v;
            if(edge[i].w<in[v]&&u!=v)  //重构的新图可能存在自环
            {
                pre[v]=u;
                in[v]=edge[i].w;
            }
        }
        for(int i=0; i<=n; i++) //判断是否存在最小树形图
        {
            if(i==root) continue;
            if(in[i]==INF) return -1;
        }
        int cntnode=0;
        memset(hash,-1,sizeof(hash));
        memset(vis,-1,sizeof(vis));
        in[root]=0;
        for(int i=0; i<=n; i++)  //检查是否存在环
        {
            sum+=in[i];
            int v=i;
            while(vis[v]!=i&&hash[v]==-1&&v!=root)
            {
                vis[v]=i;
                v=pre[v];
            }
            if(v!=root&&hash[v]==-1)      //若存在环,将环缩点
            {
                for(int u=pre[v]; u!=v; u=pre[u])
                    hash[u]=cntnode;
                hash[v]=cntnode++;
            }
        }
        if(cntnode==0) break;   //若不存在环,算法终止
        for(int i=0; i<=n; i++) //构新图
            if(hash[i]==-1)
                hash[i]=cntnode++;
        for(int i=0; i<cntedge; i++)
        {
            int v=edge[i].v;
            edge[i].u=hash[edge[i].u];
            edge[i].v=hash[edge[i].v];
            if(edge[i].u!=edge[i].v)
                edge[i].w-=in[v];
        }
        n=cntnode-1;
        root=hash[root];
    }
    return sum;
}
int main()
{
    int v,k;
    while(scanf("%d%d%d%d",&n,&X,&Y,&Z)!=EOF)
    {
        if(!n&&!X&&!Y&&!Z) break;
        cntedge=0;
        for(int i=1; i<=n; i++)
            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
        for(int u=1; u<=n; u++)
        {
            scanf("%d",&k);
            while(k--)
            {
                scanf("%d",&v);
                add(u,v,get_dis(p[u],p[v]));
            }
        }
        for(int i=1; i<=n; i++)
            add(0,i,p[i].z*X);
        int ans=Directed_MST(0);
        if(ans==-1)
            printf("poor XiaoA\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值