PAT 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 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

解题思路

  • 该题是最短路径题目中的经典题型,算是一道大满贯题目,集计算边权、点权、路径个数为一体的一道题目。第一优先,是找出最少花费(最短路径,边权最小),第二优先是找出最大幸福指数(点权最大),第三优先是找出最大平均幸福指数,即需要将幸福指数与路径上经过的顶点个数相除。题目最后还要输出最短路径的条数。

C++实现

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <queue>
#include <stack>
#define MaxNum 1000
#define INF 99999

using namespace std;

unordered_map<string,int> city_num;  //cityname->citynum 
unordered_map<int,string> city_name; //citynum->cityname
vector< vector<int> > city(MaxNum);  //邻接表存储图 
unordered_map<int,int> line;         //存储边权(费用) 
int cityhappiness[MaxNum];           //存储点权 (幸福指数) 
int N;                               

void BuildGraph()
{
	int K;
	scanf("%d %d",&N,&K);
	string str;
	cin>>str;
	city_name[0]=str;
	city_num[str]=0;
	cityhappiness[0]=0;
	int happiness;
	int i;
	/*建立cityname<->citynum的索引*/ 
	for(i=1;i<N;i++){
		cin>>str>>happiness;
		city_name[i]=str;
		city_num[str]=i;
		cityhappiness[i]=happiness;
	}
	string city1,city2;
	int cost;
	/*用citynum建立图(邻接表)*/ 
	for(i=0;i<K;i++){
		cin>>city1>>city2>>cost;
		city[city_num[city1]].push_back(city_num[city2]);
		city[city_num[city2]].push_back(city_num[city1]);
		line[city_num[city1]*MaxNum+city_num[city2]]=cost;
		line[city_num[city2]*MaxNum+city_num[city1]]=cost;
	}
}

int FindMinCost(int cost[],bool collected[])
{
	int MinCost=INF;
	int MinV;
	int V;
	for(V=0;V<N;V++){
		if(!collected[V]){
			if(cost[V]<MinCost){
				MinCost=cost[V];
				MinV=V;
			}
		}
	}
	if(MinCost<INF){
		return MinV;
	}else{
		return -1;
	}
}

void Dijkstra(int count[],int path[],int average[],int cost[],int happiness[],int S)
{
	bool collected[MaxNum];
	int cnt[MaxNum];
	int V,W,i;
	/*初始化*/ 
	for(V=0;V<N;V++){
		collected[V]=false;
		happiness[V]=0;  //存储点权(幸福指数)
		cost[V]=INF;     //存储边权(最小花费)
		path[V]=-1;      //存储所要求的最终最短路径
		average[V]=0;   
		count[V]=0;      //存储最短路径的条数
		cnt[V]=0;        //存储最短路径经过的顶点个数
	}
	for(V=0;V<city[S].size();V++){
		cost[city[S][V]]=line[S*MaxNum+city[S][V]];
		path[city[S][V]]=S;
		count[city[S][V]]=1;
		happiness[city[S][V]]=cityhappiness[city[S][V]];
		cnt[city[S][V]]=1;
		average[city[S][V]]=happiness[city[S][V]]/cnt[city[S][V]];
	}
	cost[S]=0;
	count[S]=1;
	collected[S]=true;
	while(true){
		V=FindMinCost(cost,collected);
		if(V==-1){
			break;
		}else{
			collected[V]=true;
			for(i=0;i<city[V].size();i++){
				W=city[V][i];
				if(!collected[W]){
					if(cost[W]>line[V*MaxNum+W]+cost[V]){
						cost[W]=line[V*MaxNum+W]+cost[V];
						path[W]=V;
						happiness[W]=cityhappiness[W]+happiness[V];
						count[W]=count[V];
						cnt[W]=cnt[V]+1;
					}else if(cost[W]==line[V*MaxNum+W]+cost[V]){
						if(happiness[W]<cityhappiness[W]+happiness[V]){
							path[W]=V;
							happiness[W]=cityhappiness[W]+happiness[V];
							cnt[W]=cnt[V]+1;
						}else if(happiness[W]==cityhappiness[W]+happiness[V]){
							if(cnt[W]>cnt[V]+1){
								cnt[W]=cnt[V]+1;
								path[W]=V;
							}
						}
						count[W]+=count[V];
					}
					average[W]=happiness[W]/cnt[W];
				}
			}
		}	
	}
	
}

void Solve()
{
	int count[MaxNum],path[MaxNum],average[MaxNum],cost[MaxNum],happiness[MaxNum];
	Dijkstra(count,path,average,cost,happiness,0);
	stack<int> SS;
	int W;
	W=city_num["ROM"];
	int temp=W;
	while(temp!=-1){
		SS.push(temp);
		temp=path[temp];
	}
	printf("%d %d %d %d\n",count[W],cost[W],happiness[W],average[W]);
	int flag=0;
	while(SS.size()>0){
		int V=SS.top();
		SS.pop();
		if(flag){
			printf("->");
		}else{
			flag=1;
		}
		cout<<city_name[V];
	}
}

int main()
{
	BuildGraph();
	Solve();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值