hdu4009Transfer water最小树形图

Posted MissZhou要努力

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu4009Transfer water最小树形图相关的知识,希望对你有一定的参考价值。

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 The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest  
Recommend lcy   |   We have carefully selected several similar problems for you:   4007  4003  4004  4005  4001   

以后做完题一定要及时发博客!!这个题差点忘了QAQ

题意:居民点取水,可以自己挖井,可以从别的地方引用,但是有些边不能引用,如果引用的地点海拔高只需要买水管子的钱,引用点海拔低不仅要买泵也要买水管子的钱!!!(这里读错题了)

很明显的不定根最小树形图,既然不定根是虚拟出来一个根把它和所有点连起来,最终的结果减去这个边长就可以了,那么不定多根也是一样啊,这题加的虚边权正好是挖井的钱啊。最后就不减去那些边权了啊。所以说,图论的核心还是建边啊

#include <cstdio>
#include <iostream>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<cstring>
using namespace std;
const double eps=1e-10;
#define M 1101
#define type int
const type inf=(1)<<30;
struct Node
    int u , v;
    type cost;
E[1200000];
int pre[M],ID[M],vis[M];
type In[M];
int n,m,x,y,z;
int abs(int x)return x>=0?x:-x;
int cnt;
void insert(int u,int v,int cost)

    E[cnt].u=u;E[cnt].v=v;E[cnt++].cost=cost;

type Directed_MST(int root,int NV,int NE) 
    type ret = 0;
    while(true) 
        //1.找最小入边
        for(int i=0;i<NV;i++) In[i] = inf;
        for(int i=0;i<NE;i++)
            int u = E[i].u;
            int v = E[i].v;
            if(E[i].cost < In[v] && u != v) 
                pre[v] = u;
                In[v] = E[i].cost;
            
        
        for(int i=0;i<NV;i++) 
            if(i == root) continue;
            if(In[i] == inf)    return -1;//除了跟以外有点没有入边,则根无法到达它
        
        //2.找环
        int cntnode = 0;
        memset(ID,-1,sizeof(ID));
        memset(vis,-1,sizeof(vis));
        In[root] = 0;
        for(int i=0;i<NV;i++) //标记每个环
            ret += In[i];
            int 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] = cntnode;
                
                ID[v] = cntnode ++;
            
        
        if(cntnode == 0)    break;//无环
        for(int i=0;i<NV;i++) if(ID[i] == -1) 
            ID[i] = cntnode ++;
        
        //3.缩点,重新标记
        for(int i=0;i<NE;i++) 
            int v = E[i].v;
            E[i].u = ID[E[i].u];
            E[i].v = ID[E[i].v];
            if(E[i].u != E[i].v) 
                E[i].cost -= In[v];
            
        
        NV = cntnode;
        root = ID[root];
    
    return ret;

struct node

    int a,b,c;
num[M];
int main()

   // freopen("cin.txt","r",stdin);
    while(~scanf("%d%d%d%d",&n,&x,&y,&z)&&n&&x&&y&&z)
    
        for(int i=0;i<n;i++)scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].c);
        m=0;
        cnt=0;
        for(int i=0;i<n;i++)
        
            int k;
            scanf("%d",&k);
            insert(n,i,x*num[i].c);
            while(k--)
            
                int t;
                scanf("%d",&t);
                t--;/!!!!
                if(t==i)continue;
                if(num[i].c>=num[t].c)insert(i,t,y*(abs(num[i].a-num[t].a)+abs(num[i].b-num[t].b)+abs(num[i].c-num[t].c)));
                else insert(i,t,z+y*(abs(num[i].a-num[t].a)+abs(num[i].b-num[t].b)+abs(num[i].c-num[t].c)));
                ///读错题 边权加少了QAQ
            
        
        int tmp=Directed_MST(n,n+1,cnt);
         printf("%d\\n",tmp);
    
    return 0;




以上是关于hdu4009Transfer water最小树形图的主要内容,如果未能解决你的问题,请参考以下文章

HDU4009 Transfer water 最小树形图

HDU4009 Transfer water —— 最小树形图 + 超级点

HDU - 4009 Transfer water(最小树形图)

HDU 4009 Transfer water

HDU 4009 Transfer water

hdu4009最小树形图板子题