HDU 4009 Transfer water (最小树形图,建图,4级)

E - Transfer water
Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u
Appoint description:

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|. 
 
 思路:算是半个模板题。每条边建权为u->v的花费。独立建井的花费就设一个超级源向所有点建边为建井花费。
   来遍最小树形图就OK了。
#include<cstdio>
#include<iostream>
#include<cstring>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
typedef long long type;
using namespace std;
const int nn=1003;
const int mm=nn*nn+nn;
const type oo=1e18;
class point
{
  public:int x,y,z;
}p[nn];
class Edge
{
  public:int u,v;type w;
}e[mm];
type well[nn];
int n,X,Y,Z;
int fabs(int x)
{
  if(x<0)return -x;
  return x;
}
type get(int x,int y)
{
  type a,b,c;
  type ret=fabs(p[x].x-p[y].x)+fabs(p[x].y-p[y].y)+fabs(p[x].z-p[y].z);
  ret*=Y;
  if(p[x].z<p[y].z)ret+=Z;
  //if(well[y]<ret)ret=well[y];
  return ret;
}
type in[nn];
int vis[nn],pre[mm],id[nn];
type Directed_MST(int root,int V,int E)
{ int u,v;
  type ret=0;
  while(1)
  {
    FOR(i,0,V-1)in[i]=oo;
    in[root]=0;
    FOR(i,0,E-1)
    {
      u=e[i].u;v=e[i].v;
      if(u!=v&&e[i].w<in[v])
      {
        in[v]=e[i].w;pre[v]=u;
      }
    }
    FOR(i,0,V-1)
    {
      if(in[i]==oo)return -1;
    }
    //find cicle
    clr(vis,-1);clr(id,-1);
    int bcc_no=0;
    FOR(i,0,V-1)
    { 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(int u=pre[v];u!=v;u=pre[u])
          id[u]=bcc_no;
        id[v]=bcc_no++;
      }
    }
    if(bcc_no==0)break;
    FOR(i,0,V-1)
    if(id[i]==-1)
      id[i]=bcc_no++;
    FOR(i,0,E-1)
    {
      u=e[i].u;v=e[i].v;
      e[i].u=id[u];
      e[i].v=id[v];
      if(id[u]^id[v])
      {
        e[i].w-=in[v];
      }
    }
    V=bcc_no;root=id[root];
  }
  return ret;
}
int main()
{ int m,num,x;
  while(~scanf("%d%d%d%d",&n,&X,&Y,&Z))
  {
    if(n==0&&X==0&&Y==0&&Z==0)break;
    FOR(i,1,n)
    {scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
     well[i]=X*p[i].z;
    }
    num=0;
    FOR(i,1,n)
    {
      scanf("%d",&m);
      FOR(j,1,m)
      {
        scanf("%d",&x);
        if(x==i)continue;
        e[num].u=i;e[num].v=x;e[num].w=get(i,x);
        ++num;
      }
    }
    //add src=0
    FOR(i,1,n)
    {
      e[num].u=0;e[num].v=i;e[num].w=well[i];++num;
    }
    type ans=Directed_MST(0,n+1,num);
    printf("%I64d\n",ans);
  }
  return 0;
}


posted @ 2013-09-01 10:03  剑不飞  阅读(177)  评论(0编辑  收藏  举报