HDU4009 Transfer water【最小树形图】【不定根】

Transfer water

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 3943    Accepted Submission(s): 1415

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

The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

题目大意:一个村庄被洪水摧毁了,整个村庄都要转移。但是山上没有泉水,每户家庭只能

在自家挖一个水井或是修一个水渠从别的家庭引水。如果要修井,则修井费用和房子所在海

拔高度有关,每米X元。如果从别人的家里引水,如果从高于自己家高度的人家里引水,费

用为每米Y元。如果从低于自己家高度的人家里引水,每条要多花费Z元。现在给你这个村庄

N个家庭房屋的坐标(a,b,c)和三种花费X,Y,Z。接着给你各家之间能单向修建引水沟渠的限制。

问:能使全村庄的人喝上水的总修建费用最低为多少。若不能,则输出"poor XiaoA"。

思路:其实就是给你一个有向图,求有向图的最小树形图是多少。但是根结点不确定。在这

里可以假设一个根结点,也就是虚根,让所有家庭都从虚根引水(其实就是每家都自己修井),

所以问题肯定有解,最后就是朱刘算法(模板)直接求最小树形图。

朱刘算法参考我的另一篇博文:http://blog.csdn.net/lianai911/article/details/42242371

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN = 1010;
const int MAXM = 1000010;

struct Node
{
    int from;
    int to;
    int w;
};
Node Edges[MAXM];

struct Node1
{
    int x;
    int y;
    int z;
};
Node1 Point[MAXN];

int Dist(Node1 a,Node1 b)
{
    return abs(a.x-b.x) + abs(a.y-b.y) + abs(a.z-b.z);
}

int pre[MAXN],vis[MAXN],flag[MAXN],In[MAXN],sum;

int ZhuLiu(int root,int N,int M)
{
    sum = 0;
    while(true)
    {
        for(int i = 0; i < N; ++i)
            In[i] = INT_MAX;
        for(int i = 0; i < M; ++i)
        {
            int u = Edges[i].from;
            int v = Edges[i].to;
            if(Edges[i].w < In[v] && u != v)
            {
                pre[v] = u;
                In[v] = Edges[i].w;
            }
        }

        for(int i = 0; i < N; ++i)
        {
            if(i == root)
                continue;
            if(In[i] == INT_MAX)
                return -1;
        }
        int CntNode = 0;
        memset(flag,-1,sizeof(flag));
        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 && flag[v] == -1 && v != root)
            {
                vis[v] = i;
                v = pre[v];
            }
            if(v != root && flag[v] == -1)
            {
                for(int u = pre[v]; u != v; u = pre[u])
                    flag[u] = CntNode;
                flag[v] = CntNode++;
            }
        }
        if(CntNode == 0)
            break;
        for(int i = 0; i < N; ++i)
        {
            if(flag[i] == -1)
                flag[i] = CntNode++;
        }
        for(int i = 0; i < M; ++i)
        {
            int v = Edges[i].to;
            Edges[i].from = flag[Edges[i].from];
            Edges[i].to = flag[Edges[i].to];
            if(Edges[i].from != Edges[i].to)
                Edges[i].w -= In[v];
        }
        N = CntNode;
        root = flag[root];
    }
    return sum;
}

int main()
{
    int N,M,X,Y,Z,k,d;
    while(~scanf("%d%d%d%d",&N,&X,&Y,&Z) && (N||X||Y||Z))
    {
        for(int i = 0; i < N; ++i)
            scanf("%d%d%d",&Point[i].x,&Point[i].y,&Point[i].z);
        M = 0;
        for(int i = 0; i < N; ++i)
        {
            scanf("%d",&k);
            while(k--)
            {
                scanf("%d",&d);
                d--;
                Edges[M].from = i;
                Edges[M].to = d;
                Edges[M].w = Dist(Point[i],Point[d])*Y;
                if(Point[d].z > Point[i].z)
                    Edges[M].w += Z;
                M++;
            }
        }
        for(int i = 0; i < N; ++i)
        {
            Edges[M].from = N;
            Edges[M].to = i;
            Edges[M++].w = Point[i].z*X;
        }
        printf("%d\n",ZhuLiu(N,N+1,M));
    }

    return 0;
}
时间: 2024-12-28 14:52:47

HDU4009 Transfer water【最小树形图】【不定根】的相关文章

HDU4009 Transfer water —— 最小树形图 + 超级点

题目链接:https://vjudge.net/problem/HDU-4009 Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 5612    Accepted Submission(s): 1997 Problem Description XiaoA lives in a village. Last ye

hdu4009 Transfer water 最小树形图

每一户人家水的来源有两种打井和从别家接水,每户人家都可能向外输送水. 打井和接水两种的付出代价都接边.设一个超级源点,每家每户打井的代价就是从该点(0)到该户人家(1~n)的边的权值.接水有两种可能,从高处接水,那么代价是哈密顿距离与Y的乘积(可以认为就是水管的费用):从低处接水,还要加上付出水泵的钱(水管+水泵的费用).这样就可以建图了.图论,会建图的话问题就解决一半了. 然后,用模版来解题.不过朱刘算法的模版时间复杂度的差异还是蛮大的.我的模版的建图是邻接矩阵,时间复杂度是O(N^3).超时

hdu 4009 Transfer water(最小树形图:有向图的最小生成树模板)

题目: 链接:点击打开链接 题意: 有n个村庄,要求使得每个村庄都能得到水的最小费用.每个村庄可以通过挖井或从其他村庄修水路获得水.挖井的费用是房子的高度乘以X,修水道的费用和有向图边的起点和终点的高度有关. 思路: 代码: #include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define inf 0x3f3f3f3f

HDU 4009 Transfer water 最小树形图

分析:建一个远点,往每个点连建井的价值(单向边),其它输水线按照题意建单向边 然后以源点为根的权值最小的有向树就是答案,套最小树形图模板 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <stack> #include <

HDOJ 4009 Transfer water 最小树形图

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 4216    Accepted Submission(s): 1499 Problem Description XiaoA lives in a village. Last year flood rained the village. So they dec

HDU4009 Transfer water 【最小树形图】

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 3775    Accepted Submission(s): 1356 Problem Description XiaoA lives in a village. Last year flood rained the village. So they dec

hdoj 4009 Transfer water 【无源点最小树形图】【好题】

题目:hdoj 4009 Transfer water 题意:题目有点长,说是有个村子,有 n 户人家要用水,他们加的位置用三维坐标来表示(x,y,z),他们有两种选择: 1:自己挖一口井,花费为 z * cost_x 2:从别人家接个水管引过来,化为为距离 * cost_y,如果要引的地方比当前地方低的话,还要买一个水泵,花费cost_z. 距离算法|x2‐x1|+|y2‐y1|+|z2‐z1|. 然后求让所有的人都有用的水的最小花费. 分析:发现它是一个求最小生成树的题目,但是关键点有二 1

Hdu 4009 Transfer water【最小树形图】

Transfer water Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 4702 Accepted Submission(s): 1665 Problem Description XiaoA lives in a village. Last year flood rained the village. So they decide to

HDU 2121 Ice_cream’s world II (不定根最小树形图)

题目地址:HDU 2121 这题没有给定根.最容易想到的当然是暴力,枚举所有的根,但是TLE是显然的..为了处理不定根的情况,可以虚拟一个根,然后用这个根去跟所有的点连边,权值为其他所有权值的和+1,目的是防止成为最小树形图的一条边.然后跑出最小树形图后,那么这个虚拟根肯定跟一个实际根相连,这时候根就找到了,然后再在最终的总花费中减去虚拟的那条边的权值就可以了. 代码如下: #include <iostream> #include <string.h> #include <m