HDU - 4009 - Transfer water 朱刘算法 +建立虚拟节点
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 朱刘算法 +建立虚拟节点的更多相关文章
- HDU 4009——Transfer water——————【最小树形图、不定根】
Transfer water Time Limit:3000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64u Subm ...
- hdu 4009 Transfer water(最小型树图)
Transfer water Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)To ...
- HDU 4009 Transfer water
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...
- HDU 4009 Transfer water(最小树形图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...
- HDU 4009 Transfer water 最小树形图
分析:建一个远点,往每个点连建井的价值(单向边),其它输水线按照题意建单向边 然后以源点为根的权值最小的有向树就是答案,套最小树形图模板 #include <iostream> #incl ...
- HDUOJ--2121--Ice_cream’s world II【朱刘算法】不定根最小树形图
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2121 题意:n个顶点,m条边,求从某一点起建立有向图最小生成树而且花费最小.输出最小花费和根节点下标. ...
- hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , ...
- POJ 3164 Command Network ( 最小树形图 朱刘算法)
题目链接 Description After a long lasting war on words, a war on arms finally breaks out between littlek ...
- poj3164(最小树形图&朱刘算法模板)
题目链接:http://poj.org/problem?id=3164 题意:第一行为n, m,接下来n行为n个点的二维坐标, 再接下来m行每行输入两个数u, v,表点u到点v是单向可达的,求这个有向 ...
随机推荐
- superset安装文档
1 安装python3.6 yum install epel-release -y yum install https://centos7.iuscommunity.org/ius-release.r ...
- tab切换echarts无法正常显示问题
项目中使用到了Echarts来在展示图表,两个tab切换页面中都存在图表,页面加载完成后 对所有图表进行了初始化和绘制,然后切换查看时,发现图表的宽度不正确.,第一个tab显示是很正常的,但是第二个t ...
- 【Python-django后端开发】logging日制配置详解!!!
官方文档请查看:https://docs.djangoproject.com/en/1.11/topics/logging/ 1. 配置工程日志,在setting.py里,如下 LOGGING = { ...
- 图片验证码+session
生成随机验证码 #!/usr/bin/env python # -*- coding:utf-8 -*- import random from PIL import Image, ImageDraw, ...
- Dubbo里面线程池的拒绝策略
Dubbo里面线程池的拒绝策略 public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy { protecte ...
- JDK、JRE、JVM之间的区别和联系
JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...
- SpringBoot中读取配置文件的几种方式
1.读取application文件 在application.yml或者properties文件中添加: info: name: xiaoming age: 13 sex: 1 读取方式如下: imp ...
- java并发编程(十六)----(线程池)java线程池的使用
上节我们简单介绍了线程池,这次我们就来使用一下.Executors提供四种线程池,分别是:newCachedThreadPool,newFixedThreadPool ,newScheduledThr ...
- Mybatis学习笔记之---动态sql中标签的使用
动态Sql语句中标签的使用 (一)常用标签 1.<if> if标签通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件, 他也经常用于UPDATE语句中判断是否更新某一个字段 ...
- Spring项目集成ShiroFilter简单实现权限管理
Shiros是我们开发中常用的用来实现权限控制的一种工具包,它主要有认证.授权.加密.会话管理.与Web集成.缓存等功能.我是从事javaweb工作的,我就经常遇到需要实现权限控制的项目,之前我们都是 ...