天天看点

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
           

15分:

我的思路:

1. 利用map记录对应城市和城市编号的关系,city2Num:城市名做键,对应的编号为值,Num2City同理。

2. 进行 dijkstra + dfs 进行搜索。

过了一个点,大致知道因为自己的代码缺少判断平均最大路径的部分~先记录一下~

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
using namespace std;

const int MAXN = 210;
const int INF = 1000000000;

map<int, string> num2City; //城市编号
map<string, int> city2Num;
int G[MAXN][MAXN];
int dis[MAXN], w[MAXN],weight[MAXN], pre[MAXN], num[MAXN]; //dis最短距离数据,cost最大幸福值,weight每个城市的幸福值
bool visit[MAXN];
vector<int> temp, ans;
int maxHappiness = -1;
int n, k;
string beginCity;


void dfsPath(int f)
{
	temp.push_back(f);
	if (f == 0) return;
	temp.push_back(pre[f]);
}

void dij(int st)
{
	fill(dis, dis+MAXN, INF);
	fill(w, w+MAXN, 0);
	fill(num, num+MAXN, 0);
	dis[st] = 0;
	w[st] = weight[st];
	num[st] = 1;
	for (int i = 0; i < n; i++)
	{
		int u = -1, MIN = INF;
		for (int j = 0; j < n; j++)
		{
			if (visit[j] == false && dis[j] < MIN)
			{
				u = j;
				MIN = dis[j];
			}
		}
		if (u == -1) return;
		visit[u] = true;
		for (int v = 0; v < n; v++)
		{
			if (visit[v] == false && G[u][v] != INF)
			{
				if (dis[u] + G[u][v] < dis[v])
				{
					dis[v] = dis[u] + G[u][v];
					w[v] = w[u] + weight[v];
					pre[v] = u;
					num[v] = num[u];
				}else if (dis[u] + G[u][v] == dis[v])
				{
					if (w[u] + weight[v] > w[v])
					{
						w[v] = w[u] + weight[v];
						pre[v] = u;
					}
					num[v] += num[u];
				}
				
			}
		}
	}
}

int main()
{
	fill(G[0], G[0]+MAXN*MAXN, INF);	
	//fill(weight, weight+MAXN, INF);
	cin >> n >> k >> beginCity;
	int index = 0;
	num2City[index++] = beginCity;
	city2Num[beginCity] = index-1;

	weight[0] = 0;
	for (int i = 0; i < n - 1; i++)
	{
		string tempCity;
		int happy;
		cin >> tempCity >> happy;
		weight[index] = happy;
		city2Num[tempCity] = index;
		num2City[index++] = tempCity;		
	}
	for(int i =0; i < k; i++)
	{
		string v1, v2;
		cin >> v1 >> v2;
		int c;
		cin >> c;
		G[city2Num[v1]][city2Num[v2]] = c;
		G[city2Num[v2]][city2Num[v1]] = c;
	}

	dij(city2Num[beginCity]);
	dfsPath(city2Num["ROM"]);
	cout << num[city2Num["ROM"]] << " " << dis[city2Num["ROM"]]  <<  " " << w[city2Num["ROM"]] <<" " <<  w[city2Num["ROM"]]/temp.size() << endl;
	cout << beginCity;
	for (int i = temp.size()-1; i >= 0; i--)
	{
		cout << "->" << num2City[temp[i]];
	}
	return 0;
}
           

 补:

修改了一下,30分过~~

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
using namespace std;

const int MAXN = 210;
const int INF = 1000000000;

map<int, string> num2City; //城市编号
map<string, int> city2Num;
int G[MAXN][MAXN];
int dis[MAXN], w[MAXN],weight[MAXN], num[MAXN]; //dis最短距离数据,cost最大幸福值,weight每个城市的幸福值
vector<int> pre[MAXN]; //前驱数组
bool visit[MAXN];
vector<int> temp, ans;
int maxHappiness = -1, maxAverage = -1;
int n, k;
string beginCity;


void DFS(int v)
{
	if (v == 0)
	{
		temp.push_back(v);
		int happy = 0;
		for (int i = temp.size()-1; i >= 0; i--)
		{
			happy += weight[temp[i]];  //这个地方要注意,是temp[i],刚开始写成了i
		}

		int aver = happy / (temp.size()-1);  //这个地方,注意不包括起点,起点幸福指数为0

		if (happy > maxHappiness)
		{
			ans = temp;
			maxHappiness = happy;
			maxAverage = aver;  //***
		}else if(happy == maxHappiness)
		{
			if (maxAverage < aver)
			{
				maxAverage = aver;
				ans = temp;
			}
		}
		temp.pop_back();
		return;
	}
	temp.push_back(v);
	for (int i = 0; i < pre[v].size(); i++)
	{
		DFS(pre[v][i]);
	}
	temp.pop_back();	
}

void dij(int st)
{
	fill(dis, dis+MAXN, INF);
	fill(w, w+MAXN, 0);
	fill(num, num+MAXN, 0);
	dis[st] = 0;
	w[st] = weight[st];
	num[st] = 1;


	for (int i = 0; i < n; i++)
	{
		int u = -1, MIN = INF;
		for (int j = 0; j < n; j++)
		{
			if (visit[j] == false && dis[j] < MIN)
			{
				u = j;
				MIN = dis[j];
			}
		}
		if (u == -1) return;
		visit[u] = true;
		for (int v = 0; v < n; v++)
		{
			if (visit[v] == false && G[u][v] != INF)
			{
				if (dis[u] + G[u][v] < dis[v])
				{
					dis[v] = dis[u] + G[u][v];
					num[v] = num[u];
					pre[v].clear();
					pre[v].push_back(u);
				}else if (dis[u] + G[u][v] == dis[v])
				{
					num[v] += num[u];
					pre[v].push_back(u);
				}
				
			}
		}
	}
}

int main()
{
	fill(G[0], G[0]+MAXN*MAXN, INF);	
	//fill(weight, weight+MAXN, INF);
	cin >> n >> k >> beginCity;
	int index = 0;
	num2City[index++] = beginCity;
	city2Num[beginCity] = index-1;

	weight[0] = 0;
	for (int i = 0; i < n - 1; i++)
	{
		string tempCity;
		int happy;
		cin >> tempCity >> happy;
		weight[index] = happy;
		city2Num[tempCity] = index;
		num2City[index++] = tempCity;		
	}
	for(int i =0; i < k; i++)
	{
		string v1, v2;
		cin >> v1 >> v2;
		int c;
		cin >> c;
		G[city2Num[v1]][city2Num[v2]] = c;
		G[city2Num[v2]][city2Num[v1]] = c;
	}

	dij(city2Num[beginCity]);
	DFS(city2Num["ROM"]);
	cout << num[city2Num["ROM"]] << " " << dis[city2Num["ROM"]]  <<  " " << maxHappiness <<" " <<  maxAverage << endl;
	//cout << beginCity;
	for (int i = ans.size()-1; i >= 0; i--)
	{
		if (i == 0)
		{
			cout << num2City[ans[i]];
		}else
		{
			cout << num2City[ans[i]] << "->" ;
		}
		
	}
	return 0;
}