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

Description

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

思路
一开始在网上搜索题解,照着他们的算法写,写完了才发现,他们有最重要的一步没有讲,幸好此时峰巨告诉了我算法的全过程,orz orz orz。 算法:
1.无视Park及其它的边,建立最小生成树(森林)。
2.选择park到每个树的最短边,与树相连。
3.此时,park到每棵树还剩了一些边,枚举他们,每一条边加进去都会有一个环,删去环内的最大边。枚举的时候不要真实操作(或者操作后还原),而是记录他们的值,选择最大的,再进行删边操作。
4.重复第三步(k-第一步最小生成树的数目)次 博主水平不高,如需帮助,请在下方留言
 #include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int inf = ;
int mp[][],pp;
int dis[],disx[],n,k,kk;
int book[],flag[][];
char name[][];
int e1,e2,f[];
bool vis[];
int fo[];
struct node
{
int pio;
int wei;
int ex1,ex2;
}getr[],exa; int prim(int k)
{
dis[k]=;
int ans=; while(true){
int t=;
for(int i=;i<=n;i++){
if(!book[i]&&dis[i]<dis[t]){
t=i;
}
}
if(t==){break;}
ans+=dis[t];book[t]=k;flag[disx[t]][t]=flag[t][disx[t]]=true;
for(int i=;i<=n;i++){
if(!book[i]&&dis[i]>mp[t][i]){
dis[i]=mp[t][i];
disx[i]=t;
}
} }
return ans;
} void init()
{
for(int i=;i<=;i++){
for(int j=;j<=;j++){
mp[i][j]=inf;
}
}
for(int i=;i<=;i++){
dis[i]=inf;
}
} void scan()
{
int s,x,y;
scanf("%d",&n);
char a[],b[];
int t=;
init();
for(int i=;i<=n;i++){
scanf("%s%s%d",a,b,&s);
x=y=-;
for(int j=;j<=t;j++){
if(!strcmp(name[j],a)){x=j;}
if(!strcmp(name[j],b)){y=j;}
}
if(x==-){x=++t;strcpy(name[t],a);}
if(y==-){y=++t;strcpy(name[t],b);}
mp[x][y]=mp[y][x]=s;
} scanf("%d",&k);
n=t;
} int dfs(int p)//找出环内最大边
{
vis[p]=true;
int ans=-,op; for(int i=;i<=n;i++){
if(flag[i][p]&&i==pp&&f[p]!=i){e1=i;e2=p;return mp[i][p];}
if(!vis[i]&&flag[i][p]){
f[i]=p;
op=dfs(i); if(op!=-){
if(op<mp[i][p]){e1=i;e2=p;return mp[i][p];}
else return op;
}
}
}
return ans;
} int solve(int p)
{ int tx=,ans=;pp=p;
for(int i=;i<=kk;i++){
tx=;
memset(getr,,sizeof(getr));
// cout<<endl;
// cout<<"第"<<i<<"次轮回"<<endl;
for(int j=;j<=n;j++){
if(flag[p][j]||mp[p][j]==inf){continue;}
memset(vis,,sizeof(vis));
flag[p][j]=flag[j][p]=true;
// cout<<"新增的边 "<<j<<"--"<<p<<endl;
int yj=dfs(p);
// cout<<"删除边的长度 "<<yj<<endl;
getr[tx].pio=j;getr[tx].wei=mp[p][j]-yj;
getr[tx].ex1=e1;getr[tx].ex2=e2;
tx++;
flag[p][j]=flag[j][p]=false;//还原
}
exa.pio=;exa.wei=inf;
for(int i=;i<n;i++){
if(exa.wei>getr[i].wei){exa=getr[i];}
}
// cout<<"最终的决定 "<<exa.ex1<<" "<<exa.ex2<<" "<<exa.pio<<" "<<exa.wei<<endl;
ans+=min(exa.wei,);
flag[p][exa.pio]=flag[exa.pio][p]=true;
flag[exa.ex1][exa.ex2]=flag[exa.ex2][exa.ex1]=false;
}
return ans;
} int main()
{
scan();
int p;
for(int i=;i<=n;i++){
if(!strcmp(name[i],"Park")){p=i;break;}
}
int m=,ans=;
book[p]=p;
for(int i=;i<=n;i++){
if(!book[i]){
m++;
ans+=prim(i);
}
}
// cout<<"初次最小生成树 "<<ans<<endl;
kk=k;
for(int i=;i<=m;i++){
int minn=inf,ss=-;;
for(int j=;j<=n;j++){
if(!flag[p][j]&&!vis[book[j]]&&mp[p][j]!=inf){
if(mp[j][p]<minn){minn=mp[j][p];ss=j;}
}
}
if(minn!=inf){
kk--;
ans+=mp[p][ss];vis[book[ss]]=true;
flag[p][ss]=flag[ss][p]=true;
}
}
// cout<<"最小m度生成树 "<<ans<<endl;
printf("Total miles driven: %d\n",ans+solve(p));
}

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

  1. [POJ 1639] Picnic Planning

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

  2. Picnic Planning POJ - 1639(最小k度生成树)

    The Contortion Brothers are a famous set of circus clowns, known worldwide for their incredible abil ...

  3. POJ 1639 Picnic Planning:最小度限制生成树

    题目链接:http://poj.org/problem?id=1639 题意: 给你一个无向图,n个节点,m条边,每条边有边权. 让你求一棵最小生成树,同时保证1号节点的度数<=k. 题解: 最 ...

  4. poj 1639 Picnic Planning 度限制mst

    https://vjudge.net/problem/POJ-1639 题意: 有一群人,他们要去某一个地方,每个车可以装无数个人,给出了n条路,包含的信息有路连接的地方,以及路的长度,路是双向的,但 ...

  5. POJ 1639 Picnic Planning(最小度限制生成树)

    Description The Contortion Brothers are a famous set of circus clowns, known worldwide for their inc ...

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

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

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

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

  8. poj1639 Picnic Planning 最小度数限制生成树

    题意:若干个人开车要去park聚会,可是park能停的车是有限的,为k.所以这些人要通过先开车到其它人家中,停车,然后拼车去聚会.另外,车的容量是无限的,他们家停车位也是无限的. 求开车总行程最短. ...

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

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

随机推荐

  1. python 网络编程 IO多路复用之epoll

    python网络编程——IO多路复用之epoll 1.内核EPOLL模型讲解     此部分参考http://blog.csdn.net/mango_song/article/details/4264 ...

  2. Tembin

    1:组织机构和用户之间是多对一的关系,一个组织结构可以有多个成员,一个成员只能属于一个组织机构. 2:app里面的邀请成员:是邀请发送短信通知用户注册tembin账户,当用户去注册的时候下面就会显示所 ...

  3. 安装mysql zip5.7版--安裝

    一直以来都习惯了使用MySQL安装文件(.exe),今天下载了一个.zip版本的MySQL,安装过程中遇到了一些问题,如下: 1.在MySQL官网上(http://dev.mysql.com/down ...

  4. 配置Web.config 元素CustomErrors

    一.customErrors 元素 属性 说明 defaultRedirect 指定出错时将浏览器定向到的默认 URL.如果未指定该属性,则显示一般性错误. 可选的属性. URL 可以是绝对的(如 w ...

  5. 思路:controller层:后台如何取值 前端如何给name赋值 例如是id赋值还是自己随意定义

    思路:controller层:后台如何取值 前端如何给name赋值 例如是id赋值还是自己随意定义

  6. U68641 划水(swim.pas/c/cpp)

    U68641 划水(swim.pas/c/cpp) 题目背景 小小迪带你划水. 题目描述 原题 输入输出格式 输入格式: 第一行一个数 T. 接下来 T 行每行一个数表示 n 输出格式: 输出 T 行 ...

  7. Nginx proxy_protocol协议

    L:113

  8. PHP——emjoin表情存入数据库

    前言 还有一种解决的方法是更改数据库,这里就不写了,这里直接对emoji进行转码 代码 mb_strlen() | strlen() | rawurlencode() | rawurldecode() ...

  9. hiho1258 Osu! Master

    题目链接:http://hihocoder.com/problemset/problem/1258 题目大意:看能连击的次数 思路:水 看有多少个1和s就好了 #include <stdio.h ...

  10. mysql 备份数据语句

    rem ******MySQL backup start********@echo offforfiles /p "D:\website\备份\数据库日常备份" /m backup ...