讲一下建图过程,首先建立一个超级源点S,对于这个源点,向每个HOUSE连一条有向边,权值为该HOUSE建立WELL的费用,即高度*X。

然后每个可以连边的WELL之间,费用为曼哈顿距离*Y,然后考虑两边的高度,如果需要连接PUMB,则在该费用上+Z。

这样建图之后,以S为根,跑一遍最小树形图算法即可。

CODE:

#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#define Max 2505
#define FI first
#define SE second
#define ll long long
#define PI acos(-1.0)
#define inf 0x3fffffff
#define LL(x) ( x << 1 )
#define bug puts("here")
#define PII pair<int,int>
#define RR(x) ( x << 1 | 1 )
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i ) using namespace std; inline void RD(int &ret) {
char c;
int flag = 1 ;
do {
c = getchar();
if(c == '-')flag = -1 ;
} while(c < '0' || c > '9') ;
ret = c - '0';
while((c=getchar()) >= '0' && c <= '9')
ret = ret * 10 + ( c - '0' );
ret *= flag ;
} inline void OT(int a) {
if(a >= 10)OT(a / 10) ;
putchar(a % 10 + '0') ;
} /*********************************************/
#define N 1005
int n , X , Y , Z ;
struct C{
int x , y , z ;
}city[N] ;
int num = 0 ;
int S ;
void init(){
num = S = 0 ;
}
struct kdq{
int s ,e ,l ;
}E[N * N] ;
int get_Mandis(int i ,int j){
return abs(city[i].x - city[j].x) + abs(city[i].y - city[j].y) + abs(city[i].z - city[j].z) ;
}
void add(int s ,int e ,int l){
E[num].s = s ;
E[num].e = e ;
E[num].l = l ;
num ++ ;
}
int pre[N] , vis[N] , id[N] , in[N] ;
int Directed_MST(int root ,int NV ,int NE){
int ret = 0 ;
while(1){
for (int i = 0 ; i < NV ; i ++ )in[i] = inf ;
for (int i = 0 ; i < NE ; i ++ ){
int s = E[i].s ;
int e = E[i].e ;
if(in[e] > E[i].l && s != e){
in[e] = E[i].l ;
pre[e] = s ;
}
}
for (int i = 0 ; i < NV ; i ++ ){
if(i == root)continue ;
if(in[i] == inf)return -1 ;
}
int cntnode = 0 ;
mem(vis , -1) ;
mem(id , -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 ;
for (int i = 0 ; i < NV ; i ++ )if(id[i] == -1)id[i] = cntnode ++ ;
for (int i = 0 ; i < NE ; i ++ ){
int s = E[i].s ;
int e = E[i].e ;
E[i].s = id[s] ;
E[i].e = id[e] ;
if(id[s] != id[e])E[i].l -= in[e] ;
}
NV = cntnode ;
root = id[root] ;
}
return ret ;
}
int main() {
int a , k ;
while(scanf("%d%d%d%d",&n,&X,&Y,&Z) , ( n + X + Y + Z)){
init() ;
for (int i = 1 ; i <= n ;i ++ ){
RD(city[i].x) ;RD(city[i].y) ;RD(city[i].z) ;
}
for (int i = 1 ; i <= n ; i ++ ){
RD(k) ;
while(k -- ){
RD(a) ;
if(i == a)continue ;//自环
int dis = get_Mandis(i , a) * Y ;
if(city[i].z < city[a].z)dis += Z ;
add(i , a , dis) ;
}
}
for (int i = 1 ; i <= n ; i ++ ){
add(S , i , city[i].z * X) ;
}
int ans = Directed_MST(0 , n + 1 , num) ;
if(ans == -1)puts("poor XiaoA") ;
else OT(ans) ;
puts("") ;
}
return 0 ;
}

HDU 4009 不定根最小树形图的更多相关文章

  1. HDU 2121 Ice_cream’s world II 不定根最小树形图

    题目链接: 题目 Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  2. HDUOJ--2121--Ice_cream’s world II【朱刘算法】不定根最小树形图

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2121 题意:n个顶点,m条边,求从某一点起建立有向图最小生成树而且花费最小.输出最小花费和根节点下标. ...

  3. hdu 4966 GGS-DDU (最小树形图)

    比较好的讲解:http://blog.csdn.net/wsniyufang/article/details/6747392 view code//首先为除根之外的每个点选定一条入边,这条入边一定要是 ...

  4. UVA 6199 不定根最小树形图

    首先是最小树形图的介绍. 看这个博客.最小树形图 上面介绍的很详细了,我就讲一下这道题的题意. 首先给出一些二维点坐标,这些坐标之间构成一些有向图,根据题意,假设两个点a(x1 ,y1) ,b(x2 ...

  5. HDU 4009——Transfer water——————【最小树形图、不定根】

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

  6. HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】

    Ice_cream’s world II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

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

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

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

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

  9. HDU 2121:Ice_cream’s world II(不定根的最小树形图)

    题目链接 题意 求有向图的最小生成树,且根不定. 思路 最小树形图即求有向图的最小生成树,用的是朱刘算法. 这里不定根,那么可以建立一个虚根,让虚根和所有点相连,权值为一个很大的数(这里直接设为所有边 ...

随机推荐

  1. ubuntu下aircrack-ng的wifi破解

    首先安装aircrack-ng,apt-get install aircrack-ng. 然后打开shell,输入airmon-ng start wlan0. 输入airodump-ng mon0. ...

  2. WCF编程系列(二)了解WCF

    WCF编程系列(二)了解WCF   面向服务     服务是复用进化的结果,起初的复用是函数,面向对象编程的出现使复用从函数上升到对象,随后面向组件编程又将复用从对象上升到组件,现在面向服务编程将复用 ...

  3. CSS FIXED porn javhd

    CSS position property - W3Schools W3Schools › cssref › pr_class_position Definition and Usage. The p ...

  4. JavaScript中的apply与call与arguments对象

    (一) call方法 语法:presentObj.call(thisObj,arg1,arg2,arg3...) 参数thisObj :将被用作当前对象presentObj的对象. 当thisObj无 ...

  5. Python快速入门学习笔记(一)

    本篇文章适合有其他高级语言基础的人群阅读 使用的Python版本为python2.7 使用的编辑器为Sublime Text3 世界始于Hello World: print 'Hello world' ...

  6. OpenJudge 2766 最大子矩阵

    1.链接: http://bailian.openjudge.cn/practice/2766 2.题目: 总Time Limit: 1000ms Memory Limit: 65536kB Desc ...

  7. HaProxy+keepalived实现负载均衡

    HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持 ...

  8. mysql---左连接、右连接、内连接之间的区别与联系

    现有两张表 第一张表为男生表,记录了男生的姓名和配偶的编号 第二张表为女生表,记录了女生的姓名和自己的编号 第一种情况:主持人请所有男生都上台,并且带上自己的配偶.这时不管男生有没有配偶都要上台,所以 ...

  9. jQuery Mobile里xxx怎么用呀? (事件篇)

    jQuery Mobile里$(document).ready()怎么用呀? 相关链接: http://stackoverflow.com/questions/14468659/jquery-mobi ...

  10. C#获取运行程序的进程ID

    C#获取运行程序的进程ID [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int G ...