PAT甲級1087

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
#include<bits/stdc++.h>
using namespace std;

const int Max = 210;
const int inf = 0x3fffffff;
int n, k, g[Max][Max], d[Max], happy[Max], happynum = -1, happyavg = -1, num = 0;
string st;
bool visit[Max];
vector<int> pre[Max], ans, temp;
map<string, int> cityid;
map<int, string> cityname;

void Dijkstra(int s){
	fill(visit, visit + Max, false);
	fill(d, d + Max, inf);
	d[s] = 0;
	for(int i = 0; i < n; i++){
		int u = -1, min = inf;
		for(int j = 0; j < n; j++){
			if(!visit[j] && d[j] < min){
				min = d[j];
				u = j;
			}
		}
		if(u == -1)
			return;
		visit[u] = true;
		for(int v = 0; v < n; v++){
			if(!visit[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);
			}
		}
	}
}

void dfs(int s){
	if(s == 0){
		num++;
		int tempnum = 0, tempavg;
		for(int i = 0; i < temp.size(); i++){
			int id = temp[i];
			tempnum += happy[id];
		}
		tempavg = tempnum / temp.size();
		if(tempnum > happynum){
			happynum = tempnum;
			happyavg = tempavg;
			ans = temp;
		}
		else if(tempnum == happynum){
			if(tempavg > happyavg){
				happyavg = tempavg;
				ans = temp;
			}
		}
	}
	temp.push_back(s);
	for(int i = 0; i < pre[s].size(); i++)
		dfs(pre[s][i]);
	temp.pop_back();
}

int main(){
	fill(g[0], g[0] + Max * Max, inf);
	fill(happy, happy + Max, 0);
	cin >> n >> k >> st;
	cityid[st] = 0;
	cityname[0] = st;
	for(int i = 1; i < n; i++){
		string c;
		int h;
		cin >> c >> h;
		cityid[c] = i;
		cityname[i] = c;
		happy[i] = h;
	}
	for(int i = 0; i < k; i++){
		string a, b;
		int dis;
		cin >> a >> b >> dis;
		g[cityid[a]][cityid[b]] = dis;
		g[cityid[b]][cityid[a]] = dis;
	}
	Dijkstra(0);
	dfs(cityid["ROM"]);
	printf("%d %d %d %d\n", num, d[cityid["ROM"]], happynum, happyavg);
	cout << st;
	for(int i = ans.size() -1; i >=0; i--)
		cout << "->" << cityname[ans[i]];
	return 0;
}

 

發佈了117 篇原創文章 · 獲贊 15 · 訪問量 7282
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章