生成树专题-----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|.

这道题考的就是不定根地求有向图的最小生成树,难点主要是每个点可以独自挖井供水,不需要别家来提供水,设置一个虚点0,连接每一个点,距离设为每家独自挖井的费用,不像前一道不定根的题,虚点只能选择一个点,像现在这道题如果选择多个点,那么相当于perfect地选择了挖井的人家,最后的最小树形图中虚点有几条出边,那么就有几家需要自己挖井

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int MAXN = 1005;

typedef struct Node{
    int u,v,w;
}Node;
Node edge[MAXN * MAXN];
int in[1005],pre[1005],vis[1005],id[1005];
int val[1005][3];

int zhuliu(int root,int n,int m,Node *edge)
{
    int ans = 0;
    while(true){
        for(int i = 0;i < n;++i) in[i] = inf;
        for(int i = 0;i < m;++i){
            int u = edge[i].u,v = edge[i].v,w = edge[i].w;
            if(u != v && w < in[v]){
                in[v] = w;
                pre[v] = u;
            }
        }
        for(int i = 0;i < n;++i){
            if(i != root && in[i] == inf){
                return -1;
            }
        }
        in[root] = 0;
        memset(id,-1,sizeof(id));
        memset(vis,-1,sizeof(vis));
        int tn = 0;
        for(int i = 0;i < n;++i){
            ans += in[i];
            int v = i;
            while(vis[v] != i && id[v] == -1 && v != root){
                vis[v] = i;
                v = pre[v];
            }
            if(id[v] == -1 && v != root){
                for(int u = pre[v];u != v;u = pre[u])
                    id[u] = tn;
                id[v] = tn++;
            }
        }
        if(tn == 0) break;
        for(int i = 0;i < n;++i){
            if(id[i] == -1){
                id[i] = tn++;
            }
        }
        for(int i = 0;i < m;++i){
            int v = edge[i].v;
            edge[i].u = id[edge[i].u];
            edge[i].v = id[edge[i].v];
            if(edge[i].u != edge[i].v){
                edge[i].w -= in[v];
            }
        }
        n = tn;
        root = id[root];
    }
    return ans;
}

int main()
{
    int n,x,y,z;
    while(~scanf("%d %d %d %d",&n,&x,&y,&z)){
        if(x == 0 && y == 0 && z == 0 && n == 0)
            break;
        for(int i = 1;i <= n;++i){
            scanf("%d %d %d",&val[i][0],&val[i][1],&val[i][2]);
        }
        int cnt = 0;
        for(int i = 1;i <= n;++i){
            int k;
            scanf("%d",&k);
            for(int j = 0;j < k;++j){
                int p;
                scanf("%d",&p);
                if(i != p){
                    edge[cnt].u = i;edge[cnt].v = p;
                    int dis = abs(val[i][0] - val[p][0]) + abs(val[i][1] - val[p][1]) + abs(val[i][2] - val[p][2]);
                    if(val[i][2] < val[p][2]){
                        edge[cnt++].w = dis * y + z;
                    }else{
                        edge[cnt++].w = dis * y;
                    }
                }
            }
        }
        for(int i = 1;i <= n;++i){
            edge[cnt].u = 0;
            edge[cnt].v = i;
            edge[cnt++].w = x * val[i][2];
        }
        int ans = zhuliu(0,n + 1,cnt,edge);
        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、付费专栏及课程。

余额充值