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. SQL SERVER 2005快捷键

    一.SQL SERVER 2005快捷键 快捷键 功能 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 ...

  2. delete指针以后应赋值为NULL——QT deletelater指针以后也同样要马上赋值为NULL

    delete p后,只是释放了指针指向的内存空间.p并不会自动被置为NULL,而且指针还在,同时还指向了之前的地址 delete NULL编译器不会报错(因为delete空指针是合法的) 例: 对一个 ...

  3. 基本上每个应用程序领域的程序员都有使用 C++

    C++ 的使用基本上每个应用程序领域的程序员都有使用 C++. C++ 通常用于编写设备驱动程序和其他要求实时性的直接操作硬件的软件. C++ 广泛用于教学和研究. 任何一个使用苹果电脑或 Windo ...

  4. 高速入门:十分钟学会Python

    初试牛刀 如果你希望学习Python这门语言.却苦于找不到一个简短而全面的新手教程.那么本教程将花费十分钟的时间带你走入Python的大门.本文的内容介于教程(Toturial)和速查手冊(Cheat ...

  5. 10个开发中常用的PHP代码样例

    一.黑名单过滤 function is_spam($text, $file, $split = ':', $regex = false){ $handle = fopen($file, 'rb'); ...

  6. 系统管理员应该知道的20条Linux命令

    如果您的应用程序不工作,或者您希望在寻找更多信息,这 20 个命令将派上用场. 在这个全新的工具和多样化的开发环境井喷的大环境下,任何开发者和工程师都有必要学习一些基本的系统管理命令.特定的命令和工具 ...

  7. Spring+Swagger文档无法排序问题解决

    项目中用到swagger用于自动生成文档,遇到了好多结合后的问题.而对于这个排序问题,在查看了后端Swagger原代码之后,发现视乎当前使用的swagger(不是springfox,应该不是官方的,网 ...

  8. C# Asp.net 制作一个windows服务

    那下面就来说说如何制作一个服务来 实现开机自动启动,每隔一段时间向student表中插入数据. 步骤:  1)   新建项目 ---> Windows 服务 2) 拖放Times控件 工具箱中 ...

  9. RedisTemplate实现事物问题剖析和解决

    一.问题描述 Redis为单进程单线程模式,采用队列模式将并发访问变成串行访问,Redis对事物支持不会很复杂,当一个客服端连接Redis服务时,发出了MULTI命令时,这个连接会进入事物,在执行MU ...

  10. C#字符串二进制互换

    static void Main(string[] args)         {             string str = "宋军辉";             Cons ...