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

题意:

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

思路:

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

  1. #include <algorithm>
  2. #include <iterator>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <iomanip>
  7. #include <bitset>
  8. #include <cctype>
  9. #include <cstdio>
  10. #include <string>
  11. #include <vector>
  12. #include <stack>
  13. #include <cmath>
  14. #include <queue>
  15. #include <list>
  16. #include <map>
  17. #include <set>
  18. #include <cassert>
  19.  
  20. using namespace std;
  21. //#pragma GCC optimize(3)
  22. //#pragma comment(linker, "/STACK:102400000,102400000") //c++
  23. #define lson (l , mid , rt << 1)
  24. #define rson (mid + 1 , r , rt << 1 | 1)
  25. #define debug(x) cerr << #x << " = " << x << "\n";
  26. #define pb push_back
  27. #define pq priority_queue
  28.  
  29. typedef long long ll;
  30. typedef unsigned long long ull;
  31.  
  32. typedef pair<ll ,ll > pll;
  33. typedef pair<int ,int > pii;
  34. typedef pair<int,pii> p3;
  35.  
  36. //priority_queue<int> q;//这是一个大根堆q
  37. //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
  38. #define fi first
  39. #define se second
  40. //#define endl '\n'
  41.  
  42. #define OKC ios::sync_with_stdio(false);cin.tie(0)
  43. #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
  44. #define REP(i , j , k) for(int i = j ; i < k ; ++i)
  45. //priority_queue<int ,vector<int>, greater<int> >que;
  46.  
  47. const ll mos = 0x7FFFFFFF; //
  48. const ll nmos = 0x80000000; //-2147483648
  49. const int inf = 0x3f3f3f3f;
  50. const ll inff = 0x3f3f3f3f3f3f3f3f; //
  51. const int mod = ;
  52. const double esp = 1e-;
  53. const double PI=acos(-1.0);
  54.  
  55. template<typename T>
  56. inline T read(T&x){
  57. x=;int f=;char ch=getchar();
  58. while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
  59. while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
  60. return x=f?-x:x;
  61. }
  62.  
  63. /*-----------------------showtime----------------------*/
  64. const int maxm = 1e6+;
  65. const int maxn = 1e3+;
  66. struct edge
  67. {
  68. int from,to,c;
  69. }e[maxm];
  70. int X,Y,Z,n,tot;
  71. struct node
  72. {
  73. int x,y,z;
  74. }p[maxn];
  75. int in[maxn],vis[maxn],pre[maxn],id[maxn];
  76. int rtt;
  77. int zhuliu(int root,int n,int m){
  78. int res = ;
  79. while(true){
  80. memset(in, inf, sizeof(in));
  81. for(int i=; i<=m ;i++){
  82. if(e[i].from != e[i].to && e[i].c < in[e[i].to]){
  83. pre[e[i].to] = e[i].from;
  84. in[e[i].to] = e[i].c;
  85. }
  86. }
  87. for(int i=; i<=n; i++){
  88. if(i!=root && in[i] == inf)
  89. return -;
  90. }
  91. int tn = ,v;
  92. memset(id,-,sizeof(id));
  93. memset(vis,-,sizeof(vis));
  94. in[root] = ;
  95. for(int i=; i<=n; i++){
  96. res += in[i];
  97. v = i;
  98. while(v!=root && id[v] == - && vis[v] != i){
  99. vis[v] = i;
  100. v = pre[v];
  101. }
  102. if(v!=root && id[v] == -){
  103. id[v] = ++tn;
  104. for(int u = pre[v] ; u!=v; u = pre[u]){
  105. id[u] = tn;
  106. }
  107. }
  108. }
  109. if(tn == )break;
  110. for(int i=; i<=n; i++){
  111. if(id[i] == -)id[i] = ++tn;
  112. }
  113.  
  114. for(int i=; i<=m; i++){
  115. int v = e[i].to;
  116. e[i].to = id[e[i].to];
  117. e[i].from = id[e[i].from];
  118. if(e[i].to != e[i].from){
  119. e[i].c -= in[v];
  120. }
  121. }
  122. n = tn;root = id[root];
  123. }
  124. return res;
  125. }
  126. int get(int u,int v){
  127. int ans = abs(p[u].x - p[v].x) +abs(p[u].y - p[v].y)+abs(p[u].z - p[v].z);
  128. ans = ans * Y;
  129. if(p[v].z > p[u].z)ans += Z;
  130. return ans;
  131. }
  132. int main(){
  133. while(~scanf("%d%d%d%d", &n, &X,&Y,&Z) && n+X+Y+Z){
  134. tot = ;
  135. for(int i=; i<=n; i++){
  136. scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].z);
  137. }
  138. for(int i=; i<=n; i++){
  139. int x,k;scanf("%d", &k);
  140. for(int j=; j<=k; j++)
  141. {
  142. scanf("%d", &x);
  143. e[++tot].from = i;
  144. e[tot].to = x;
  145. e[tot].c = get(i,x);
  146. }
  147. }
  148.  
  149. for(int i=; i<=n; i++){
  150. e[++tot].from = ;
  151. e[tot].to = i;
  152. e[tot].c = X * p[i].z;
  153. }
  154.  
  155. int ans = zhuliu(,n,tot);
  156. printf("%d\n", ans);
  157. }
  158. return ;
  159. }

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. superset安装文档

    1 安装python3.6 yum install epel-release -y yum install https://centos7.iuscommunity.org/ius-release.r ...

  2. tab切换echarts无法正常显示问题

    项目中使用到了Echarts来在展示图表,两个tab切换页面中都存在图表,页面加载完成后 对所有图表进行了初始化和绘制,然后切换查看时,发现图表的宽度不正确.,第一个tab显示是很正常的,但是第二个t ...

  3. 【Python-django后端开发】logging日制配置详解!!!

    官方文档请查看:https://docs.djangoproject.com/en/1.11/topics/logging/ 1. 配置工程日志,在setting.py里,如下 LOGGING = { ...

  4. 图片验证码+session

    生成随机验证码 #!/usr/bin/env python # -*- coding:utf-8 -*- import random from PIL import Image, ImageDraw, ...

  5. Dubbo里面线程池的拒绝策略

    Dubbo里面线程池的拒绝策略 public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy { protecte ...

  6. JDK、JRE、JVM之间的区别和联系

    JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...

  7. SpringBoot中读取配置文件的几种方式

    1.读取application文件 在application.yml或者properties文件中添加: info: name: xiaoming age: 13 sex: 1 读取方式如下: imp ...

  8. java并发编程(十六)----(线程池)java线程池的使用

    上节我们简单介绍了线程池,这次我们就来使用一下.Executors提供四种线程池,分别是:newCachedThreadPool,newFixedThreadPool ,newScheduledThr ...

  9. Mybatis学习笔记之---动态sql中标签的使用

    动态Sql语句中标签的使用 (一)常用标签 1.<if> if标签通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件, 他也经常用于UPDATE语句中判断是否更新某一个字段 ...

  10. Spring项目集成ShiroFilter简单实现权限管理

    Shiros是我们开发中常用的用来实现权限控制的一种工具包,它主要有认证.授权.加密.会话管理.与Web集成.缓存等功能.我是从事javaweb工作的,我就经常遇到需要实现权限控制的项目,之前我们都是 ...