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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章