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|. 
题意
最小树形图(不定根)
思路
超级点+朱刘算法
代码
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<iostream>
#include<cmath>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1010;
typedef long long ll;
struct Node
{
    int x;
    int y;
    int z;
}point[maxn];
struct node//边的权和顶点
{
    int u;
    int v;
    ll w;
}edge[maxn*maxn];
int pre[maxn],id[maxn],vis[maxn],n,m,pos;
ll in[maxn];//存最小入边权,pre[v]为该边的起点
ll Directed_MST(int root,int V,int E)//朱刘算法求最小树形图(有向图) 
{
    ll res=0;//存最小树形图总权值
    while(1)
    {
        //1.找每个节点的最小入边
        for(int i=0;i<V;i++)
            in[i]=inf;//初始化为无穷大
        for(int i=0;i<E;i++)//遍历每条边
        {
            int u=edge[i].u;
            int v=edge[i].v;
            if(edge[i].w<in[v]&&u!=v)//说明顶点v有条权值较小的入边 ,记录之
            {
                pre[v]=u;//节点u指向v
                in[v]=edge[i].w;//最小入边
                if(u==root)//这个点就是实际的起点
                    pos=i;
            }
        }
        for(int i=0;i<V;i++)//判断是否存在最小树形图
        {
            if(i==root)
                continue;
            if(in[i]==inf)
                return -1;//除了根以外有点没有入边,则根无法到达它  说明它是独立的点 一定不能构成树形图
        }
        //2.找环
        int cnt=0;//记录环数
        memset(id,-1,sizeof(id));
        memset(vis,-1,sizeof(vis));
        in[root]=0;
        for(int i=0;i<V;i++) //标记每个环
        {
            res+=in[i];//记录权值
            int v=i;
            while(vis[v]!=i&&id[v]==-1&&v!=root)
            {
                vis[v]=i;
                v=pre[v];
            }
            if(v!=root&&id[v]==-1)
            {
                for(int u=pre[v];u!=v;u=pre[u])
                    id[u]=cnt;//标记节点u为第几个环
                id[v]=cnt++;
            }
        }
        if(cnt==0)
            break; //无环   则break
        for(int i=0;i<V;i++)
            if(id[i]==-1)
                id[i]=cnt++;
            //3.建立新图   缩点,重新标记
        for(int i=0;i<E;i++)
        {
            int u=edge[i].u;
            int v=edge[i].v;
            edge[i].u=id[u];
            edge[i].v=id[v];
            if(id[u]!=id[v])
                edge[i].w-=in[v];
        }
        V=cnt;
        root=id[root];
    }
    return res;
}
int main()
{
    int x,y,z,k,p,tot=0;
    while(~scanf("%d%d%d%d",&n,&x,&y,&z))
    {
        tot=0;
        if(n==0&&x==0&&y==0&&z==0) break;
        for(int i=1;i<=n;i++)
            scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].z);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&k);
            while(k--)
            {
                scanf("%d",&p);
                if(p==i) continue;
                int tmp=(abs(point[i].x-point[p].x)+abs(point[i].y-point[p].y)+abs(point[i].z-point[p].z))*y; //边权值的计算
                if(point[i].z<point[p].z) tmp+=z;
                edge[tot].u=i,edge[tot].v=p,edge[tot++].w=tmp;
            }
        }
        for(int i=tot;i<tot+n;i++)//增加超级节点0,节点0到其余各个节点的边权相同
        {
            edge[i].u=0;
            edge[i].v=i-tot+1;
            edge[i].w=point[i-tot+1].z*x;
        }
        ll ans=Directed_MST(0,n+1,tot+n);
        printf("%lld\n",ans);
    }
    return 0;
}

 

 
posted @ 2021-03-05 19:42  我找木鱼  阅读(52)  评论(0)    收藏  举报