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

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

PAT甲級1087

2019-11-11 03:54:04
字體:
來源:轉載
供稿:網友

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();}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聂荣县| 岳普湖县| 大宁县| 无锡市| 栾川县| 和龙市| 泾阳县| 准格尔旗| 巴楚县| 子长县| 丁青县| 讷河市| 五寨县| 罗山县| 夹江县| 米林县| 林周县| 邵阳县| 苍南县| 施甸县| 衡阳县| 河北省| 项城市| 色达县| 伊金霍洛旗| 古浪县| 曲阳县| 页游| 三河市| 芜湖县| 邹平县| 凤山县| 福泉市| 武隆县| 榆中县| 古蔺县| 贵溪市| 澳门| 徐闻县| 崇信县| 镇坪县|