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

实现思路:

最短路径模板题,直接上代码。

AC代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cstring>
using namespace std;
const int MAXN=220;
const double INF=(1<<30)-1;
int n,k,happiness[MAXN] = {0},G[MAXN][MAXN],d[MAXN];
string start;
unordered_map<int,string> id2n;
unordered_map<string,int> n2id;
bool vist[MAXN];
vector<int> tempPath,ansPath,pre[MAXN];

void Dijkstra(int s) {
	fill(d,d+MAXN,INF);
	memset(vist,sizeof(vist),false);
	d[s]=0;
	for(int i=0; i<n; i++) {
		int u=-1,MIN=INF;
		for(int j=0; j<n; j++) {
			if(!vist[j]&&d[j]<MIN) {
				MIN=d[j];
				u=j;
			}
		}
		if(u==-1) return;
		vist[u]=true;
		for(int v=0; v<n; v++) {
			if(!vist[v]&&G[u][v]!=INF) {
				if(d[u]+G[u][v]<d[v]) {
					d[v]=d[u]+G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				} else if(d[u]+G[u][v]==d[v]) {
					pre[v].push_back(u);
				}
			}
		}
	}
}

int ansNum=0,maxHap=-1,maxAvg=-1;
void dfs(int x) {
	if(pre[x].size()==0) {
		int tempHap=0,tempAvg=0;
		tempPath.push_back(x);
		ansNum++;
		for(int i=0; i<tempPath.size(); i++) {
			int id=tempPath[i];
			tempHap+=happiness[id];
		}
		tempAvg=tempHap/(tempPath.size()-1);
		if(tempHap>maxHap) {
			maxHap=tempHap;
			maxAvg=tempAvg;
			ansPath=tempPath;
		} else if(tempHap==maxHap&&tempAvg>maxAvg) {
			maxAvg=tempAvg;
			ansPath=tempPath;
		}
		tempPath.pop_back();
		return;
	}
	tempPath.push_back(x);
	for(int i=0; i<pre[x].size(); i++) {
		dfs(pre[x][i]);
	}
	tempPath.pop_back();
}

int main() {
	fill(G[0],G[0]+MAXN*MAXN,INF);
	cin>>n>>k>>start;
	char id1[4],id2[4],id[4];
	int cnt=1,dis;
	n2id[start]=0;
	id2n[0]=start;
	for(int i=0; i<n-1; i++ ) {
		scanf("%s",id);
		n2id[id]=cnt;
		id2n[cnt]=id;
		scanf("%d",&happiness[cnt]);
		cnt++;
	}
	while(k--) {
		scanf("%s %s %d",id1,id2,&dis);
		G[n2id[id1]][n2id[id2]]=G[n2id[id2]][n2id[id1]]=dis;
	}
	Dijkstra(n2id[start]);
	dfs(n2id["ROM"]);
	printf("%d %d %d %d\n",ansNum,d[n2id["ROM"]],maxHap,maxAvg);
	for(int i=ansPath.size()-1; i>=0; i--) {
		cout<<id2n[ansPath[i]];
		if(i>0) printf("->");
	}
	return 0;
}
posted @ 2021-02-27 14:27  coderJ_ONE  阅读(60)  评论(0编辑  收藏  举报