zoukankan      html  css  js  c++  java
  • HDU 4009——Transfer water——————【最小树形图、不定根】

    Transfer water
    Time Limit:3000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u

    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表示有n个房子,自建水井需要X*海拔(纵坐标)的花费,从高海拔或者等高海拔处引水,需要Y*曼哈顿距离(|x1-x2|+|y1-y2|+|z1-z2|)。如果从低海拔引水过来,需要Y*曼哈顿距离+Y(水泵价格)的花费。问你让每个房子都能用水的最小花费是多少。如果有房子不能用水,输出”poor XiaoA“。

    解题思路:由于可以自建水井,所以不存在不能用水的情况。对于引水的我们可以直接建立有向边,那么对于自建水井的我们应该怎么处理呢?自环?当然不能这样写。我们可以构造一个起点,跟所有点都连边,边的权值表示自建水井的花费。然后跑朱刘算法,就能得到结果。

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<iostream>
    #include<vector>
    using namespace std;
    typedef long long INT;
    const int maxn = 1100;
    const int INF = 0x3f3f3f3f;
    struct Coor{
        int x,y,z;
    }coors[maxn];
    struct Edge{
        int from,to;
        int dist;
    }edges[maxn*maxn];
    int pre[maxn],vis[maxn],ID[maxn];
    int In[maxn];
    int ansidx ;
    int distan(Coor a,Coor b){
        return abs(a.x-b.x)+abs(a.y-b.y)+abs(a.z-b.z);
    }
    INT Zhuliu(int root,int n,int m){
        INT ret = 0;
        int u,v;
        while(true){
            for(int i = 0; i < n; i++){
                In[i] =  INF;
            }
            for(int i = 0; i < m; i++){
                Edge &e = edges[i];
                u = e.from; v = e.to;
                if(In[v] > e.dist && u != v){
                    pre[v] = u;
                    if(u == root){
                        ansidx = i;
                    }
                    In[v] = e.dist;
                }
            }
            for(int i = 0; i < n; i++){
                if(i == root) continue;
                if(In[i] == INF)
                    return -1;
            }
            In[root] = 0;
            int cntcir = 0;
            memset(vis,-1,sizeof(vis));
            memset(ID,-1,sizeof(ID));
            for(int i = 0; i < n; i++){
                ret += 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(u = pre[v]; u != v; u = pre[u]){
                        ID[u] = cntcir;
                    }
                    ID[v] = cntcir++;
                }
            }
            if(cntcir == 0){
                break;
            }
            for(int i = 0; i < n; i++){
                if(ID[i]==-1){
                    ID[i] = cntcir++;
                }
            }
            for(int i = 0; i < m; i++){
                v = edges[i].to;
                Edge & e = edges[i];
                e.from = ID[e.from];
                e.to = ID[e.to];
                if(e.from != e.to){
                    e.dist -= In[v];
                }
            }
            n = cntcir;
            root = ID[root];
        }
        return ret;
    }
    int main(){
        int n, m, k, T, cas = 0, X,Y,Z;
        while(scanf("%d%d%d%d",&n,&X,&Y,&Z)!=EOF&&(n+X+Y+Z)){
            int a,b,c;
            for(int i = 1; i <= n; i++){
                scanf("%d%d%d",&coors[i].x,&coors[i].y,&coors[i].z);
            }
            int m = 0;
            for(int i = 1; i <= n; i++){
                scanf("%d",&k);
                for(int j = 1; j <= k; j++){
                    scanf("%d",&b);
                    edges[m].from = i;
                    edges[m].to = b;
                    if(coors[i].z >= coors[b].z){
                        edges[m++].dist = distan(coors[i],coors[b]) * Y;
                    }else{
                        edges[m++].dist = distan(coors[i],coors[b]) * Y + Z;
                    }
                }
            }
            for(int i = 1; i <= n; i++){
                edges[m].from = 0;
                edges[m].to = i;
                edges[m++].dist = coors[i].z * X;
            }
            INT res = Zhuliu(0,n+1,m);
            printf("%lld
    ",res);
        }
        return 0;
    }
    

      



  • 相关阅读:
    uva 10986
    sed命令详解及应用实例
    JavaScript弹出层
    eclipspe导入hibernate的源代码
    用eclipse查看JDK源代码
    Java设计模式之适配器模式(Adapter)
    Java计算程序运行时间
    Java的IO流各个类的使用原则
    Java的IO输入输出流类的介绍(有图)
    解惑:字、位、字节、字符、字符串。
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4933328.html
Copyright © 2011-2022 走看看