HDU 4009 Transfer water

题目链接:https://vjudge.net/problem/HDU-4009

Transfer water
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|.

题意:有n个地方需要供水,每个地方都可以选择是自己挖井,还是从别的地方引水,根据方法不同和每个地方的坐标不同,花费也不同,现在给出每个地方的坐标,花费的计算方法,以及每个地方可以给哪些地方供水(即对方可以从这里引水),求给所有地方供水的最小花费。

思路:显然对于每个地方,只有一种供水方式就足够了,这样也能保证花费最小,而每个地方都可以自己挖井,所以是不可能出现无解的情况的,为了方便思考,我们引入一个虚拟点,把所有自己挖井的都连到这个点,边权为挖井的花费,而如果i能从j处引水,则从j向i连边,边权为引水的花费,然后对这个有向图,以虚拟点为根,求最小树形图即可(最小树形图即为有向图的最小生成树)。

这个思路是看的别人写的,感觉超详细的,本来对这个问题还不知道如何下手,一看完上面这段就全明白了
参考博客:http://blog.csdn.net/yamiiyamii/article/details/7858115

我的代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
#define MAX 1005
#define INF 0x3f3f3f3f
struct Edge
{
    int u,v;
    int w;
} edge[MAX*MAX];
int pos;
int vis[MAX],id[MAX];
int minIn[MAX];
int pre[MAX];
int a,b,c;
int x[1005],y[1005],z[1005];
int Directed_MST(int root,int V,int E)
{
    int ans=0;
    int u,v;
    while(1)
    {
        for(int i=0; i<=V; i++)
            minIn[i]=INF;///minIn存放i节点的最小入边权
        for(int i=0; i<E; i++)
        {///找最小入边权
            u=edge[i].u;
            v=edge[i].v;
            if(minIn[v]>edge[i].w&&u!=v)
            {
                minIn[v]=edge[i].w;
                pre[v]=u;
                if(u==root)
                    pos=i;
            }
        }
        for(int i=0; i<V; i++)
        {///如果存在root以外的孤立点,则不存在最小树形图
            if(i==root)continue;
            if(minIn[i]==INF) return -1;
        }
        memset(vis,-1,sizeof(vis));
        memset(id,-1,sizeof(id));///id[i]存放节点i的新编号
        int cnt=0;
        minIn[root]=0;
        for(int i=0; i<V; i++)
        {
            ans+=minIn[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]=cnt;
                id[v]=cnt++;
            }
        }
        if(cnt==0) break;
        for(int i=0; i<V; i++)
            if(id[i]==-1)
            id[i]=cnt++;///标记孤立点
        for(int i=0; i<E; i++)
        {
            u=edge[i].u;
            v=edge[i].v;
            edge[i].u=id[u];
            edge[i].v=id[v];
            if(id[u]!=id[v])
            {///不在一个环中
                edge[i].w-=minIn[v];
            }
        }
        V=cnt;
        root=id[root];
    }
    return ans;
}
int main()
{
    int N;
    int ans;
    while(scanf("%d %d %d %d",&N,&a,&b,&c)!=EOF)
    {
        if(a+b+c+N==0)
            break;
        int sum=0;
        int i,cnt=0;
        for(i=1; i<=N; i++)
        {
            scanf("%d%d%d",&x[i],&y[i],&z[i]);
            edge[cnt].u=0;///虚拟一个起始点,让所有打井的地方连到这个点,边的权值为打水井的花费
            edge[cnt].v=i;
            edge[cnt++].w=z[i]*a;
        }
        for(int i=1;i<=N;i++)
        {
            int k,d;
            scanf("%d",&k);
            while(k--)
            {
                scanf("%d",&d);
                if(z[d]<=z[i])
                {//而如果d能从j处引水,则从j向d连边,边权为引水的花费
                    edge[cnt].u=i;
                    edge[cnt].v=d;
                    edge[cnt++].w=(abs(x[i]-x[d])+abs(y[i]-y[d])+abs(z[i]-z[d]))*b;
                }
                else
                {
                    edge[cnt].u=i;
                    edge[cnt].v=d;
                    edge[cnt++].w=(abs(x[i]-x[d])+abs(y[i]-y[d])+abs(z[i]-z[d]))*b+c;
                }
            }
        }
        ans=Directed_MST(0,N+1,cnt);
        if(ans==-1)
            printf("poor XiaoA\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值