国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PAT甲級1087

2019-11-11 04:32:55
字體:
來源:轉載
供稿:網友

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();}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苗栗市| 安图县| 开化县| 临朐县| 肇庆市| 黄陵县| 惠东县| 肥东县| 嘉鱼县| 隆子县| 霸州市| 德兴市| 汕尾市| 潮州市| 苏州市| 甘德县| 施秉县| 拉孜县| 息烽县| 玉环县| 札达县| 长丰县| 镇安县| 蓬莱市| 平原县| 凤山市| 冀州市| 饶平县| 聂荣县| 沾益县| 门源| 桂阳县| 酒泉市| 霸州市| 乐平市| 闽清县| 广汉市| 峨边| 土默特左旗| 微博| 资溪县|