首页 > 学院 > 开发设计 > 正文

PAT甲级1087

2019-11-11 05:23:21
字体:
来源:转载
供稿:网友

1087. All Roads Lead to Rome (30)

时间限制200 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, Yue

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 recommended. 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 recommended route. Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".

Sample Input:
6 7 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1Sample Output:
3 3 195 97HZH->PRS->ROM
#include<cstdio>#include<string>#include<iostream>#include<map>#include<vector>#include<queue>#include<algorithm>using namespace std;struct Node{	int v, dis;}node;struct compare{	bool Operator()(Node n1, Node n2)	{		return n1.dis > n2.dis;	}};const int maxn = 210;const int INF = 1000000000;int N, K;string startCity;vector<int> pre[maxn];bool vis[maxn] = { false };int d[maxn];int happiness[maxn];vector<Node> Adj[maxn];map<string, int> s2i;map<int, string> i2s;vector<int> path, tempPath;int id = 0;//给城市编号int StringToInt(string s){	if (s2i.find(s) == s2i.end())	{		s2i[s] = ++id;		i2s[id] = s;		return id;	}	else		return s2i[s];}void Dijkstra(int s){	fill(d, d + maxn, INF);	d[s] = 0;	priority_queue<Node, vector<Node>, compare> Q;	node.dis = 0; node.v = s;	Q.push(node); int u;	for (int i = 0; i < N; i++)	{		if (!Q.empty())		{			u = Q.top().v;			vis[u] = true;			Q.pop();		}		for(int i=0;i<Adj[u].size();i++)		{			int v = Adj[u][i].v;			int dis = Adj[u][i].dis;			if (!vis[v])			{				if (d[u] + dis < d[v])				{					d[v] = d[u] + dis;					pre[v].clear();					pre[v].push_back(u);					node.v = v; node.dis = d[v];					Q.push(node);				}				else if (d[u] + dis == d[v])				{					pre[v].push_back(u);				}			}		}	}}int start = 0;int opthappinesssum = -1;int optaveragehappiness = INF;int countRoute = 0;void DFS(int v){	if (v == start)	{		countRoute++;		tempPath.push_back(v);		int happinesssum = 0, averagehappiness = 0;		for (int i = 0; i < tempPath.size(); i++)		{			int id = tempPath[i];			happinesssum += happiness[id];		}		averagehappiness = happinesssum / (tempPath.size() - 1);		if (opthappinesssum < happinesssum)		{			opthappinesssum = happinesssum;			path = tempPath;			optaveragehappiness = averagehappiness;//这里一定要注意更新一下		}		else if (opthappinesssum == happinesssum)		{			if (optaveragehappiness < averagehappiness)			{				optaveragehappiness = averagehappiness;				path = tempPath;			}		}		tempPath.pop_back();		return;	}	tempPath.push_back(v);	for (int i = 0; i < pre[v].size(); i++)	{		DFS(pre[v][i]);	}	tempPath.pop_back();}void PrintPath(){	for (int i = path.size() - 1; i >= 0; i--)	{		cout << i2s[path[i]];		if (i > 0)			cout << "->";		else			cout << endl;	}}int main(){	cin >> N >> K >> startCity;	s2i[startCity] = 0;	i2s[0] = startCity;	string s;	for (int i = 0; i < N-1; i++)	{		cin >> s;		int v = StringToInt(s);		cin >> happiness[v];	}	string s1; int cost;	for (int i = 0; i < K; i++)	{		cin >> s >> s1 >> cost;		int u = StringToInt(s);		int v = StringToInt(s1);		node.v = v; node.dis = cost;		Adj[u].push_back(node); node.v = u;		Adj[v].push_back(node);	}	Dijkstra(0);	DFS(s2i["ROM"]);	cout << countRoute << " " << d[s2i["ROM"]] << " "		<< opthappinesssum << " " << optaveragehappiness		<< endl;	PrintPath();}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表