PAT甲级-1087-All Roads Lead to Rome(Dijkstra算法+最短路径)

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

代码如下

#include<iostream>
#include<map>
#include<vector>
#include<climits>
using namespace std;
const int N = 205;
int n,k,count=0;
int dis[N],weigh[N],visit[N],path[N][N];
double ansMax=-1,ansAve=-1;
map<string,int>nametonum;
map<int,string>numtoname;
vector<int> tempPath,ansPath,pre[N];
void dfs(int v)
{
	double tmpMax=0,tmpAve=-1;
	tempPath.push_back(v);
	if(v == 1){
		count++;
		for(int i = tempPath.size()-2; i >= 0; i--)
		{
			int id = tempPath[i];
			tmpMax += weigh[id];
		}
		tmpAve = tmpMax*1.0/(tempPath.size()-1);
		if(tmpMax > ansMax){
			ansMax = tmpMax;
			ansAve = tmpAve;
			ansPath = tempPath;
		}else if(tmpMax == ansMax&&tmpAve > ansAve){
			ansAve = tmpAve;
			ansPath = tempPath;
		} 
		tempPath.pop_back();
		return;
	} 
	for(int i = 0; i < (int)pre[v].size(); i++)
	{
		dfs(pre[v][i]);
	}
	tempPath.pop_back();
}
void dijsktra()
{
	dis[1] = 0;
	for(int i = 1; i <= n; i++)
	{
		int min = INT_MAX,index = -1;
		for(int j = 1; j <= n; j++)
		{
			if(!visit[j] && dis[j] < min){
				min = dis[j];
				index = j;
			}
		}
		if(index == -1) break;
		visit[index] = 1;
		for(int j = 1; j <= n; j++)
		{
			if(!visit[j] && path[index][j]!=INT_MAX){
				if(dis[j] > dis[index] + path[index][j]){
					dis[j] = dis[index] + path[index][j];
					pre[j].clear();
					pre[j].push_back(index);
				}else if(dis[j] == dis[index] + path[index][j]){
					pre[j].push_back(index);
				}
			}
		}
	}
}
int main()
{
	string start;
	cin>>n>>k>>start;
	nametonum[start] = 1;
	numtoname[1] = start;
	fill(path[0], path[0]+N*N,INT_MAX);
	fill(dis, dis+N, INT_MAX);
	fill(visit, visit+N, 0);
	for(int i = 2; i <= n; i++)
	{
		string cityName;int happy;
		cin>>cityName>>happy;
		nametonum[cityName] = i;
		numtoname[i] = cityName;
		weigh[i] = happy;
	}
	for(int i = 0; i < k; i++)
	{
		string s1,s2;int cost;
		cin>>s1>>s2>>cost;
		path[nametonum[s1]][nametonum[s2]] = path[nametonum[s2]][nametonum[s1]] = cost;
	}
	dijsktra();
	dfs(nametonum["ROM"]);
	cout<<count<<" "<<dis[nametonum["ROM"]]<<" "<<ansMax<<" "<<(int)ansAve<<endl;
	for(int i = ansPath.size()-1; i > 0; i--)
	{
		cout<<numtoname[ansPath[i]]<<"->";
	}
	cout<<numtoname[ansPath[0]];
	return 0;
}
发布了110 篇原创文章 · 获赞 746 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_42437577/article/details/104293576