zoukankan      html  css  js  c++  java
  • hdu 4009 Transfer water

    Transfer water

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


    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
     
    
    
    Recommend
    lcy
    //加个0点,然后就是模板题目
    //最开始学的是矩阵储存方式,这里就超时
    //然后网上看了下、用边储存、看了一早上、想了一下午、终于是明白了

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #define N 1002 #define INF 1000000000 using namespace std; struct node { int x,y,h; }; node inf[N]; struct Edge { int s,t; int cost; }edge[1000*600]; int dis(int &i,int &j) { return abs(inf[i].x-inf[j].x)+abs(inf[i].y-inf[j].y)+abs(inf[i].h-inf[j].h); } int pre[N],in[N]; int vis[N],Id[N];int nv; int ne; int zhuliu() { int i,s,t; nv++; int ret = 0; while(1) { for(i=1;i<nv;i++) in[i] = INF,Id[i]=-1,vis[i]=-1; for(i = 0;i < ne;i++) { s = edge[i].s; t = edge[i].t; if(edge[i].cost>=in[t]||s==t)continue;// 1:s==t pre[t] = s; in[t] = edge[i].cost; } pre[0]=0; for(i = 1;i < nv;i++) { ret += in[i]; // 2 if(in[i] == INF) return -1; } int Idx = 1; Id[0]=vis[0]=0; for(i = 1;i < nv;i++)if(vis[i]==-1) { t = i; while(vis[t]==-1) { vis[t] = i; t = pre[t]; } if(vis[t]!=i||t==0) continue; for(int s=pre[t];s!= t;s = pre[s])//这里想了一下午,明白了我用i代替t为什么不可以 Id[s] = Idx; //用i代替t会超时,原因就是一个环还还连了不在环内的边就会死循环 Id[t]=Idx++; } if(Idx == 1) break; for(i = 1;i < nv;i++) if(Id[i] == -1) Id[i] = Idx++; for(i = 0;i < ne;i++) { int v = edge[i].t; edge[i].s = Id[edge[i].s];//这2步影响了1. edge[i].t = Id[edge[i].t]; edge[i].cost -= in[v];//这里和2有关,不然只需减掉与环有关的权 } nv = Idx; } return ret; } int main() { int i,j,k; int X,Y,Z; while(scanf("%d %d %d %d",&nv,&X,&Y,&Z),nv) { ne=0; for(i=1;i<=nv;i++) { scanf("%d %d %d",&inf[i].x,&inf[i].y,&inf[i].h); edge[ne].s=0; edge[ne].t=i; edge[ne++].cost=X*inf[i].h; } for(i=1;i<=nv;i++) { scanf("%d",&k); while(k--) { scanf("%d",&j); if(j==i) continue; edge[ne].s=i; edge[ne].t=j; edge[ne].cost=dis(i,j)*Y; if(inf[i].h<inf[j].h) edge[ne].cost+=Z; ne++; } } printf("%d\n",zhuliu()); } return 0; }
    //以下是根据我自己对朱刘算法的理解,写的一些代码、估计自己的思想受第一次见到的算法思路影响吧
    //总之对这个算法加深了理解
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #define N 1002
    #define INF 1000000000
    using namespace std;
    struct node
    {
        int x,y,h;
    };
    node inf[N];
    struct Edge
    {
        int s,t;
        int cost;
    }edge[1000*600];
    int dis(int &i,int &j)
    {
       return abs(inf[i].x-inf[j].x)+abs(inf[i].y-inf[j].y)+abs(inf[i].h-inf[j].h);
    }
    int pre[N],in[N];
    int vis[N],Id[N];
    int flag[N];
    int nv;
    int ne;
    int zhuliu()
    {
      int i,s,t;
        nv++;
        int  ret = 0;
        while(1)
        {
            for(i=1;i<nv;i++) in[i] = INF,Id[i]=-1,vis[i]=-1,flag[i]=0;
            for(i = 0;i < ne;i++)
            {
                s = edge[i].s;
                t = edge[i].t;
                if(edge[i].cost>=in[t]||s==t)continue;
                pre[t] = s;
                in[t] = edge[i].cost;
            }
             pre[0]=0;
            for(i = 1;i < nv;i++)
            {
               // ret += in[i];
                if(in[i] == INF)
                    return -1;
            }
            int Idx = 1,tp=0;
            Id[0]=vis[0]=0;
            for(i = 1;i < nv;i++)if(vis[i]==-1)
            {
                t = i;
                while(vis[t]==-1)
                {
                    vis[t] = i;
                    t = pre[t];
                }
                if(vis[t]!=i||t==0) continue;
                for(int s=pre[t];s!= t;s = pre[s])
                    Id[s] = Idx,flag[s]=1,tp+=in[s];
                flag[t]=1;tp+=in[t];
                Id[t]=Idx++;
            }
            if(Idx == 1)
            {
                 for(i = 1;i < nv;i++)
                 {
                     ret+=in[i];
                 }
                  break;
            }
            ret+=tp;
            for(i = 1;i < nv;i++)  if(Id[i] == -1)
                Id[i] = Idx++;
            for(i = 0;i < ne;i++)
            {
                int v = edge[i].t;
                edge[i].s = Id[edge[i].s];
                edge[i].t = Id[edge[i].t];
                if(flag[v]) //只改变与环有关的权值
                edge[i].cost -= in[v];
            }
            nv = Idx;
        }
        return ret;
    }
    int main()
    {
        int i,j,k;
        int X,Y,Z;
        while(scanf("%d %d %d %d",&nv,&X,&Y,&Z),nv)
        {
            ne=0;
            for(i=1;i<=nv;i++)
             {
              scanf("%d %d %d",&inf[i].x,&inf[i].y,&inf[i].h);
               edge[ne].s=0;
               edge[ne].t=i;
               edge[ne++].cost=X*inf[i].h;
             }
            for(i=1;i<=nv;i++)
            {
                scanf("%d",&k);
                while(k--)
                {
                    scanf("%d",&j);
                    if(j==i) continue;
                    edge[ne].s=i;
                    edge[ne].t=j;
                    edge[ne].cost=dis(i,j)*Y;
                    if(inf[i].h<inf[j].h)
                      edge[ne].cost+=Z;
                    ne++;
                }
            }
             printf("%d\n",zhuliu());
        }
        return 0;
    }


  • 相关阅读:
    【坑】提答题
    Google Code Jam 2014 Round2
    湖北省队互测Week1
    [xyz模拟题]动态维护树的直径
    音乐会的等待【单调栈】
    51nod1202【DP-树状数组维护】
    51nod1113【矩阵快速幂】
    51nod1255【贪心-栈的应用】
    Lightoj1059【最小生成树】
    SPOJ IAPCR2F 【并查集】
  • 原文地址:https://www.cnblogs.com/372465774y/p/2662594.html
Copyright © 2011-2022 走看看