博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1639 Picnic Planning(最小度限制生成树)
阅读量:4072 次
发布时间:2019-05-25

本文共 5774 字,大约阅读时间需要 19 分钟。

链接:

题目:

Picnic Planning
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 7780   Accepted: 2726

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

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

Sample Output

Total miles driven: 183

Source

题目大意:

马戏团的小丑们有一个特异功能,无论一个车子有多小,他们都能钻进去,也就是说,一辆车子能够容纳无限个小丑。

现在小丑们要去一个公园野餐,他们住在不同的地方,为了节约路费(石油),要使得所有车子加起来走的路程最小,那么,小丑A

可以直接开车到公园,或者开到小丑B家,然后把车停在B家,搭B的车一起去公园。

小丑家的停车位是有限制的,但是公园的停车位是有限制的,公园最多只能停k辆车。一旦某个小丑开车到了公园,那么就必须停在公园,不能在回去载其他小丑了。

求所有小丑开车的最短总路程。

分析与总结:

最小生成树的拓展问题,经典的最小度限制生成树问题。

从昨晚搞到了现在,思想比较容易理解,但是代码实现起来比较复杂,而且还不是独立完成的。

参考资料:

1.这个ppt真心不错,看了基本上懂了:

     

2.IOI2004国家集训队论文--王汀《最小生成树问题的扩展》

    

3.黑书, P300~303,比较难懂

4.代码参考:

    

代码:

/*******************************************最小度限制生成树算法框架:    1. 先求出最小m度限制生成树;    2. 由最小m度限制生成树得到最小m+1度限制生成树;    3. 当dT(v0)=k时停止(即当V0的度为k的时候停止);********************************************/#include
#include
#include
#include
#include
#include
using namespace std;map
mp;const int VN = 30; // 点的数量const int EN = VN*VN; // 边的数量const int INF = 0x7fffffff;int limit;template
class Prim{public: void init(int _n){ for(int i=1; i
weight) w[u][v] = weight; //注意可能有重复边 } Type minDegreeST(int v0, int k){ // v0是限制度的点, k是限制的度数 memset(father, -1, sizeof(father)); memset(vis, 0, sizeof(vis)); memset(edge, 0, sizeof(edge)); vis[v0] = true; int m = 0; // 连通分支的个数 mst = 0; // 所求答案 /* 步骤1: 先求出m限制树 */ for(int i=1; i<=n; ++i)if(!vis[i]){ ++m; mst += prim(i, v0); } /* 步骤2: 由m限制树得到m+1限制树 */ int minAdd, a, b, tmp; int change; // 回路上权值最大的边,用于交换 for(int i=m+1; i<=k&&i<=n; ++i){ memset(best, -1, sizeof(best)); for(int j=1; j<=n; ++j)if(best[j]==-1 && father[j]!=v0){ Best(j, v0); } minAdd = INF; for(int j=1; j<=n; ++j)if(w[v0][j]!=INF && father[j]!=v0){ //遍历所有边 a = best[j]; b = father[best[j]]; tmp = w[v0][j] - w[a][b]; if(tmp < minAdd){ minAdd=tmp; change = j; } } if(minAdd >= 0) break; //用于度数不大于k的限制,如果k限制,就不用break了 mst += minAdd; a = best[change]; b = father[change]; w[a][b] = w[b][a] = INF; father[a] = b = v0; w[a][b] = w[b][a] = w[change][v0]; w[v0][change] = w[change][v0] = INF; } return mst; } private: // 拉成有根树 void dfs(int cur){ for(int i=1; i<=n; ++i)if(mark[i] && edge[i][cur]){ father[i] = cur; mark[i] = 0; dfs(i); } } // 记忆化搜索,求x到V0路径上权值最大的边 int Best(int x, int V0){ if(father[x]==V0) return -1; if(best[x] != -1){ return best[x]; } int tmp = Best(father[x], V0); if(tmp!=-1 && w[tmp][father[tmp]] > w[father[x]][x]) best[x] = tmp; else best[x] = x; return best[x]; } /* 求去掉与V0相连的边之后的连通分量的最小生成树 */ Type prim(int s, int V0){ memset(mark, false, sizeof(mark)); vis[s] = mark[s] = true; for(int i=1; i<=n; ++i){ key[i] = w[s][i]; pre[i] = s; } int sum=0; for(int i=1; i
w[u][j]){ key[j] = w[u][j]; pre[j] = u; } } } int Min = INF; int root = -1; // 树根 for(int i=1; i<=n; ++i)if(mark[i] && w[i][V0]
G; int main(){ int n, d, a, b; string name1,name2; mp["Park"] = 1; scanf("%d",&n); G.init(n); int cnt=1; for(int i=0; i
> name1 >> name2 >> d; a=mp[name1]; b=mp[name2]; if(!a)a=mp[name1]=++cnt; if(!b)b=mp[name2]=++cnt; G.insert(a,b,d); G.insert(b,a,d); } G.setVertexNum(cnt); scanf("%d",&limit); printf("Total miles driven: %d\n", G.minDegreeST(1, limit)); return 0;}

—— 生命的意义,在于赋予它意义。   
原创 , By D_Double (转载请标明)

你可能感兴趣的文章
Java8 Lambda表达式介绍
查看>>
Java8 stream流介绍
查看>>
Java多线程之synchronized及死锁编写
查看>>
Java NIO源码剖析及使用实例(一):Buffer
查看>>
[swift实战入门]手把手教你编写2048(一)
查看>>
[swift实战入门]手把手教你编写2048(二)
查看>>
Java 爬虫入门(网易云音乐和知乎实例)
查看>>
[swift实战入门]手把手教你编写2048(三)
查看>>
堆排序原理(图)及java版代码
查看>>
【JAVA数据结构】栈(数组实现)
查看>>
【JAVA数据结构】双向链表
查看>>
【JAVA数据结构】先进先出队列
查看>>
String类的intern方法随笔
查看>>
【泛型】一个简易的对象间转换的工具类(DO转VO)
查看>>
adb server version (39) doesn't match this client (40); killing...
查看>>
Unity高级游戏地编案例
查看>>
UE4地编大型开放世界~制作烘焙全流程
查看>>
TextMesh Pro不能显示中文的解决办法是创建字贴图,常用汉字3500
查看>>
permanently
查看>>
Unity2019游戏框架搭建第一季C# 核心知识与简易框架搭建 + Unity2019 游戏框架搭建第二季:UI 模块与资源模块持续精进...
查看>>