1087. All Roads Lead to Rome (30)

题目:

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 recommended. 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 recommended 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
注意:
1、这道题目跟1018类似,但是复杂度没有1018那么大,这里直接Dijkstra就可以找出solution(就跟我1018里面贴上的那个有一个case未ac的代码思路一样),而不需要像1018那样先找出所有满足条件的路径再dfs从中寻找solution。
2、大概思路是Dijkstra搜索的时候如果找到了cost更小的路径则选择更小的,如果cost相等则选择total happiness更大的,如果total happiness也相等则选择average happiness更大的(也就是路径中city个数最小的)。
3、需要小心路径数量pathnum的计算,我刚开始是只用一个int变量来记录,结果case 2一直不过,后来改成vector来累计就可以ac了,一定要注意这一点。

代码:
//1087
#include<iostream>
#include<vector>
#define inf 65535
using namespace std;

vector<vector<int>>G;//graph matrix
vector<int>cost;//cost of path from the start to each city
vector<int>happiness;//happiness of each city
vector<int>path;//the solution path
vector<int>totalHap;//total happiness of each route
vector<int>city;//the value of all the citis
int name2index[20000]={0};//name to index
int n;//the number of cities

int str2num(char *city)
{
	return (city[0]-'A')*676+(city[1]-'A')*26+city[2]-'A';
}

void findpath()
{//based on Dijkstra algorithm
	vector<int>pre;//previous city of each city's path
	vector<int>citynum;//number of city in path
	vector<int>visited;//visited flag
	vector<int>pathnum;//number of path
	int dest=name2index[str2num("ROM")];//the destination Rome
	//--------innitiate--------
	pre.assign(n,0);
	citynum.assign(n,1);
	visited.assign(n,0);
	pathnum.assign(n,1);
	cost=G[0];
	totalHap=happiness;
	visited[0]=1;
	//--------find path--------
	int k=0;//current city
	while(1)
	{
		int min=inf;
		for(int i=0;i<n;++i)
		{//find the nearist unvisited city
			if(!visited[i] && cost[i]<min)
			{
				min=cost[i];
				k=i;
			}
		}
		if(k==dest)
			break;
		visited[k]=1;
		for(int i=0;i<n;++i)
		{
			if(!visited[i] && cost[k]+G[k][i]<cost[i])
			{//choose the path with minimum cost
				pre[i]=k;
				cost[i]=cost[k]+G[k][i];
				totalHap[i]=totalHap[k]+happiness[i];
				citynum[i]=citynum[k]+1;
				pathnum[i]=pathnum[k];
			}
			else if(!visited[i] && cost[k]+G[k][i]==cost[i])
			{//choose the path with the maximum total happiness or average happiness
				pathnum[i] += pathnum[k];
				if(totalHap[k]+happiness[i]>totalHap[i] ||
					(totalHap[k]+happiness[i]==totalHap[i] && citynum[k]+1<citynum[i]))
				{
					pre[i]=k;
					totalHap[i]=totalHap[k]+happiness[i];
					citynum[i]=citynum[k]+1;
				}
			}
		}
	}
	//--------print the result-----
	printf("%d %d %d %d\n",pathnum[dest],cost[dest],totalHap[dest],totalHap[dest]/citynum[dest]);
	while(dest!=0)
	{
		path.push_back(dest);
		dest=pre[dest];
	}
	printf("%c%c%c",city[0]/676+'A',(city[0]/26)%26+'A',city[0]%26+'A');
	for(int i=path.size()-1;i>=0;--i)
		printf("->%c%c%c",city[path[i]]/676+'A',(city[path[i]]/26)%26+'A',city[path[i]]%26+'A');
}

int main()
{
	int m;
	char name[5];
	scanf("%d%d%s",&n,&m,name);
	int start=str2num(name);
	city.push_back(start);
	happiness.assign(n,0);
	G.resize(n);
	G[0].assign(n,inf);
	G[0][0]=0;
	for(int i=1;i<n;++i)
	{
		scanf("%s%d",name,&happiness[i]);
		int c=str2num(name);
		city.push_back(c);
		name2index[c]=i;
		G[i].assign(n,inf);
		G[i][i]=0;
	}
	for(int i=0;i<m;++i)
	{//build the graph
		char name0[5];
		int a,b,c;
		scanf("%s%s%d",name,name0,&c);
		a=name2index[str2num(name)];
		b=name2index[str2num(name0)];
		G[a][b]=G[b][a]=c;
	}
	findpath();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值