hdu 4009 Transfer water(最小型树图)
Transfer water
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 3995 Accepted Submission(s): 1438
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
------------ 遇到一个国人发明的算法(algorithm)--------
下面就开始简单的剖析一下,下面的部分吧! 看图
----------~~~~~~~~~~~~~~~魔板AC~~~~~~~~~~~~~--------------
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1010
#define type int
const int inf = ~0u >> 1;
struct node
{
int u,v;
type cost;
node(){}
node(int _u,int _v,type _c):u(_u),v(_v),cost(_c){}
}e[maxn * maxn];
int pre[maxn],id[maxn],vis[maxn];
type in[maxn];
type dirmst(int root,int nv,int ne)
{
type ret = 0;
while(1)
{
//find the smallest in-arc
fill(in,in + nv,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;//there are some nodes other than root with no in-arc connected to it
}
//find the dir circle
int cntnode = 0;
fill(id,id + nv,-1);
fill(vis,vis + nv,-1);
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;//no circle
for(int i = 0;i < nv;i++)
if(id[i] == -1)
id[i] = cntnode++;
//compress the nodes
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;
}
int n,tot,X,Y,Z;
int ab(int x)
{
return x >= 0?x:-x;
}
struct point
{
int x,y,z;
point(){}
point(int a,int b,int c):x(a),y(b),z(c){}
point operator - (const point p)
{
return point(x - p.x,y - p.y,z - p.z);
}
int dis()
{
return ab(x) + ab(y) + ab(z);
}
}p[maxn];
int main()
{
while(scanf("%d %d %d %d",&n,&X,&Y,&Z) == 4 && (n || X || Y || Z))
{
tot = 0;
for(int i = 1;i <= n;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
p[i] = point(a,b,c);
e[tot++] = node(0,i,ab(p[i].z) * X);
}
for(int i = 1;i <= n;i++)
{
int opt;
scanf("%d",&opt);
for(int j = 0;j < opt;j++)
{
int a;
scanf("%d",&a);
if(a == i)
continue;
int temp = Y * (p[i] - p[a]).dis();
if(p[i].z < p[a].z)
temp += Z;
e[tot++] = node(i,a,temp);
}
}
int ans = dirmst(0,n + 1,tot);
if(ans == -1)
puts("poor XiaoA");
else
printf("%dn",ans);
}
}
- Golang语言中Path包用法
- Golang中container/list包中的坑
- 关于Golang语言数组索引的有趣现象
- Golang不定参数
- Go并发编程基础(译)
- go-concurrent-programming.md
- Go语言并发模型:以并行处理MD5为例
- golang 使用json 包 实现序列化
- 【远古文章】用 Go 语言来看 Android! 出发, Android, 出发!
- Leaf 游戏服务器框架简介
- MongoDB 存储过程的使用以及性能调优方案
- [go语言]利用缓冲信道来实现网游帐号验证消息的分发和等待
- 【Golang语言社区--投稿专区】简单,好玩,有趣的命令行版12306(golang)
- 网游内存数据库的设计(1)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 谈谈线程
- springboot整合定时框架-Elastic-job-lite
- hashMap的循环姿势你真的使用对了吗?
- SpringBoot整合常用技术
- SpringBoot整合Quartz实现定时任务(单任务、多任务)
- 你的登录接口,真的安全吗?如何预防黑客攻击
- JWT登录信息加密
- 我画了近百张图来理解红黑树
- SpringBoot之API--Swagger2接口文档管理
- 索引失效原理,终于有人讲明白了
- 你真了解你的系统吗?它要崩溃了
- 伸手党的容器镜像加固流程
- 前端模块化开发--React框架(四):高级应用(redux)
- Tomcat性能优化,学会薪水翻倍
- 前端模块化开发--React框架(三):应用进阶(react-router4&&antd框架)