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.

输入

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.

输出

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.

思路

此题不难,用深搜即可,关键是用map去转换。

代码

#include<cstdio>
#include<iostream>
#include<stdlib.h>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
#define INF  2047483647
int graph[300][300];
map<string, int> CityId;
map<int, string>CityName;
int vis[300] = {
    
     0 };
int happy[300] = {
    
     0 };
int N, K;
int mmin = INF;
int mmax = 0-INF;
int mend;
vector<int> ans;
vector<int> temp;
int number = 0;
void fu()
{
    
    
	ans.clear();
	for (int i = 0; i < temp.size(); i++)
	{
    
    
		ans.push_back(temp[i]);
	}
}
void dfs(int cur,int dis,int h)
{
    
    
	if (dis > mmin)
	{
    
    
		return;
	}
	if (cur == mend)
	{
    
    
		if (mmin > dis)
		{
    
    
			mmin = dis;
			fu();
			mmax = h;
			number = 1;
		}
		else if(mmin==dis)
		{
    
    
			number++;
			if (mmax < h)
			{
    
    
				mmax = h;
				fu();
			}
			else if (mmax == h)
			{
    
    
				if (((mmax * 1.0) / (ans.size() - 1) )< ((h * 1.0) / (temp.size() - 1)))
				{
    
    
					fu();
				}
			}
		}
		return;
	}
	vis[cur] = 1;
	for (int i = 1; i <= N; i++)
	{
    
    
		if (vis[i] == 0 && graph[cur][i]!=-1)
		{
    
    
			temp.push_back(i);
			dfs(i, dis + graph[cur][i], h + happy[i]);
			temp.pop_back();
		}
	}
	vis[cur] = 0;
}
int main()
{
    
    
	cin >> N >> K;
	for (int i = 1; i <= N; i++)
	{
    
    
		for (int j = 1; j <= N; j++)
		{
    
    
			graph[i][j] = -1;
		}
	}
	string start;
	cin >> start;
	CityId[start] = 1;
	CityName[1] = start;
	for (int i = 2; i <= N; i++)
	{
    
    
		string str;
		cin >> str >> happy[i];
		if (str == "ROM")
		{
    
    
			mend = i;
		}
		CityId[str] = i;
		CityName[i] = str;
	}
	for (int i = 0; i < K; i++)
	{
    
    
		string a, b;
		int d;
		cin >> a >> b >> d;
		graph[CityId[a]][CityId[b]] = d;
		graph[CityId[b]][CityId[a]] = d;
	}
	vis[1] = 1;
	temp.push_back(1);
	dfs(1,0,0);
	cout << number << " " << mmin << " " << mmax << " ";
	printf("%d\n", mmax / (ans.size() - 1));
	cout << CityName[1];
	for (int i = 1; i < ans.size(); i++)
	{
    
    
		cout << "->" << CityName[ans[i]];
	}
	cout << endl;
}

猜你喜欢

转载自blog.csdn.net/qq_45478482/article/details/120100325