HDU 4009 最小树形图

Transfer water

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


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
 
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cmath>
  4 #define maxn 1002
  5 #define maxm 1000002
  6 #define inf 0x3fffffff
  7 using namespace std;
  8 int x,y,z;
  9 struct node{
 10     int x,y,z;
 11 }ver[maxn];
 12 struct nope{
 13     int u,v,cost;
 14 }E[maxm];
 15 int in[maxn],flag[maxn],vis[maxn],pre[maxn];
 16 int dis(node a,node b){
 17      return abs(a.x-b.x)+abs(a.y-b.y)+abs(a.z-b.z);
 18 }
 19 
 20 long long int solve(int root,int nv,int ne){
 21     long long int ans=0;
 22     int u,v,i,cnt;
 23     while(true){
 24         for(i=0;i<nv;i++){
 25             in[i]=inf;
 26         }
 27         for(i=0;i<ne;i++){
 28             u=E[i].u;
 29             v=E[i].v;
 30             if(E[i].cost<in[v]&&u!=v){
 31                 in[v]=E[i].cost;
 32                 pre[v]=u;
 33             }
 34         }
 35         memset(flag,-1,sizeof(flag));
 36         memset(vis,-1,sizeof(vis));
 37         cnt=in[root]=0;
 38         for(i=0;i<nv;i++){
 39             ans+=in[i];
 40             v=i;
 41             while(vis[v]!=i&&v!=root&&flag[v]==-1){
 42                 vis[v]=i;
 43                 v=pre[v];
 44             }
 45             if(v!=root&&flag[v]==-1){
 46                 for(u=pre[v];u!=v;u=pre[u]){
 47                     flag[u]=cnt;
 48                 }
 49                 flag[v]=cnt++;
 50             }
 51         }
 52         if(cnt==0){
 53             return ans;
 54         }
 55         for(i=0;i<nv;i++){
 56             if(flag[i]==-1){
 57                 flag[i]=cnt++;
 58             }
 59         }
 60         for(i=0;i<ne;i++){
 61             v=E[i].v;
 62             E[i].u=flag[E[i].u];
 63             E[i].v=flag[v];
 64             if(E[i].u!=E[i].v){
 65                 E[i].cost-=in[v];
 66             }
 67         }
 68         nv=cnt;
 69         root=flag[root];
 70     }
 71     return ans;
 72 }
 73 
 74 int main(int argc, char const *argv[]){
 75     int n,i,a,b,id;
 76     cin.sync_with_stdio(false);
 77     while(cin>>n>>x>>y>>z&&(n||x||y||z)){
 78         for(i=0;i<n;i++){
 79             cin>>ver[i].x>>ver[i].y>>ver[i].z;
 80         }
 81         for(i=0,id=0;i<n;i++){
 82             cin>>a;
 83             while(a--){
 84                 cin>>b;
 85                 b--;
 86                 E[id].cost=dis(ver[i],ver[b])*y;
 87                 if(ver[b].z>ver[i].z){
 88                     E[id].cost+=z;
 89                 }
 90                 E[id].u=i;
 91                 E[id++].v=b;
 92             }
 93         }
 94         for(i=0;i<n;i++){
 95             E[id].u=n;
 96             E[id].v=i;
 97             E[id++].cost=ver[i].z*x;
 98         }
 99         cout<<solve(n,n+1,id)<<endl;
100     }
101     return 0;
102 }

 

posted @ 2016-12-05 16:18  ガ落涙『不變』  阅读(80)  评论(0编辑  收藏  举报