POJ 1639
来源:互联网 发布:上瘾网络剧第十六集 编辑:程序博客网 时间:2024/06/05 21:52
Picnic Planning
Time Limit: 5000MS Memory Limit: 10000KTotal Submissions: 10742 Accepted: 3885
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.
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
East Central North America 2000
#include"map"#include"string"#include"cmath"#include"cstdio"#include"iostream"#include"cstring"#include"algorithm"#include"vector"using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 105;map <string,int> mp;string s1,s2;int cnt;int dis[maxn],fro[maxn];int side[maxn][maxn];int tail,head[maxn];int K,ans;int fa[maxn];bool vis[maxn][maxn];int getfa(int x){ if(x == fa[x]) return x; return fa[x] = getfa(fa[x]);}struct edge{ int u,v,nxt,c; edge(){} edge(int u, int v,int nxt, int c) : u(u),v(v), nxt(nxt), c(c){}}e[maxn*maxn];struct node{ int u,v,c; node(){} node(int u, int v, int c):u(u),v(v),c(c){}}dp[maxn];void init(){ ans = cnt = 0; tail = -1; mp.clear(); memset(head,-1,sizeof(head)); memset(side,0x3f,sizeof(side)); memset(vis,0,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); memset(fro,0,sizeof(fro));}void add(int u, int v, int c){ e[++tail] = edge(u,v,head[u],c); head[u] = tail;}int get_num(string s){ if(mp.find(s) == mp.end()){ mp[s] = ++cnt; } return mp[s];}bool cmp(edge a, edge b){ return a.c < b.c;}void kruskal(){ sort(e,e+tail,cmp); for(int i = 1; i<= cnt; i++) fa[i] = i; for(int i = 0; i <= tail; i++){ int u = e[i].u, v = e[i].v; if(u == 1 || v == 1) continue; int fu = getfa(u), fv = getfa(v); if(fu == fv) continue; vis[u][v] = vis[v][u] = 1; fa[fu] = fv; ans += e[i].c; }}void dfs(int u, int pre){ for(int i = 2; i <= cnt; i++) if(vis[u][i] && i!=pre){ if(dp[u].c > side[u][i]){ dp[i] = dp[u]; } else{ dp[i] = node(u,i,side[u][i]); } dfs(i,u); }}void solve(){ int m = 0; for(int i = 2; i<= cnt; i++){ if(side[1][i] != INF){ int fv = getfa(i); if(dis[fv] > side[1][i]){ dis[fv] = side[1][i]; fro[fv] = i; } } } for(int i = 2; i<= cnt; i++){ if(dis[i] != INF){ ans += dis[i]; vis[1][fro[i]] = vis[fro[i]][1] = 1; ++m; } } for(int i = m+1; i <= K; i++){ memset(dp,-1,sizeof(dp)); dfs(1,-1); int minx = 0,temp = 1; for(int j = 2; j <= cnt; j++){ if(side[1][j] != INF && !vis[1][j]){ int val = side[1][j] - dp[j].c; if(minx > val){ minx = val; temp = j; } } } if(minx >= 0) break; int u = dp[temp].u, v = dp[temp].v; vis[u][v] = vis[v][u] = 0; vis[1][temp] = 1; ans += minx; } printf("Total miles driven: %d\n",ans);}int main(){ int n,dis,u,v; scanf("%d",&n); init(); get_num("Park"); for(int i = 1; i<= n; i++){ cin>>s1>>s2; scanf("%d",&dis); u = get_num(s1); v = get_num(s2); side[u][v] = min(side[u][v],dis); side[v][u] = min(side[v][u],dis); } for(int i = 1; i <= cnt; i++){ for(int j = i+1; j <= cnt; j++){ if(side[i][j] != INF){ add(i,j,side[i][j]); add(j,i,side[j][i]); } } } scanf("%d",&K); kruskal(); solve(); return 0;}
字符串“park”所连的边不能超过k条的最小生成树。
阅读全文
0 0
- poj 1639
- poj 1639
- [POJ 1639]
- POJ 1639
- POJ 1639 Picnic Planning
- poj 1639 Picnic Planning
- poj 1639 Picnic Planning
- poj 1639 Picnic Planning
- [POJ 1639]Picnic Planning
- POJ 1639 Picnic Planning
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- java实现udp打洞,服务器转发消息
- Java 内存模型与线程
- python基础第四篇
- Go语言程序记录日志
- 浅谈 uninitialized_copy() 的参数类型
- POJ 1639
- PHP cookie管理
- JVM优化
- java 判断线程都执行完了(非线程池方式)
- js面试题(一)
- Eclipse使用Maven搭建Java Web项目并直接部署Tomcat8(全实战)
- [Android]Android内存泄漏你所要知道的一切(翻译)
- Ubuntu16.04 安装eclipse 并配置OpenCV (C/C++项目)
- flask中的上下文处理器app_context_processor