HDU - 4009:http://acm.hdu.edu.cn/showproblem.php?pid=4009

题意:

    有n户人家住在山上,现在每户人家(x,y,z)都要解决供水的问题,他可以自己挖井,也可以从特定的别人那里调水。问所有人家都接上水后最小的花费。

思路:

    据说是一道最小生成树的模版题,我觉得在建图上还是比较难的。每个人家从别人那里调水的关系是明确的,直接建图就行。那自己挖井,我们可以建立一个虚拟的源点,向每个点连一条边,边的权值就是挖井所要的费用。建完图后,就可以跑一遍最小树形图了。显然答案是一定存在的;

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxm = 1e6+;
const int maxn = 1e3+;
struct edge
{
int from,to,c;
}e[maxm];
int X,Y,Z,n,tot;
struct node
{
int x,y,z;
}p[maxn];
int in[maxn],vis[maxn],pre[maxn],id[maxn];
int rtt;
int zhuliu(int root,int n,int m){
int res = ;
while(true){
memset(in, inf, sizeof(in));
for(int i=; i<=m ;i++){
if(e[i].from != e[i].to && e[i].c < in[e[i].to]){
pre[e[i].to] = e[i].from;
in[e[i].to] = e[i].c;
}
}
for(int i=; i<=n; i++){
if(i!=root && in[i] == inf)
return -;
}
int tn = ,v;
memset(id,-,sizeof(id));
memset(vis,-,sizeof(vis));
in[root] = ;
for(int i=; i<=n; i++){
res += in[i];
v = i;
while(v!=root && id[v] == - && vis[v] != i){
vis[v] = i;
v = pre[v];
}
if(v!=root && id[v] == -){
id[v] = ++tn;
for(int u = pre[v] ; u!=v; u = pre[u]){
id[u] = tn;
}
}
}
if(tn == )break;
for(int i=; i<=n; i++){
if(id[i] == -)id[i] = ++tn;
} for(int i=; i<=m; i++){
int v = e[i].to;
e[i].to = id[e[i].to];
e[i].from = id[e[i].from];
if(e[i].to != e[i].from){
e[i].c -= in[v];
}
}
n = tn;root = id[root];
}
return res;
}
int get(int u,int v){
int ans = abs(p[u].x - p[v].x) +abs(p[u].y - p[v].y)+abs(p[u].z - p[v].z);
ans = ans * Y;
if(p[v].z > p[u].z)ans += Z;
return ans;
}
int main(){
while(~scanf("%d%d%d%d", &n, &X,&Y,&Z) && n+X+Y+Z){
tot = ;
for(int i=; i<=n; i++){
scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].z);
}
for(int i=; i<=n; i++){
int x,k;scanf("%d", &k);
for(int j=; j<=k; j++)
{
scanf("%d", &x);
e[++tot].from = i;
e[tot].to = x;
e[tot].c = get(i,x);
}
} for(int i=; i<=n; i++){
e[++tot].from = ;
e[tot].to = i;
e[tot].c = X * p[i].z;
} int ans = zhuliu(,n,tot);
printf("%d\n", ans);
}
return ;
}

HDU - 4009

HDU - 4009 - Transfer water 朱刘算法 +建立虚拟节点的更多相关文章

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

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

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

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

  3. HDU 4009 Transfer water

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

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

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

  5. HDU 4009 Transfer water 最小树形图

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

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

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

  7. hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , ...

  8. POJ 3164 Command Network ( 最小树形图 朱刘算法)

    题目链接 Description After a long lasting war on words, a war on arms finally breaks out between littlek ...

  9. poj3164(最小树形图&朱刘算法模板)

    题目链接:http://poj.org/problem?id=3164 题意:第一行为n, m,接下来n行为n个点的二维坐标, 再接下来m行每行输入两个数u, v,表点u到点v是单向可达的,求这个有向 ...

随机推荐

  1. 走近OPENCV // opencv 2.4.9+vs2013配置

    一直很懒去配opencv,这几周忍不了终于抽空来配了一下环境... 用的是旧版opencv2.4系列,最新到3.0了,之后再看看教程不知道有什么特别大的区别. (FF14国服没有4.0 // 8.19 ...

  2. Python装饰器 (转)

    多个装饰器执行的顺序就是从最后一个装饰器开始,执行到第一个装饰器,再执行函数本身. #多个装饰器 import time def deco01(func): def wrapper(*args, ** ...

  3. 一看就懂的K近邻算法(KNN),K-D树,并实现手写数字识别!

    1. 什么是KNN 1.1 KNN的通俗解释 何谓K近邻算法,即K-Nearest Neighbor algorithm,简称KNN算法,单从名字来猜想,可以简单粗暴的认为是:K个最近的邻居,当K=1 ...

  4. 使用用树莓派打造远程WEB服务器

    简介:系统配置Raspberry Pi 3B + Raspbian + MySQL5.7 + Tomcat 9 + Nginx + 公网IP. 工具:Win32DiskImager .FileZill ...

  5. 从无到满意offer,你需要知道的那些事

    本文首发于微信公众号:[坂本先生] 原文地址:从无到满意offer,你需要知道的那些事 1.求职软件/网站汇总 软件 评价 推荐指数 拉钩网 手机端产品设计的比较好,当时在上面找到了很多的面试机会 5 ...

  6. RabbitMQ与spring集成,配置完整的生产者和消费者

    RabbitMQ与AMQP协议详解可以看看这个 http://www.cnblogs.com/frankyou/p/5283539.html 下面是rabbitMQ和spring集成的配置,我配置了二 ...

  7. Unity经典游戏教程之:冒险岛

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  8. [nghttp2]压测工具,源码编译并进行deb打包过程

    编译环境:deepin 15.11桌面版 nghttp2下载地址:https://github.com/nghttp2/nghttp2 环境要求 emm只能在类Linux环境才能完整编译,想在Wind ...

  9. containerd与kubernetes集成

    kubernetes集群三步安装 概念介绍 cri (Container runtime interface) cri is a containerd plugin implementation of ...

  10. unity 四叉树管理场景

    当场景元素过多时,需要实时的显示及隐藏物体使得性能提示,但是物体那么多,怎么知道哪些物体需要显示,哪些物体不需要显示的.当然,遍历物体判断该物体是否可以显示是最容易想到的方法,但是每次更新要遍历所有物 ...