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

原创 2012年04月18日 12:34:18
Picnic Planning
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 7356   Accepted: 2555

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

Source



题意:一共有nv个人从家里出发要到目的地的“Park”,一个人可以开车去接另一个人,一辆车可载重无限,问这n个人到Park的总路程最小,此外,还有一个条件,Park处最多能挺s辆车,即Park的度数不能超过s,求最小生成树。

大概思路:设Park为标号0,其他地点为1,2...,n-1;先求出1~n-1的最小生成树,然后在枚举0结点度数d从1~s的情况,即向已生成的最小生成树插入边,当d>=2的时候,必然产生回路,因此求出回路中最大的边,进行判断是否替换……

源代码:(0Ms)
#include<iostream>
#include<cstring>
using namespace std;

const int MAX_NV = 21;
const int INF = 0x7f7f7f7f;

typedef struct Edge
{
	int sv,ev,w;
}Edge;

int nv;
char name[MAX_NV][12];
int gam[MAX_NV][MAX_NV];
Edge mstEdge[MAX_NV];
int s;
int ans;
bool isCycle;

int IndName(char ch[])
{
	int ind=0;
	while(ind<nv && strcmp(name[ind],ch)!=0)
		ind++;
	if(ind==nv)
		strcpy(name[nv++],ch);
	return ind;
}

int Prim()
{
	int res=0;
	int i,j,k;
	for(i=1;i<nv-1;i++)
	{
		mstEdge[i].sv = 1;
		mstEdge[i].ev = i+1;
		mstEdge[i].w = gam[1][i+1];
	}

	for(k=2;k<nv;k++)
	{
		int minw = mstEdge[k-1].w,ind = k-1;
		for(i=k;i<nv-1;i++)
			if(minw > mstEdge[i].w)
			{
				minw = mstEdge[i].w;  
				ind = i;
			}
		res += minw;
		
		Edge tmp = mstEdge[ind];	mstEdge[ind] = mstEdge[k-1]; mstEdge[k-1]=tmp;

		j = mstEdge[k-1].ev;

		for(i=k;i<nv-1;i++)
		{
			int v = mstEdge[i].ev,w = gam[j][v];
			if(mstEdge[i].w > w)
			{
				mstEdge[i].w = w;
				mstEdge[i].sv = j;
			}
		}		
	}

	return res;
}

void MaxWeightEdgeInCycle(int mv,int sv,int ev,int& maxw,int& ind)
{
	if(mv == ev)
	{
		isCycle = true;
		return;
	}

	for(int i=0;i<nv-1;i++)
	{
		if(mstEdge[i].sv != ev && mstEdge[i].ev != ev)
			continue;
		if(mstEdge[i].sv == ev && mstEdge[i].ev != sv)
		{
			MaxWeightEdgeInCycle(mv,ev,mstEdge[i].ev,maxw,ind);
			if(isCycle)
			{
				if(maxw<mstEdge[i].w && mstEdge[i].ev!=0)
				{
					maxw=mstEdge[i].w;
					ind=i;
				}
				break;
			}
		}
		else if(mstEdge[i].sv != sv && mstEdge[i].ev == ev)
		{
			MaxWeightEdgeInCycle(mv,ev,mstEdge[i].sv,maxw,ind);
			if(isCycle)
			{
				if(maxw<mstEdge[i].w && mstEdge[i].sv!=0)
				{
					maxw=mstEdge[i].w;
					ind=i;
				}
				break;
			}
		}
	}
}

void Solve()
{
	int i;
	bool exist[MAX_NV];
	ans = Prim();
	
	int minw = INF+1,ev = -1;
	for(i=1;i<nv;i++)
	{
		if(gam[0][i] < minw)
		{
			minw = gam[0][i];
			ev = i;
		}
		exist[i]=false;
	}

	ans += minw;
	
	exist[ev]=true;
	mstEdge[0].w=minw;	mstEdge[0].sv=0;	mstEdge[0].ev=ev;

	for(int d=2;d<=s;d++)
	{
		int dec = INF+1,edgeInd=-1;
		ev = -1;
		for(i=1;i<nv;i++)
		{
			if(exist[i]==true)
				continue;
			int maxw=0,ind=-1;
			isCycle = false;
			MaxWeightEdgeInCycle(0,0,i,maxw,ind);
			if(dec > gam[0][i]-maxw)
			{
				dec=gam[0][i]-maxw;
				edgeInd=ind;
				ev=i;
			}
		}
		if(dec>=0)
			break;
		else
		{
			
			mstEdge[edgeInd].sv=0;	mstEdge[edgeInd].ev=ev;	mstEdge[edgeInd].w=gam[0][ev];
			ans += dec;
			exist[ev]=true;
		}
	}
}

int main()
{
	int i;
	char name1[12],name2[12];
	int ne;
	strcpy(name[0],"Park");
	memset(gam,0x7f,sizeof(gam));
	while(scanf("%d",&ne)!=EOF)
	{
		nv = 1;
		int dis,ind1,ind2;
		for(i=0;i<ne;i++)
		{
			cin >> name1 >> name2 >> dis;
			ind1=IndName(name1);
			ind2=IndName(name2);
			gam[ind1][ind2] = gam[ind2][ind1] = dis;
		}
		
		cin >> s;

		Solve();
		printf("Total miles driven: %d\n",ans);
	}
	return 0;
}


poj_1639 Picnic Planning(度限制最小生成树)

Picnic Planning Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 10431   Accepted:...
  • christry_stool
  • christry_stool
  • 2017年02月07日 00:40
  • 363

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

hihoCoder挑战赛11来啦!有Tshirt作为奖品哦~ Language: Default Picnic Planning Time Limi...
  • u011699990
  • u011699990
  • 2015年05月12日 23:04
  • 470

【POJ】1639 Picnic Planning 度限制最小生成树

Picnic Planning Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 9137 ...
  • u013368721
  • u013368721
  • 2014年07月18日 14:42
  • 922

POJ 1639 Picnic Planning【度限制最小生成树】

POJ 1639 Picnic Planning【度限制最小生成树】 分类: 【图论专辑】2010-08-02 03:47 706人阅读 评论(3) 收藏 举报 treestrin...
  • pi9nc
  • pi9nc
  • 2013年07月30日 20:36
  • 470

poj 1639 Picnic Planning 单度限制的最小生成树

题意: 给一个无向图连通图,求它的最小生成树,生成树满足条件点v0的度小于等于limit。 分析: 一般有度限制的最小生成树问题是np完全的,但单点度限制就比较简单了,先在原图上求不含v0的最小...
  • sepNINE
  • sepNINE
  • 2014年11月23日 10:25
  • 386

POJ 1639 Picnic Planning 【最小度限制生成树】

原题链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11062 http://poj.org/problem?id=16...
  • qq_35205305
  • qq_35205305
  • 2016年06月22日 20:48
  • 127

最小k度限制生成树

/************************************************* 算法引入: 最小k度限制生成树,就是指有特殊的某一点的度不能超过k时的最小生成树; 如果T是G的一...
  • Jarily
  • Jarily
  • 2013年04月09日 20:48
  • 3118

最小度限制生成树

设G=(V, E, ω)是连通的无向图,v0 ∈V是特别指定的一个顶点,k为给定的一个正整数。如果T是G的一个生成树且dT(v0)=k,则称T为G的k度限制生成树。G中权值和最小的k度限制生成树称为G...
  • qq_21120027
  • qq_21120027
  • 2016年08月09日 19:26
  • 980

POJ 1639 最小度限制生成树

#include #include #include #include #include #define max(f, s) (f) > (s)? (f):(s) using namespace s...
  • u012576214
  • u012576214
  • 2015年08月10日 14:33
  • 228

POJ 1639 Picnic Planning(初遇最小度限制生成树)

这是最小度限制生成树的经典问题,题意就不说了 题目链接:http://poj.org/problem?id=1639 一般都是1个顶点的度有限制k,如果每个顶点的度都有限制,那么当前是NP难的。 为了...
  • kalilili
  • kalilili
  • 2015年02月11日 20:56
  • 666
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1639:Picnic Planning(最小度限制生成树)
举报原因:
原因补充:

(最多只允许输入30个字)