hdu 4009 Transfer water(最小树形图)

Transfer water

Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 6572 Accepted Submission(s): 2319
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|.

题意

在一个三维空间内,有n个点,坐标为(x,y,z),每个点都需要水,每个点可以向部分点传递水(传递关系由输入确定)。每个点可以选择向下打水,代价为X*z,两者的曼哈顿距离dis=(|x1-x2|+|y1-y2|),或者一个点向另一个传水需要付出dis*Y的代价,如果由低处向比它高的地方传,则需要额外付出Z的代价,求n个点全部通水需要的最少代价。

题解:

因为传递是单向的,所以可以加其看作为一个由n+1个点的有向图,0号点为地下水,求0号点出发,可以到达所有点的最小代价和,建出有向图,求以0为根的最小树形图就是结果。

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctype.h>
#include<cstring>
#include<vector>
#include<deque>
#include<map>
#include<iostream>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-8

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1020;
const int mod = 1000000007;
struct node
{
    int u, v, w;
}eg[maxn*maxn];
struct coor{
    int x,y,z;
}cr[maxn];
int in[maxn], id[maxn], vis[maxn], pre[maxn];
void addeg(int &m, int fr, int to, int w);
LL ZhuLiu(int n, int m, int root);

int main()
{
    int n, m, X, Y, Z, i, j, k;
    while(scanf("%d %d %d %d", &n, &X, &Y, &Z),n||X||Y||Z)
    {
        m = 0;
        for(i=1;i<=n;i++)
            scanf("%d %d %d", &cr[i].x, &cr[i].y, &cr[i].z);
        for(i=1;i<=n;i++)
        {
            scanf("%d", &k);
            //添加从地下打井的边
            addeg(m, 0, i, cr[i].z*X);
            while(k--)
            {
                scanf("%d", &j);
                int dis=abs(cr[i].x-cr[j].x)+abs(cr[i].y-cr[j].y)+abs(cr[i].z-cr[j].z);
                if(cr[i].z < cr[j].z)
                    addeg(m, i, j, dis*Y+Z);
                else
                    addeg(m, i, j, dis*Y);
            }
        }
        LL ans = ZhuLiu(n, m, 0);
        if(ans == -1)
            printf("poor XiaoA\n");
        else
            printf("%lld\n", ans);
    }
    return 0;
}
//添加边
void addeg(int &m, int fr, int to, int w)
{
    eg[m].u = fr, eg[m].v = to;
    eg[m].w = w;
    m++;
}
//朱刘算法求最小树形图
//n:点的数量,m:边的数量,root:根节点(就是求root开始的最小树形图)
LL ZhuLiu(int n, int m, int root)
{
    LL res = 0;
    int i, cnt;
    while(1)
    {
        for(i=0;i<=n;i++)
            in[i] = INF, id[i]=vis[i]=-1;
        //遍历m条边,对每个点求到其最近的那条边(根节点除外)
        for(i=0;i<m;i++)
        {
            int u=eg[i].u, v=eg[i].v;
            if(u!=v && eg[i].w<in[v])
                in[v] = eg[i].w, pre[v] = u;
        }
        //如果有点无法到达,则不存在最小树形图
        for(i=0;i<=n;i++)
            if(i!=root && in[i] == INF)return -1;
        cnt = -1;
        //将当前图里的环缩为一个点
        for(i=0;i<=n;i++)
        {
            if(i == root)continue;
            res += in[i];
            int v = i;
            while(id[v]==-1 && vis[v]!=i && v!=root)
            {
                vis[v] = i;
                v = pre[v];
            }
            if(v!=root && id[v] == -1)
            {
                id[v] = ++cnt;
                for(int u=pre[v];u!=v;u=pre[u])
                    id[u] = cnt;
            }
        }
        //如果没有环,则最小树形图存在,返回
        if(cnt == -1)break;
        //如果存在环,将其余不是环的点重新编号
        for(i=0;i<=n;i++)
        if(id[i] == -1)id[i] = ++cnt;
        //将边的点的编号更新为新的编号,以及将其边的权值减去离其最近的边
        //因为对于到这个点的边来说,最近的那个点权值已经加过了
        for(i=0;i<m;i++)
        {
            int u=eg[i].u, v=eg[i].v;
            if(id[u]!=id[v])
                eg[i].w -= in[v];
            eg[i].u = id[u], eg[i].v = id[v];
        }
        //更新点的数量和根节点的编号
        n = cnt;
        root = id[root];
    }
    return res;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值