Transfer water

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 3821    Accepted Submission(s): 1371
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|.
题意:在山上有n户人家,给出他们的坐标(x,y,z)z是海拔;每户人家的水来源有地下水,或从其他人家引进来,如果打地下水每米X元,深度是海拔,如果从他家引水,有两种情况,一是供水人家海拔较高,费用是每米Y元,距离是曼哈顿距离,二是海拔较低,需要水泵,一个水泵需要额外花费Z元,问要是每家人都有水,至少花费是多少?
分析:水的最终来源肯定是地下水,所以至少有一家人是从地下获得的水,所以n户人家编号1到n,地下水编号n+1,地下水到每户建边,花费是L*Z,然后按照题目中的关系把相应的人家建边,费用为(Z)+L*Y;地下水就是根节点,运行一下最小树形图即可:
程序:
#include"string.h"
#include"stdio.h"
#include"math.h"
#include"queue"
#define eps 1e-10
#define M 1009
#define inf 100000000
using namespace std;
struct node
{
int x,y,z;
}p[M];
struct edge
{
int u,v;
int w;
}edge[M*M];
int pre[M],id[M],use[M],in[M];
int Fabs(int x)
{
return x>0?x:-x;
}
int mini_tree(int root,int n,int m)//分别是树根,节点数,边数,序号从1开始
{
int ans=0;
int i,u;
while(1)
{
for(i=1;i<=n;i++)
in[i]=inf;
for(i=1;i<=m;i++)
{
int u=edge[i].u;
int v=edge[i].v;
if(edge[i].w<in[v]&&u!=v)
{
in[v]=edge[i].w;
pre[v]=u;
}
}//找最小的入边
for(i=1;i<=n;i++)
{
if(i==root)continue;
ans+=in[i];//把边权加起来
if(in[i]==inf)//如果存在没有入弧的点则不存在最小树形图
return -1;
}
memset(id,-1,sizeof(id));
memset(use,-1,sizeof(use));
int cnt=0;
for(i=1;i<=n;i++)//枚举每个点,搜索找环
{
int v=i;
while(v!=root&&use[v]!=i&&id[v]==-1)
{
use[v]=i;
v=pre[v];
}
if(v!=root&&id[v]==-1)//当找到环的时候缩点编号
{
++cnt;
id[v]=cnt;
for(u=pre[v];u!=v;u=pre[u])
id[u]=cnt;
}
}
if(cnt==0)//如果没有环结束程序
break;
for(i=1;i<=n;i++)//把余下的不在环里的点编号
if(id[i]==-1)
id[i]=++cnt;
for(i=1;i<=m;i++)//建立新的图
{
int u=edge[i].u;
int v=edge[i].v;
edge[i].u=id[u];
edge[i].v=id[v];
if(edge[i].u!=edge[i].v)
edge[i].w-=in[v];
}
n=cnt;//更新节点数和根节点的编号
root=id[root];
}
return ans;
}
int main()
{
int n,X,Y,Z,i,j;
while(scanf("%d%d%d%d",&n,&X,&Y,&Z),n||X||Y||Z)
{
for(i=1;i<=n;i++)
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
int m=0;
for(i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
while(k--)
{
scanf("%d",&j);
if(i==j)continue;
m++;
edge[m].u=i;
edge[m].v=j;
if(p[j].z>p[i].z)
edge[m].w=Z+Y*(Fabs(p[i].x-p[j].x)+Fabs(p[i].y-p[j].y)+Fabs(p[i].z-p[j].z));
else
edge[m].w=Y*(Fabs(p[i].x-p[j].x)+Fabs(p[i].y-p[j].y)+Fabs(p[i].z-p[j].z));
}
}
for(i=1;i<=n;i++)
{
m++;
edge[m].u=n+1;
edge[m].v=i;
edge[m].w=p[i].z*X;
}
int ans=mini_tree(n+1,n+1,m);
printf("%d\n",ans);
}
return 0;
}

最小树形图(hdu4009)的更多相关文章

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

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

  2. hdu4009最小树形图板子题

    /*调了一下午的最小树形图,昨天刚刚看懂模板..最小树形图,就是有向图的最小生成树,很神奇==*/ #include<iostream> #include<cstring> # ...

  3. hdu4009最小树形图

    多建一个根,连到每一个点,然后花费是建水井的钱 然后跑一边最小树形图即可,这题必定有解,因为可以从根开始到每一点,可以不用判无解的情况 #include<map> #include< ...

  4. hdu4009 Transfer water 最小树形图

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

  5. HDU4009 Transfer water 【最小树形图】

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

  6. POJ 3164 Command Network 最小树形图模板

    最小树形图求的是有向图的最小生成树,跟无向图求最小生成树有很大的区别. 步骤大致如下: 1.求除了根节点以外每个节点的最小入边,记录前驱 2.判断除了根节点,是否每个节点都有入边,如果存在没有入边的点 ...

  7. 树的问题小结(最小生成树、次小生成树、最小树形图、LCA、最小支配集、最小点覆盖、最大独立集)

    树的定义:连通无回路的无向图是一棵树. 有关树的问题: 1.最小生成树. 2.次小生成树. 3.有向图的最小树形图. 4.LCA(树上两点的最近公共祖先). 5.树的最小支配集.最小点覆盖.最大独立集 ...

  8. hdu2121 Ice_cream’s world II 最小树形图(难)

    这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...

  9. bzoj4349: 最小树形图

    最小树形图模板题…… 这种\(O(nm)\)的东西真的能考到么…… #include <bits/stdc++.h> #define N 60 #define INF 1000000000 ...

随机推荐

  1. 写给测试人员:不是所有的bug都需要修复

    用户往往对产品中各种各样的bug抱怨不已,而测试人员往往认为自己的职责就是揪出这些所有的bug并把它们全都修复.然而,这是一个误区.微软卓越测试工程总监Alan Page近日撰文,再次解释了有哪些bu ...

  2. Python python的输入输出

    #-*- coding:utf-8 -*- #屏蔽中文乱码方案一(官方推荐) #这个语句必须顶行写 #屏蔽中文乱码方案二(不建议使用) '''#coding=utf-8 ''' #input(),输入 ...

  3. 关于Cocos Creator用js脚本代码播放骨骼动画的步骤和注意事项

    步骤: 1.用cc.find()方法找到相应的骨骼动画节点,并把这个对象赋值给一个var出来的新对象. 具体代码:var spineboy_anim = cc.find("UI_Root/a ...

  4. : error C3861: “Sleep”: 找不到标识符

    编译的时候:error C3861: “sleep”: 找不到标识符,是什么原因啊?编译的时候:error C3861: “sleep”: 找不到标识符,是什么原因啊?哪位好心的哥哥告诉我吧,分全是你 ...

  5. 深入浅出Redis-redis哨兵集群[转]

    1.Sentinel 哨兵 Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所 ...

  6. Ubuntu 13.04 安装 Oracle11gR2

    #step 1: groupadd -g 2000 dbauseradd -g 2000 -m -s /bin/bash -u 2000 griduseradd -g 2000 -m -s /bin/ ...

  7. 使用Visual Studio将C#生成DLL文件的方法

    1.命令方式 打开Visual Studio安装目录下的开发人员命令提示 译 File.cs 以产生 File.exe csc File.cs 编译 File.cs 以产生 File.dll csc ...

  8. MathType中公式不对齐怎么办

    MathType是一款专门用来编辑数学公式的数学公式编辑器,利用它可以在文档中快速编辑公式,与文字完美结合,可以编辑出各种各样的数学符号与公式,省下你不少的时间. 一.对齐上标与下标 MathType ...

  9. 记录下DynamicXml和HtmlDocument 使用方式

    之前解析都是XmlDocument.Load 而现在可以利用DynamicXml生成Dynamic对象实现强类型操作,很好用. /// <summary> /// 根据Xml路径动态解析成 ...

  10. Jmeter content-type:multipart/form-data温故

    本文讲三种content-type以及在Jmeter中对应的参数输入方式 第一部分:目前工作中涉及到的content-type 有三种: content-type:在Request Headers里, ...