The Contortion Brothers are a famous set of circus clowns, known worldwide for their incredible ability to cram an unlimited number of themselves into even the smallest vehicle. During the off-season, the brothers like to get together for an Annual Contortionists Meeting at a local park. However, the brothers are not only tight with regard to cramped quarters, but with money as well, so they try to find the way to get everyone to the party which minimizes the number of miles put on everyone's cars (thus saving gas, wear and tear, etc.). To this end they are willing to cram themselves into as few cars as necessary to minimize the total number of miles put on all their cars together. This often results in many brothers driving to one brother's house, leaving all but one car there and piling into the remaining one. There is a constraint at the park, however: the parking lot at the picnic site can only hold a limited number of cars, so that must be factored into the overall miserly calculation. Also, due to an entrance fee to the park, once any brother's car arrives at the park it is there to stay; he will not drop off his passengers and then leave to pick up other brothers. Now for your average circus clan, solving this problem is a challenge, so it is left to you to write a program to solve their milage minimization problem.

Input

Input will consist of one problem instance. The first line will contain a single integer n indicating the number of highway connections between brothers or between brothers and the park. The next n lines will contain one connection per line, of the form name1 name2 dist, where name1 and name2 are either the names of two brothers or the word Park and a brother's name (in either order), and dist is the integer distance between them. These roads will all be 2-way roads, and dist will always be positive.The maximum number of brothers will be 20 and the maximumlength of any name will be 10 characters.Following these n lines will be one final line containing an integer s which specifies the number of cars which can fit in the parking lot of the picnic site. You may assume that there is a path from every brother's house to the park and that a solution exists for each problem instance.

Output

Output should consist of one line of the form
Total miles driven: xxx

where xxx is the total number of miles driven by all the brothers' cars.

Sample Input

  1. 10
  2. Alphonzo Bernardo 32
  3. Alphonzo Park 57
  4. Alphonzo Eduardo 43
  5. Bernardo Park 19
  6. Bernardo Clemenzi 82
  7. Clemenzi Park 65
  8. Clemenzi Herb 90
  9. Clemenzi Eduardo 109
  10. Park Herb 24
  11. Herb Eduardo 79
  12. 3

Sample Output

  1. Total miles driven: 183
  2.  
  3. 题意:park最多连接k次,求一个最小不超过k度的生成树
    思路:先将park点排除,构造生成树(可能是森林,s个联通块),再将park点加入,使之前的联通块联通
    然后park点还剩k-s条边可以外连,这时候当我们再次任意连接其外的任何一点,都会形成一个环,那么就应该去除该环内
    权值最大的边,利用dfs扫描各个联通块,记录从1到该点的路径中最大的一条的编号和权值。之后就进行删除操作,就此重复
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cmath>
  5. #include<queue>
  6. #include<map>
  7. #include<iostream>
  8. using namespace std;
  9.  
  10. map<string,int>mp;
  11. struct Node
  12. {
  13. int x,y,val;
  14. }node[],dist[];
  15. int fa[];
  16. struct E
  17. {
  18. int y,next,val;
  19. }edge[];
  20. int n,cnt,tot,head[],k,tot2;
  21. bool maps[][];
  22. void add(int x,int y,int val)
  23. {
  24. edge[++tot].y=y;
  25. edge[tot].val=val;
  26. edge[tot].next=head[x];
  27. head[x]=tot;
  28. }
  29.  
  30. bool cmp(Node a,Node b)
  31. {
  32. return a.val < b.val;
  33. }
  34.  
  35. int Find(int x)
  36. {
  37. return fa[x]==x?x:fa[x]=Find(fa[x]);
  38. }
  39.  
  40. void dfs(int s,int pre)
  41. {
  42. for(int i=head[s];i;i=edge[i].next)
  43. {
  44. int to = edge[i].y;
  45. if(maps[s][to] && !dist[to].val)
  46. {
  47. if(edge[i].val < dist[s].val)dist[to] = dist[s];
  48. else
  49. {
  50. dist[to].val = edge[i].val;
  51. dist[to].y = to;
  52. dist[to].x=s;
  53. }
  54. dfs(to,s);
  55. }
  56. }
  57. }
  58. int main()
  59. {
  60. cin>>n;
  61. memset(maps,,sizeof(maps));
  62. mp["Park"] = ;
  63. tot = tot2 = ;
  64. cnt = ;
  65. for(int i=;i<=n;i++)
  66. {
  67. string name1,name2;
  68. int val;
  69. cin>>name1>>name2>>val;
  70. if(!mp[name1])mp[name1] = ++cnt;
  71. if(!mp[name2])mp[name2] = ++cnt;
  72. add(mp[name1],mp[name2],val);
  73. node[++tot2].x=mp[name1];
  74. node[tot2].y=mp[name2];
  75. node[tot2].val=val;
  76. add(mp[name2],mp[name1],val);
  77. }
  78. for(int i=;i<=cnt;i++)fa[i]=i;
  79. sort(node+,node++tot2,cmp);
  80. scanf("%d",&k);
  81. int ans=;
  82. for(int i=;i<=tot2;i++)
  83. {
  84. int x=node[i].x;
  85. int y=node[i].y;
  86. if(x == || y == )continue;
  87. int fx=Find(x);
  88. int fy=Find(y);
  89. if(fx != fy)
  90. {
  91. maps[x][y] = maps[y][x] = ;
  92. fa[fx]=fy;
  93. ans += node[i].val;
  94. }
  95. }
  96. for(int i=;i<=tot2;i++)
  97. {
  98. int x=node[i].x;
  99. int y=node[i].y;
  100. if(x != && y != )continue;
  101. int fx=Find(x);
  102. int fy=Find(y);
  103. if(fx!=fy)
  104. {
  105. maps[x][y] = maps[y][x] = ;
  106. fa[fx]=fy;
  107. ans+=node[i].val;
  108. k--;
  109. }
  110. }
  111. while(k--)
  112. {
  113. memset(dist,,sizeof(dist));
  114. dfs(,);
  115. int minn = 0x3f3f3f3f;
  116. int id=;
  117. for(int i=head[];i;i=edge[i].next)
  118. {
  119. int to = edge[i].y;
  120. if(maps[][to])continue;
  121. if(minn > edge[i].val - dist[to].val)
  122. {
  123. minn = edge[i].val - dist[to].val;
  124. id = i;
  125. }
  126. }
  127. if(minn >=)break;
  128. int to = edge[id].y;
  129. maps[][to] = maps[to][] = ;
  130. maps[dist[to].x][dist[to].y] = maps[dist[to].y][dist[to].x] = ;
  131. ans += minn;
  132. }
  133. printf("Total miles driven: %d\n",ans);
  134. }
  1.  

Picnic Planning POJ - 1639(最小k度生成树)的更多相关文章

  1. POJ 1639 Picnic Planning 最小k度生成树

    Picnic Planning Time Limit: 5000MS   Memory Limit: 10000K Total Submissions:11615   Accepted: 4172 D ...

  2. poj 1639 最小k度限制生成树

    题目链接:https://vjudge.net/problem 题意: 给各位看一下题意,算法详解看下面大佬博客吧,写的很好. 参考博客:最小k度限制生成树 - chty - 博客园  https:/ ...

  3. Picnic Planning POJ - 1639(度限制生成树)

    解题报告   题意理解 给定一张N个点,M个边的无向图,求出无向图的一颗最小生成树,但是我们要求一号节点的入度不可以超过给定的整数S 也就是一个最小生成树,要求它的一号节点,最多只能和S个节点相连. ...

  4. 【POJ 1639】 Picnic Planning (最小k度限制生成树)

    [题意] 有n个巨人要去Park聚会.巨人A和先到巨人B那里去,然后和巨人B一起去Park.B君是个土豪,他家的停车场很大,可以停很多车,但是Park的停车场是比较小.只能停k辆车.现在问你在这个限制 ...

  5. 最小k度限制生成树

    [题目描述] 给你一个图,n个点,m条边,求一颗生成树满足如下条件: (1)结点1的度不超过k. (2)在(1)条件下所求生成树最小. [算法引入] 最小k度限制生成树,就是指有特殊的某一点的度不能超 ...

  6. [POJ 1639] Picnic Planning

    [题目链接] http://poj.org/problem?id=1639 [算法] 首先,我们可以用深度优先遍历求出1号节点去除后有几个联通块 设共有T个联通块,若T > K则无解,否则 : ...

  7. poj1639 Picnic Planning,K度限制生成树

    题意: 矮人虽小却喜欢乘坐巨大的轿车,车大到能够装下不管多少矮人.某天,N(N≤20)个矮人打算到野外聚餐.为了集中到聚餐地点,矮人A 要么开车到矮人B 家中,留下自己的轿车在矮人B 家,然后乘坐B ...

  8. poj1639,uva1537,uvalive2099,scu1622,fzu1761 Picnic Planning (最小限制生成树)

    Picnic Planning Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 10742   Accepted: 3885 ...

  9. K度限制MST poj 1639

    /* k度限制MST:有一个点的度<=k的MST poj 1639 要求1号点的度不超过k 求MST 我们先把1号点扔掉 跑MST 假设有sum个连通分支 然后把这sum个分支连到1上 就得到了 ...

随机推荐

  1. 【JS】前端文件下载(无刷新)方法总结

    #传统方法 利用iframe 或 form.submit 或 windows.open直接向后端发请求,后端返回文件流,后端处理成功后会直接返回到页面,浏览器会整理并打开自己的保存下载文件机制 . 1 ...

  2. 为vue添加公用方法,vue添加通用方法

    common.js var common =function() { return{ f1:function(){ console.log("this is common f1().&quo ...

  3. react图工具集成

    背景 调查了react下的图工具库, 并继承到项目中, 经过调研列出如下两个图工具库,可以同时使用. data-ui react-c3js 在一个工具中没有所需的图时候, 可以使用另一个替代. dat ...

  4. [Reinforcement Learning] 动态规划(Planning)

    动态规划 动态规划(Dynamic Programming,简称DP)是一种通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法. 动态规划常常适用于具有如下性质的问题: 具有最优子结构(Opt ...

  5. 关于接口(Interface)

    接口,其实是指类之间约定的协议,可以包含方法.属性.事件和索引: 接口成员不允许使用访问修饰符号(public.private.protected.internal),所有的接口成员都是公共的. 接口 ...

  6. LOJ #6539 奇妙数论题

    不想咕太久..就随便找个题更一下 LOJ#6539 题意 求题面里那个式子 题解 有一个常用的小式子 $$\sum_{x|a,x|b}\varphi(x)=\gcd(a,b)$$ 用这个式子直接对题面 ...

  7. kafka单机安装和启动

    1.下载并解压到/usr/local/src目录下 2.运行kafka需要使用Zookeeper,先启动Zookeeper,如果没有Zookeeper,可以使用kafka自带打包和配置好的Zookee ...

  8. Sping 里面的适配器模式的实现

    适配器模式----------设计模式最近在看SpringMVC源码,从中看到了比较优秀的设计模式所以来分享下. 1.适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口,Adap ...

  9. 【转】一文掌握 Linux 性能分析之网络篇

    [转]一文掌握 Linux 性能分析之网络篇 比较宽泛地讲,网络方向的性能分析既包括主机测的网络配置查看.监控,又包括网络链路上的包转发时延.吞吐量.带宽等指标分析.包括但不限于以下分析工具: pin ...

  10. c# Lamdba及DataTable AsEnumerable()的使用

    Lamdba是延迟执行的,实际上什么都没有发生,当真正使用对象的时候(例如调用:First, Single, ToList-.的时候)才执行. 1.Where var var_dtTable = dt ...