Transfer water

Time Limit:3000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u

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

题目大意:首先给你n,X,Y,Z表示有n个房子,自建水井需要X*海拔(纵坐标)的花费,从高海拔或者等高海拔处引水,需要Y*曼哈顿距离(|x1-x2|+|y1-y2|+|z1-z2|)。如果从低海拔引水过来,需要Y*曼哈顿距离+Y(水泵价格)的花费。问你让每个房子都能用水的最小花费是多少。如果有房子不能用水,输出”poor XiaoA“。

解题思路:由于可以自建水井,所以不存在不能用水的情况。对于引水的我们可以直接建立有向边,那么对于自建水井的我们应该怎么处理呢?自环?当然不能这样写。我们可以构造一个起点,跟所有点都连边,边的权值表示自建水井的花费。然后跑朱刘算法,就能得到结果。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long INT;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
struct Coor{
int x,y,z;
}coors[maxn];
struct Edge{
int from,to;
int dist;
}edges[maxn*maxn];
int pre[maxn],vis[maxn],ID[maxn];
int In[maxn];
int ansidx ;
int distan(Coor a,Coor b){
return abs(a.x-b.x)+abs(a.y-b.y)+abs(a.z-b.z);
}
INT Zhuliu(int root,int n,int m){
INT ret = 0;
int u,v;
while(true){
for(int i = 0; i < n; i++){
In[i] = INF;
}
for(int i = 0; i < m; i++){
Edge &e = edges[i];
u = e.from; v = e.to;
if(In[v] > e.dist && u != v){
pre[v] = u;
if(u == root){
ansidx = i;
}
In[v] = e.dist;
}
}
for(int i = 0; i < n; i++){
if(i == root) continue;
if(In[i] == INF)
return -1;
}
In[root] = 0;
int cntcir = 0;
memset(vis,-1,sizeof(vis));
memset(ID,-1,sizeof(ID));
for(int i = 0; i < n; i++){
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(u = pre[v]; u != v; u = pre[u]){
ID[u] = cntcir;
}
ID[v] = cntcir++;
}
}
if(cntcir == 0){
break;
}
for(int i = 0; i < n; i++){
if(ID[i]==-1){
ID[i] = cntcir++;
}
}
for(int i = 0; i < m; i++){
v = edges[i].to;
Edge & e = edges[i];
e.from = ID[e.from];
e.to = ID[e.to];
if(e.from != e.to){
e.dist -= In[v];
}
}
n = cntcir;
root = ID[root];
}
return ret;
}
int main(){
int n, m, k, T, cas = 0, X,Y,Z;
while(scanf("%d%d%d%d",&n,&X,&Y,&Z)!=EOF&&(n+X+Y+Z)){
int a,b,c;
for(int i = 1; i <= n; i++){
scanf("%d%d%d",&coors[i].x,&coors[i].y,&coors[i].z);
}
int m = 0;
for(int i = 1; i <= n; i++){
scanf("%d",&k);
for(int j = 1; j <= k; j++){
scanf("%d",&b);
edges[m].from = i;
edges[m].to = b;
if(coors[i].z >= coors[b].z){
edges[m++].dist = distan(coors[i],coors[b]) * Y;
}else{
edges[m++].dist = distan(coors[i],coors[b]) * Y + Z;
}
}
}
for(int i = 1; i <= n; i++){
edges[m].from = 0;
edges[m].to = i;
edges[m++].dist = coors[i].z * X;
}
INT res = Zhuliu(0,n+1,m);
printf("%lld\n",res);
}
return 0;
}

  


HDU 4009——Transfer water——————【最小树形图、不定根】的更多相关文章

  1. HDU 4009 Transfer water 最小树形图

    分析:建一个远点,往每个点连建井的价值(单向边),其它输水线按照题意建单向边 然后以源点为根的权值最小的有向树就是答案,套最小树形图模板 #include <iostream> #incl ...

  2. HDU4009 Transfer water —— 最小树形图 + 不定根 + 超级点

    题目链接:https://vjudge.net/problem/HDU-4009 Transfer water Time Limit: 5000/3000 MS (Java/Others)    Me ...

  3. HDOJ 4009 Transfer water 最小树形图

    Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) T ...

  4. HDU 4009 Transfer water(最小树形图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...

  5. HDU - 4009 - Transfer water 朱刘算法 +建立虚拟节点

    HDU - 4009:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意: 有n户人家住在山上,现在每户人家(x,y,z)都要解决供水的问题,他可以自己 ...

  6. hdu 2121 , hdu 4009 无定根最小树形图

    hdu 2121 题目:给出m条有向路,根不确定,求一棵最小的有向生成树. 分析:增加一个虚拟节点,连向n个节点,费用为inf(至少比sigma(cost_edge)大).以该虚拟节点为根求一遍最小树 ...

  7. hdu 4009 Transfer water(最小型树图)

    Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)To ...

  8. hdu4009 Transfer water 最小树形图

    每一户人家水的来源有两种打井和从别家接水,每户人家都可能向外输送水. 打井和接水两种的付出代价都接边.设一个超级源点,每家每户打井的代价就是从该点(0)到该户人家(1~n)的边的权值.接水有两种可能, ...

  9. HDU 4009 Transfer water

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...

随机推荐

  1. SQLServer存储引擎——01.数据库如何读写数据

    一.引言 在SQL Server数据库中,数据是如何被读写的?日志里都有些什么?和数据页之间是什么关系?数据页又是如何存放数据的?索引又是用来干嘛的? 一起看看SQL Server的存储引擎. 二.S ...

  2. Android按钮单击事件处理的几种方法(Android学习笔记)

    方法一:匿名内部类实现按钮事件处理 this.btnButton=(Button)super.findViewById(R.id.mybtn); this.btnButton.setOnClickLi ...

  3. UIPasteboard

    1.UIPasteboard 简介 顾名思义,UIPasteboard 是剪切板功能,因为 iOS 的原生控件 UITextField.UITextView.UIWebView, 我们在使用时如果长按 ...

  4. java枚举enum equal与==

    原文链接:https://www.cnblogs.com/xiohao/p/7405423.html 问题 我知道Java枚举会被编译成一个包含私有构造参数和一堆静态方法的类,当去比较两个枚举的时候, ...

  5. python 对三维CT数据缩放

    项目需要对CT数据进行缩放,这里我存储CT数据的格式是numpy数组. 一共尝试了三种方法,分别是numpy.resize,cv2.resize,scipy.ndimage.interpolation ...

  6. sharepoint_study_6

    描述:SharePoint 2013配置开发环境,需要安装VS2012插件 解决: 参见地址-http://www.java123.net/detail/view-330510.html

  7. asp:FileUpload 控件上传多文件

    <asp:FileUpload runat="server" ID="imgUpload" AllowMultiple="true" ...

  8. SQL Server中,varchar和nvarchar如何选择

    正常情况下,我们使用varchar也可以存储中文字符,但是如果遇到操作系统是英文操作系统并且对中文字体的支持不全面时, 在SQL Server存储中文字符为varchar就会出现乱码(显示为??). ...

  9. IP地址概念

    1.1  IP地址概念 什么是IP地址:由32位二进制数组成,划分成4组,每组八位: 为了便于人类识别记忆,IP地址表现形式为 "点分十进制" 二进制数与十进制数的转换关系:00 ...

  10. python3 enumerate()函数笔记

    d={"A":"a","B":"b","C":"c","D" ...