PAT-1087-最短路径问题+DFS

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

谷歌中文翻译:
确实,从我们的城市到罗马有许多不同的旅游路线。您应该以最低的成本找到客户的路线,同时获得最大的幸福。

输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行包含2个正整数N(2≤N≤200)(城市数)和K(城市对之间的路线总数);然后是起始城市的名称。接下来的N-1行分别给出一个城市的名称和一个整数,代表一个人可以从该城市(起始城市除外)获得的幸福。然后是K行,每行以City1 City2 Cost格式描述两个城市之间的路线。这里的城市名称是由3个大写英文字母组成的字符串,目的地始终是代表罗马的ROM。

输出规格:
对于每个测试用例,我们都应该找到成本最低的路线。如果这样的路线不是唯一的,那么将建议人们获得最大的幸福。如果这样的路线仍然不是唯一的,那么我们输出的平均幸福感最大-法官保证这样的解决方案存在并且是唯一的。

因此,在输出的第一行中,您必须打印4个数字:所建议路线的成本最小,成本,幸福度和平均幸福度(仅取整数部分)的不同路线数。然后在下一行中,您应该以City1-> City2-> …-> ROM格式打印路线。

思路:
首先当然是处理最短路径找到存入pre里,然后才是dfs的判断。dfs首先要计算出总体幸福感,然后计算出平均幸福感,接下来两个判断解决。
在这里要注意的是第一个点也就是起始点不需要考虑太多,因为起点固定并且不做多余处理。

#include<bits/stdc++.h>
using namespace std;
const int INF = 100000000;
const int MAX_N = 510;

int n, m;	 
string s;	//起点 
int e[MAX_N][MAX_N];
int dis[MAX_N];
bool visit[MAX_N];
map<string, int> m1;
map<int, string> m2;
int maxvalue = 0;
int mindepth = 0;
int countpath = 0;
int weight[MAX_N];
double maxavg;
vector<int> pre[MAX_N];
vector<int> path, temppath;

void init() {
	fill(e[0], e[0] + MAX_N * MAX_N, INF);
	fill(dis, dis + MAX_N, INF);
	cin >> n >> m >> s;
	m1[s] = 1;
	m2[1] = s;
	string stemp;
	for(int i = 1; i < n; i++) {
		//因为起始点不计入 从weight[2]开始算 
		cin >> stemp >> weight[i+1];
		m1[stemp] = i + 1;
		m2[i + 1] = stemp;
	}
	string sa, sb;
	int temp;
	for(int i = 0; i < m; i++) {
		cin >> sa >> sb >> temp;
		e[m1[sa]][m1[sb]] = temp;
		e[m1[sb]][m1[sa]] = temp;
	}
}

void dijkstra() {
	dis[1] = 0;
	for(int i = 0; i < n; i++) {
		int u = -1;
		int minn = INF;
		for(int j = 1; j <= n; j++) {
		 //不需要考虑起始点 
		 	if(visit[j] == false && dis[j] < minn) {
				u = j;
				minn = dis[j];
			}
		} 
		if(u == -1) break;
		visit[u] = true;
		for(int	v = 1; v <= n; v++) {
			if(visit[v] == false && e[u][v] != INF) {
				if(dis[v] > dis[u] + e[u][v]) {
					dis[v] = dis[u] + e[u][v];
					pre[v].clear();
					pre[v].push_back(u); 
				} else if(dis[v] == dis[u] + e[u][v]) {
					pre[v].push_back(u);
				}
			}
		}
	}
}

void dfs(int v) {
	temppath.push_back(v);
	if(v == 1) {
		int value = 0;
		for(int i = 0; i < temppath.size(); i++) {
			value += weight[temppath[i]]; //总幸福感 
		}
		double tempavg = ((double)value) / (temppath.size() - 1);
		if(value > maxvalue) {
			maxvalue = value;
			maxavg = tempavg;
			path = temppath;
		} else if(value == maxvalue && tempavg > maxavg) {
			maxavg = tempavg;
			path = temppath;
		}
		temppath.pop_back();
		countpath++;
		return ;
	}
	for(int i = 0; i < pre[v].size(); i++) {
		dfs(pre[v][i]);
	}
	temppath.pop_back();
}


int main() {
	init();
	dijkstra();
	dfs(m1["ROM"]);
	printf("%d %d %d %d\n", countpath, dis[m1["ROM"]], maxvalue, (int)maxavg);
    for(int i = path.size() - 1; i >= 1; i--) {
        cout << m2[path[i]] << "->";
    }
    cout << "ROM";
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值