码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4009 Transfer water(最小树形图)

时间:2015-02-15 16:34:07      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

Transfer water

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3986    Accepted Submission(s): 1434


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

 

有向,即最小树形图。

那么剩下就是考虑怎么去构图的问题了。

以水源为根,给每个点连一条边 , 边权是 Point[i].z * X

然后每个点连上它的合法点 , U->V .. w = 曼哈顿距离 * Y。

如果 Point[u].z < Point[v].z 要加一个 pomp 。。 费用为 Z , w += Z.

然后跑一次朱刘算法即可

 

技术分享
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>

using namespace std ;
typedef long long LL ;
const LL inf = 1e18+7;
const int N = 1010;
const int M = 2000010;

struct edge { int u , v ; LL w; } e[M];
struct node { int x , y , z ; } Point[N];
int n , m , p1 , p2 , p3 ;
int pre[N] , id[N] , vis[N] ; LL in[N] ;

LL zhuliu( int root , int n , int m ) {
    LL res = 0 , u , v ;
    while( true ) {
        for( int i = 0 ; i < n ; ++i ) in[i] = inf ;
        for( int i = 0 ; i < m ; ++i )
            if( e[i].u != e[i].v && e[i].w < in[e[i].v] ) {
                pre[e[i].v] = e[i].u;
                in[e[i].v] = e[i].w;
            }
        for( int i = 0 ; i < n ; ++i )
            if( i != root && in[i] == inf )
                return -1 ;
        int tn = 0 ;
        memset( id , -1 , sizeof id );
        memset( vis , -1 , sizeof vis );
        in[root] = 0 ;
        for( int i = 0 ; i < n ; ++i ) {
            res += in[i];
            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] = tn ;
                id[v] = tn++ ;
            }
        }
        if( tn == 0 ) break ; // no circle
        for( int i = 0 ; i < n ; ++i ) if( id[i] == -1 ) {
            id[i] = tn++ ;
        }
        for( int i = 0 ; i < m ;  ){
            v = e[i].v;
            e[i].u = id[e[i].u];
            e[i].v = id[e[i].v];
            if( e[i].u != e[i].v )
                e[i++].w -= in[v];
            else
                swap( e[i] , e[--m] );
        }
        n = tn ;
        root = id[root];
    }
    return res ;
}

inline LL DIS ( int a , int b ) { return 1LL*abs(Point[a].x-Point[b].x)+abs(Point[a].y-Point[b].y)+abs(Point[a].z-Point[b].z); }

int main ()  {
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif
    while( ~scanf("%d%d%d%d",&n,&p1,&p2,&p3) ) {
        if( !n && !p1 && !p2 && !p3 ) break ;
        int ecnt = 0 ;
        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 ) {
            int k , v ; scanf("%d",&k);
            for( int j = 1 ; j <= k ; ++j ) {
                scanf("%d",&v);
                e[ecnt].u = i , e[ecnt].v = v ;
                e[ecnt].w = DIS(i,v)*p2;
                if( Point[i].z < Point[v].z ) e[ecnt].w += p3 ;
                ecnt++ ;
            }
        }
        for( int i = 1 ; i <= n ; ++i ) {
            e[ecnt].u = 0 , e[ecnt].v = i , e[ecnt].w = (LL)p1 * Point[i].z ;
            ecnt++ ;
        }
        LL ans = zhuliu( 0 , n + 1 , ecnt );
        if( ans == -1 )  puts("poor XiaoA");
        else printf("%lld\n",ans);
    }
}
View Code

 

HDU 4009 Transfer water(最小树形图)

标签:

原文地址:http://www.cnblogs.com/hlmark/p/4293009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!