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