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

10
Alphonzo Bernardo 32
Alphonzo Park 57
Alphonzo Eduardo 43
Bernardo Park 19
Bernardo Clemenzi 82
Clemenzi Park 65
Clemenzi Herb 90
Clemenzi Eduardo 109
Park Herb 24
Herb Eduardo 79
3

Sample Output

Total miles driven: 183

题意:park最多连接k次,求一个最小不超过k度的生成树
思路:先将park点排除,构造生成树(可能是森林,s个联通块),再将park点加入,使之前的联通块联通
然后park点还剩k-s条边可以外连,这时候当我们再次任意连接其外的任何一点,都会形成一个环,那么就应该去除该环内
权值最大的边,利用dfs扫描各个联通块,记录从1到该点的路径中最大的一条的编号和权值。之后就进行删除操作,就此重复
 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<iostream>
using namespace std; map<string,int>mp;
struct Node
{
int x,y,val;
}node[],dist[];
int fa[];
struct E
{
int y,next,val;
}edge[];
int n,cnt,tot,head[],k,tot2;
bool maps[][];
void add(int x,int y,int val)
{
edge[++tot].y=y;
edge[tot].val=val;
edge[tot].next=head[x];
head[x]=tot;
} bool cmp(Node a,Node b)
{
return a.val < b.val;
} int Find(int x)
{
return fa[x]==x?x:fa[x]=Find(fa[x]);
} void dfs(int s,int pre)
{
for(int i=head[s];i;i=edge[i].next)
{
int to = edge[i].y;
if(maps[s][to] && !dist[to].val)
{
if(edge[i].val < dist[s].val)dist[to] = dist[s];
else
{
dist[to].val = edge[i].val;
dist[to].y = to;
dist[to].x=s;
}
dfs(to,s);
}
}
}
int main()
{
cin>>n;
memset(maps,,sizeof(maps));
mp["Park"] = ;
tot = tot2 = ;
cnt = ;
for(int i=;i<=n;i++)
{
string name1,name2;
int val;
cin>>name1>>name2>>val;
if(!mp[name1])mp[name1] = ++cnt;
if(!mp[name2])mp[name2] = ++cnt;
add(mp[name1],mp[name2],val);
node[++tot2].x=mp[name1];
node[tot2].y=mp[name2];
node[tot2].val=val;
add(mp[name2],mp[name1],val);
}
for(int i=;i<=cnt;i++)fa[i]=i;
sort(node+,node++tot2,cmp);
scanf("%d",&k);
int ans=;
for(int i=;i<=tot2;i++)
{
int x=node[i].x;
int y=node[i].y;
if(x == || y == )continue;
int fx=Find(x);
int fy=Find(y);
if(fx != fy)
{
maps[x][y] = maps[y][x] = ;
fa[fx]=fy;
ans += node[i].val;
}
}
for(int i=;i<=tot2;i++)
{
int x=node[i].x;
int y=node[i].y;
if(x != && y != )continue;
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
{
maps[x][y] = maps[y][x] = ;
fa[fx]=fy;
ans+=node[i].val;
k--;
}
}
while(k--)
{
memset(dist,,sizeof(dist));
dfs(,);
int minn = 0x3f3f3f3f;
int id=;
for(int i=head[];i;i=edge[i].next)
{
int to = edge[i].y;
if(maps[][to])continue;
if(minn > edge[i].val - dist[to].val)
{
minn = edge[i].val - dist[to].val;
id = i;
}
}
if(minn >=)break;
int to = edge[id].y;
maps[][to] = maps[to][] = ;
maps[dist[to].x][dist[to].y] = maps[dist[to].y][dist[to].x] = ;
ans += minn;
}
printf("Total miles driven: %d\n",ans);
}

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. Angular+Ionic+RSA实现后端加密前端解密功能

    因业务需要,需要给android应用安装证书,通过读取证书文件内容实现某些功能的控制: 流程:后台通过publicKey对指定内容的文件进行加密,生成文件共客户下载,客户下载后选择该证书文件读取到应用 ...

  2. 浅谈 JSP & Servlet

    body { text-align: center; } div.develon { background-color: #cccccc; font-size: 20px; } 背景 相信大家都见过这 ...

  3. Aras 发布Web Services

    https://blog.csdn.net/plm888/article/details/10890173

  4. Visual Studio Code Shortcuts

    https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf https://code.visualstudio.com ...

  5. windows 下的 Rsync 同步

    整理一下 windows 下的 rsync 文件同步. Rsync下载地址: 链接:https://pan.baidu.com/s/1nL0Ee_u76ytWKUFMeiKDIw 提取码:52in 一 ...

  6. 按Ctrl+Enter发送的实现

    按Ctrl+Enter发送 1, 监听textarea的onkeydown事件 <textarea tabindex="1" class="ie6ta" ...

  7. SSH整合 pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  8. Spring Cloud微服务集成配置中心

    1. 搭建Spring Cloud Config配置中心(见上一篇博客) 2. 创建微服务项目bounter-simon-app,pom文件如下: <?xml version="1.0 ...

  9. git branch 分支

    几乎所有的版本控制系统都以某种形式支持分支. 使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线. 在很多版本控制系统中,这是一个略微低效的过程——常常需要完全创建一个源代码目录的副 ...

  10. openwrt查看flash、RAM、CPU信息

    1.查看Flash容量大小(存储空间,可以理解为电脑的硬盘) root@OpenWrt:/# dmesg |grep spi |grep Kbytes  #查看Flash容量[    0.660000 ...