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

相关文章

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

Python使用python-pptx自动化操作和生成PPT

《Python使用python-pptx自动化操作和生成PPT》这篇文章主要为大家详细介绍了如何使用python-pptx库实现PPT自动化,并提供实用的代码示例和应用场景,感兴趣的小伙伴可以跟随小编... 目录使用python-pptx操作PPT文档安装python-pptx基础概念创建新的PPT文档查看

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Python实现数据可视化图表生成(适合新手入门)

《Python实现数据可视化图表生成(适合新手入门)》在数据科学和数据分析的新时代,高效、直观的数据可视化工具显得尤为重要,下面:本文主要介绍Python实现数据可视化图表生成的相关资料,文中通过... 目录前言为什么需要数据可视化准备工作基本图表绘制折线图柱状图散点图使用Seaborn创建高级图表箱线图热

基于Python实现数字限制在指定范围内的五种方式

《基于Python实现数字限制在指定范围内的五种方式》在编程中,数字范围限制是常见需求,无论是游戏开发中的角色属性值、金融计算中的利率调整,还是传感器数据处理中的异常值过滤,都需要将数字控制在合理范围... 目录引言一、基础条件判断法二、数学运算巧解法三、装饰器模式法四、自定义类封装法五、NumPy数组处理

SQLServer中生成雪花ID(Snowflake ID)的实现方法

《SQLServer中生成雪花ID(SnowflakeID)的实现方法》:本文主要介绍在SQLServer中生成雪花ID(SnowflakeID)的实现方法,文中通过示例代码介绍的非常详细,... 目录前言认识雪花ID雪花ID的核心特点雪花ID的结构(64位)雪花ID的优势雪花ID的局限性雪花ID的应用场景

Django HTTPResponse响应体中返回openpyxl生成的文件过程

《DjangoHTTPResponse响应体中返回openpyxl生成的文件过程》Django返回文件流时需通过Content-Disposition头指定编码后的文件名,使用openpyxl的sa... 目录Django返回文件流时使用指定文件名Django HTTPResponse响应体中返回openp

go动态限制并发数量的实现示例

《go动态限制并发数量的实现示例》本文主要介绍了Go并发控制方法,通过带缓冲通道和第三方库实现并发数量限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录带有缓冲大小的通道使用第三方库其他控制并发的方法因为go从语言层面支持并发,所以面试百分百会问到