poj 1639 Picnic Planning(最小K度限制生成树)

2024-06-15 19:18

本文主要是介绍poj 1639 Picnic Planning(最小K度限制生成树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

hihoCoder挑战赛11来啦!有Tshirt作为奖品哦~

Language:
Picnic Planning
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 9563 Accepted: 3427

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



求一个无向图的最小生成树 其中有个点的度有限制(不大于K)

具体内容可以参考这篇博客: http://www.cnblogs.com/jackge/archive/2013/05/12/3073669.html

代码能力有限  只能学着别人的代码 写下 作为模版使用

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#include <map>#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read()
{char c = getchar();while (c < '0' || c > '9') c = getchar();int x = 0;while (c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}return x;
}void Print(int a)
{if(a>9)Print(a/10);putchar(a%10+'0');
}
const int N=30;
struct node
{int v,cap;node() {}node(int _v,int _cap):v(_v),cap(_cap){}bool operator < (const node &a)const{return cap>a.cap;}
};
map<string,int> mp;
int g[N][N],dis[N],clo[N],pre[N],fst[N],max_side[N];
int n,m,k;//点的数量 边的数量 指定点度数int Prim(int src,int id)
{priority_queue<node> q;while(!q.empty())q.pop();dis[src]=0;q.push(node(src,0));int ans=0;while(!q.empty()){node cur=q.top(); q.pop();int u=cur.v;if(!clo[u]){clo[u]=id;ans+=dis[u];for(int i=1;i<n;i++){if(!clo[i]&&g[u][i]!=0&&dis[i]>g[u][i])//满足松弛条件{pre[i]=u;dis[i]=g[u][i];q.push(node(i,dis[i]));}}}}return ans;
}void update(int cur,int last,int maxside)//dfs过程 直到搜回到起点 并完成max_sidegengxin
{max_side[cur]=maxside>g[cur][last]?maxside:g[cur][last];for(int i=1;i<n;i++){if(i!=last&&g[cur][i]!=0&&(pre[cur]==i||pre[i]==cur))update(i,cur,max_side[cur]);}
}void Solve()
{int res,cnt;for(int i=0;i<n;i++){dis[i]=INF;clo[i]=pre[i]=fst[i]=0;}res=0; cnt=1;   //除去根节点后,图中连通子图个数 即最小生成树个数for(int i=1;i<n;i++)if(!clo[i])res+=Prim(i,cnt++);for(int i=1;i<n;i++)//找到每个生成树和Park最近的点使之和Park相连{int id=clo[i];if(g[0][i]!=0&&(!fst[id]||g[0][i]<g[0][fst[id]]))fst[id]=i;}for(int i=1;i<cnt;i++)//把m个生成树上和根节点相连的边加入res 得到关于Park的最小m度生成树{res+=g[0][fst[i]];g[0][fst[i]]=g[fst[i]][0]=0;update(fst[i],0,0);}/*添删操作:将根节点和生成树中一个点相连,会产生一个环,将这个环上(除刚添的那条边外)权值最大的边删去.由于每次操作都会给总权值带来影响 d=max_side[tmp]-mat[0][tmp],我们需要得到最小生成树,所以我们就要求 d 尽量大*/k=k-cnt+1; //接下来重复操作 直到度数满足条件while(k--){int tmp=0;for(int i=1;i<n;i++)//找d值最大的点(就是完成增删操作之后可以使总边权减小的值最大)if(g[0][i]!=0&&(tmp==0||max_side[tmp]-g[0][tmp]<max_side[i]-g[0][i]))tmp=i;if(max_side[tmp]<=g[0][tmp])//总权值无法再减小break;res=res-max_side[tmp]+g[0][tmp];g[0][tmp]=g[tmp][0]=0;int p=0;for(int i=tmp;pre[i]!=0;i=pre[i])if(p==0||g[p][pre[p]]<g[i][pre[i]])p=i;pre[p]=0;update(tmp,0,0);}printf("Total miles driven: %d\n",res);
}int main()
{
//    fread;char s1[20],s2[20];int cap;while(scanf("%d",&m)!=EOF){mp["Park"]=0;n=1;MEM(g,0);while(m--){scanf("%s %s %d",s1,s2,&cap);if(!mp.count(s1))//如果键存在返回1,否则返回0mp[s1]=n++;if(!mp.count(s2))mp[s2]=n++;int u=mp[s1],v=mp[s2];if(!g[u][v]||g[u][v]>cap)//u v之间还没建边或者之间的距离大于capg[u][v]=g[v][u]=cap;}scanf("%d",&k);Solve();}return 0;
}



这篇关于poj 1639 Picnic Planning(最小K度限制生成树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1064340

相关文章

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

python如何生成指定文件大小

《python如何生成指定文件大小》:本文主要介绍python如何生成指定文件大小的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python生成指定文件大小方法一(速度最快)方法二(中等速度)方法三(生成可读文本文件–较慢)方法四(使用内存映射高效生成

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

MybatisX快速生成增删改查的方法示例

《MybatisX快速生成增删改查的方法示例》MybatisX是基于IDEA的MyBatis/MyBatis-Plus开发插件,本文主要介绍了MybatisX快速生成增删改查的方法示例,文中通过示例代... 目录1 安装2 基本功能2.1 XML跳转2.2 代码生成2.2.1 生成.xml中的sql语句头2

Nginx 413修改上传文件大小限制的方法详解

《Nginx413修改上传文件大小限制的方法详解》在使用Nginx作为Web服务器时,有时会遇到客户端尝试上传大文件时返回​​413RequestEntityTooLarge​​... 目录1. 理解 ​​413 Request Entity Too Large​​ 错误2. 修改 Nginx 配置2.1

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

SpringBoot实现二维码生成的详细步骤与完整代码

《SpringBoot实现二维码生成的详细步骤与完整代码》如今,二维码的应用场景非常广泛,从支付到信息分享,二维码都扮演着重要角色,SpringBoot是一个非常流行的Java基于Spring框架的微... 目录一、环境搭建二、创建 Spring Boot 项目三、引入二维码生成依赖四、编写二维码生成代码五

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

PyQt5+Python-docx实现一键生成测试报告

《PyQt5+Python-docx实现一键生成测试报告》作为一名测试工程师,你是否经历过手动填写测试报告的痛苦,本文将用Python的PyQt5和python-docx库,打造一款测试报告一键生成工... 目录引言工具功能亮点工具设计思路1. 界面设计:PyQt5实现数据输入2. 文档生成:python-

Windows系统宽带限制如何解除?

《Windows系统宽带限制如何解除?》有不少用户反映电脑网速慢得情况,可能是宽带速度被限制的原因,只需解除限制即可,具体该如何操作呢?本文就跟大家一起来看看Windows系统解除网络限制的操作方法吧... 有不少用户反映电脑网速慢得情况,可能是宽带速度被限制的原因,只需解除限制即可,具体该如何操作呢?本文