【笨方法学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

二、题目大意

求从起点到ROM所需要的最少花费,并输出其路径。如果路径有多条,给出幸福值最大的那条。如果仍然不唯一,选择路径上的城市平均幸福值最大的那条路径。

三、考点

DFS

四、注意

1、需要map实现string与int的互相映射,其他比较常规。

五、代码

#include<iostream>
#include<string>
#include<map>
#include<vector>
#define N 210
#define INF 9999999
using namespace std;
map<string, int> map_str_int;
map<int, string> map_int_str;
int mstart=1, mend;
vector<int> vec_happy;
int e[N][N];
bool visit[N] = { false };
int out_happy=0, out_cost=INF, out_avg=0, out_num=0;
vector<int> vec_out_path, vec_path;
int n, k;

void dfs(int root, int cost, int happy) {
	//return
	if (cost > out_cost)
		return;

	//arrive at ROM
	//min cost, max happy, max avg
	if (root == mend) {
		if (cost < out_cost) {
			out_cost = cost;
			out_happy = happy;
			out_num = 1;
			out_avg = happy / vec_path.size();
			vec_out_path = vec_path;
		}
		else if (cost == out_cost) {
			out_num++;
			if (happy > out_happy || happy == out_happy && out_avg < happy / vec_path.size()) {
				out_happy = happy;
				out_avg = happy / vec_path.size();
				vec_out_path = vec_path;
			}
		}
		return;
	}

	//next dfs
	for (int i = 1; i <= n; ++i) {
		if (visit[i] == false && e[root][i] != INF) {
			visit[i] = true;
			vec_path.push_back(i);
			dfs(i, cost + e[root][i], happy + vec_happy[i]);
			visit[i] = false;
			vec_path.pop_back();
		}
	}

	return;
}

int main() {
	//init
	fill(e[0], e[0] + N*N, INF);
	fill(visit, visit + N, false);

	//read
	string str_begin;
	cin >> n >> k >> str_begin;
	vec_happy.resize(n + 1);
	vec_happy[0] = vec_happy[1] = 0;
	map_str_int[str_begin] = mstart;
	map_int_str[mstart] = str_begin;
	for (int i = 2; i <= n; ++i) {
		string s;
		cin >> s >> vec_happy[i];
		if (s == "ROM")
			mend = i;
		map_str_int[s] = i;
		map_int_str[i] = s;
	}

	for (int i = 0; i < k; ++i) {
		string s1, s2;
		int a, b, c;
		cin >> s1 >> s2 >> c;
		a = map_str_int[s1];
		b = map_str_int[s2];
		e[a][b] = e[b][a] = c;
	}

	//dfs
	vec_path.push_back(mstart);
	visit[mstart] = true;
	dfs(mstart, 0, 0);

	//output
	printf("%d %d %d %d\n", out_num, out_cost, out_happy, out_happy/(vec_out_path.size()-1));
	for (int i = 0; i < vec_out_path.size(); ++i) {
		if (i != 0)
			cout << "->";
		cout << map_int_str[vec_out_path[i]];
	}
	cout << endl;

	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值