PTA_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

题意:
选出最短路径,有多条则选点权最大的,还有多条则选平均点权最大,输出最短路径条数、长度、点权和、平均点权,下一行输出路径

分析:
用Dijkstra加条件判断即可

代码:

#include
#include
#include
#include
#include 
#include
using namespace std;

#define MAXV 210
#define INF 1e9
int N, M, st, G[MAXV][MAXV], weight[MAXV];//顶点数,边数,起点,邻接矩阵,点权
int d[MAXV], w[MAXV], num[MAXV], pt[MAXV], pre[MAXV];
//最短距离,最大点权,最短路径条数,最短路径上的顶点数,前驱 
bool vis[MAXV] = {false};
map<string, int> cityToIndex;//将城市名转换为编号
map<int, string> indexToCity;//将编号转换为城市名

void Dijkstra(int s){
	fill(d, d+MAXV, INF);
	memset(w, 0, sizeof(w));
	memset(num, 0, sizeof(num));
	memset(pt, 0, sizeof(pt));
	for(int i=0;i<N;i++) pre[i] = i;
	d[s] = 0;
	w[s] = weight[st];
	num[s] = 1;
	for(int i=0;i<N;i++){//循环N次 
		int u=-1, MIN=INF;//u使d[u]最小,MIN存放该最小d[u]
		for(int j=0;j<N;j++){//找到未访问的顶点中d[]最小的 
			if(vis[j]==false&&d[j]<MIN){
				u = j;
				MIN = d[j];
			} 
		} 
		if(u==-1) return;//找不到说明不连通
		vis[u] = true;
		for(int v=0;v<N;v++){
			if(vis[v]==false&&G[u][v]!=INF){
				if(d[u]+G[u][v]<d[v]){//路径更短则更新 
					d[v] = d[u] + G[u][v];
					w[v] = w[u] + weight[v];
					num[v] = num[u];
					pt[v] = pt[u] + 1;
					pre[v] = u;//v的前驱为u 
				}else if(d[u]+G[u][v]==d[v]){//相同长度路径 
					num[v] += num[u];//到v的最短路径条数继承自num[u] 
					if(w[u]+weight[v]>w[v]){//点权更大则更新 
						w[v] = w[u] + weight[v];
						pt[v] = pt[u] + 1;
						pre[v] = u;
					}else if(w[u]+weight[v]==w[v]){//点权相同则比较平均点权 
						double uAvg = 1.0*(w[u]+weight[v])/(pt[u]+1);
						double vAvg = 1.0*w[v]/pt[v];
						if(uAvg>vAvg){//平均点权更大 
							pt[v] = pt[u] + 1;
							pre[v] = u; 
						} 
					}
				}
			}
		} 
	}
} 

void printPath(int v){
	if(v==0){
		cout<<indexToCity[v];
		return;
	}
	printPath(pre[v]);
	cout<<"->"<<indexToCity[v];
}

int main()
{
    string start, city1, city2;
    cin>>N>>M>>start;
    cityToIndex[start] = 0;//起始城市 
    indexToCity[0] = start;
    for(int i=1;i<=N-1;i++){//除起始城市外还有N-1个城市 
    	cin>>city1>>weight[i];
    	cityToIndex[city1] = i;//city1下标记为i 
    	indexToCity[i] = city1;//下标i对应city1 
	}
	fill(G[0], G[0]+MAXV*MAXV, INF);
	for(int i=0;i<M;i++){
		cin>>city1>>city2;
		int c1=cityToIndex[city1], c2=cityToIndex[city2];
		cin>>G[c1][c2];//边权
		G[c2][c1] = G[c1][c2]; 
	}
	Dijkstra(0);//从起始城市开始 
	int rom = cityToIndex["ROM"];//结束城市下标
	printf("%d %d %d %d\n",num[rom], d[rom], w[rom], w[rom]/pt[rom]);
	printPath(rom);//输出路径 
    return 0;
}

你可能感兴趣的:(PTA_PAT,(Advanced,Level),Practice)