PAT 甲级 1087  All Roads Lead to Rome

1087 All Roads Lead to Rome (30 point(s))

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2≤N≤200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N−1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format City1 City2 Cost. Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommanded. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommanded route. Then in the next line, you are supposed to print the route in the format City1->City2->...->ROM.

Sample Input:

6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1

Sample Output:

3 3 195 97
HZH->PRS->ROM

经验总结:

这一题,不得不说,真的是最短路径最全最经典的题目了(如果不是,就当我是瞎说的0.0),弄懂这一题,应该就不是怎么怕大多数最短路径的问题了,首先,这一题涉及到求点权,边权,边的条数,以及存储最优路径,还涉及到字符串与顶点之间的映射,以及多条最短路径通过其他条件取最优等等知识点,考察的十分充分,如果对于所有知识点都很熟悉的话,这一题就不再话下了~我使用的是DFS+Dijkstra的方法,只使用Dijkstra方法可以做出来,但是很麻烦,还是前者比较容易理解与实现~

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>
#include <map>
using namespace std;
const int maxn=210;
const int INF=0x3fffffff;
int G[maxn][maxn],n,k,start,happiness[maxn],t,d[maxn],ind[maxn],h[maxn],final,num[maxn],avehappiness=-1;
bool flag[maxn];
vector<int> path,temppath,pre[maxn];
int convert(char s[])
{
	int ans=0;
	for(int i=0;i<3;++i)
		ans=ans*26+s[i]-'A';
	return ans;
}
void reconvert(int a)
{
	char s[4];
	int p=26*26;
	for(int i=0;i<3;++i)
	{
		putchar(a/p+'A');
		a%=p;
		p/=26;
	}
}
void Dijkstra(int s)
{
	memset(flag,0,sizeof(flag));
	fill(d,d+maxn,INF);
	memset(h,0,sizeof(h));
	memset(num,0,sizeof(num));
	d[s]=0;
	num[s]=1;
	for(int i=0;i<n;++i)
	{
		int min=INF,u=-1;
		for(int j=0;j<n;++j)
		{
			if(d[j]<min&&flag[j]==false)
			{
				min=d[j];
				u=j;
			}
		}
		if(u==-1)
			return ;
		flag[u]=true;
		for(int v=0;v<n;++v)
		{
			if(flag[v]==false&&G[u][v]!=INF)
			{
				if(d[u]+G[u][v]<d[v])
				{
					d[v]=d[u]+G[u][v];
					h[v]=h[u]+happiness[v];
					pre[v].clear();
					pre[v].push_back(u);
					num[v]=num[u];
				}
				else if(d[u]+G[u][v]==d[v])
				{
					if(h[u]+happiness[v]>h[v])
					{
						h[v]=h[u]+happiness[v];
						pre[v].clear();
					}
					pre[v].push_back(u);
					num[v]+=num[u];
				}
			}
		}
	}
}
void DFS(int v,int f)
{
	if(v==f)
	{
		temppath.push_back(v);
		int sum=0;
		for(int i=temppath.size()-1;i>=0;--i)
			sum+=happiness[temppath[i]];
		int x=sum/(temppath.size()-1);
		if(x>avehappiness)
		{
			path=temppath;
			avehappiness=x;
		}
		temppath.pop_back();
		return ;
	}
	temppath.push_back(v);
	for(int i=0;i<pre[v].size();++i)
	{
		DFS(pre[v][i],f);
	}
	temppath.pop_back();
}
int main()
{
	char str[5],str1[5];
	map<int,int> mp;
	fill(G[0],G[0]+maxn*maxn,INF);
	scanf("%d%d%s",&n,&k,str);
	start=convert(str);
	mp[start]=0;
	ind[0]=start;
	happiness[0]=0;
	for(int i=1;i<n;++i)
	{
		scanf("%s%d",str,&happiness[i]);
		t=convert(str);
		mp[t]=i;
		ind[i]=t;
	}
	for(int i=0;i<k;++i)
	{
		scanf("%s %s %d",str,str1,&t);
		int a=mp[convert(str)];
		int b=mp[convert(str1)];
		G[a][b]=G[b][a]=t;
	}
	Dijkstra(mp[start]);
	final=convert("ROM");
	DFS(mp[final],mp[start]);
	printf("%d %d %d %d\n",num[mp[final]],d[mp[final]],h[mp[final]],avehappiness);
	for(int i=path.size()-1;i>=0;--i)
	{
		reconvert(ind[path[i]]);
		if(i!=0)
			printf("->");
		else
			printf("\n");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值