1. 程式人生 > >HDU 4049 Transfer water 多根最小樹形圖

HDU 4049 Transfer water 多根最小樹形圖

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

        
 新增一個超級原點跑最小樹形圖,然後超級原點到各個點的邊設為該點作為水源的cost,然後特別注意 Hint
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;
const int maxn = 1e3 + 10;
const int maxm = 1010025;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
#define type int
int rt;
type in[maxn];
int id[maxn],used[maxn],pre[maxn];

struct edge
{
  int u,v;
  type c;
  edge(int x = 0, int y = 0, type z = 0) : u(x),v(y),c(z) {}
}es[maxm];

type MTG(int root, int n, int m)
{
  type res = 0;
  while(1)
  {
    for(int i = 1; i <= n; i++)  in[i] = INF,id[i] = used[i] = -1;
    for(int i = 0; i < m; i++)
     {
       int u = es[i].u;
       int v = es[i].v;
       if(es[i].c < in[v] && u != v)
       {
         in[v] = es[i].c,pre[v] = u;
       }
     }//找最小的入邊
    for(int i = 1; i <= n; i++)
     {
       if(i == root) continue;
       res += in[i];
       if(in[i] == INF) return -1;
     }//加入所有的入邊權到答案中
    int cnt = 0;
    for(int i = 1; i <= n; i++)//列舉每個點為起點進行找環
    {
      int v = i;
      while(v != root && used[v] != i && id[v] == -1)  used[v] = i,v = pre[v];
      if(v != root && id[v] == -1)//找到環的時候進行縮點編號
       {
         ++cnt;
         id[v] = cnt;
         for(int u = pre[v]; u != v; u = pre[u]) id[u] = cnt;
       }
    }
    if(cnt == 0) break;//無環則退出
    for(int i = 1; i <= n; i++)
       if(id[i] == -1)  id[i] = ++cnt;

    for(int i = 0; i < m; i++)//更新邊
     {
       int u = es[i].u;
       int v = es[i].v;
       es[i].u = id[u];
       es[i].v = id[v];
       if(u != v)  es[i].c -= in[v]; //更新邊權
     }
     n = cnt;//更新節點數目和根節點編號
     root = id[root];
     }
     return res;
}
int n,m;
int x[maxn],y[maxn],z[maxn];
int X,Y,Z;
int main()
{
    while(scanf("%d%d%d%d",&n,&X,&Y,&Z) && (n || X || Y || Z))
    {
      for(int i = 1; i <= n; i++)
          scanf("%d%d%d",&x[i],&y[i],&z[i]);
      m = 0;
      for(int i = 1; i <= n; i++)
      {
        int k;
        scanf("%d",&k);
        while(k--)
        {
          int u,c;
          scanf("%d",&u);
          if(u == i) continue;
          c = (abs(x[i]-x[u]) + abs(y[i]-y[u]) + abs(z[i]-z[u]))*Y;
          if(z[i] >= z[u])
          es[m++] = edge(i,u,c);
          else es[m++] = edge(i,u,c+Z);
        }
      }

    for(int i = 1; i <= n; i++)
        es[m+i-1] = edge(n+1,i,z[i]*X);
    type ans = MTG(n+1,n+1,m+n);
    if(ans == -1) printf("poor XiaoA\n");
    else printf("%d\n",ans);
  }
  return 0;
}