PAT 1087 最短路径

题目

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 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

思路

  1. 读完题就知道是最短路径(边权 + 点权)问题,但是这里给出了另一个约束条件,就是平均点权
  2. 首先观察输入,这里输入不像之前的题目,给的都是数字,这里给的是字符串,所以需要一个字符串向整数和整数向字符串的转换过程,题目说点的个数并不多,所以直接用map和string也不用担心超时问题
  3. 有了转换以后就可以变成标准的Dijkstra问题了,这里有两种思路
  4. 注意平均点权的时候不计算起始节点

Dijkstra + DFS

  1. 这样思路比较直观,先通过Dijkstra算法得到所有的最短路径,然后再去比哪一个是符合要求的
  2. 好处是不需要记录每个节点所在的最短路径上结点的个数
  3. 关于DIjkstra + DFS 的具体细节在之前已经详细叙述了,就需要注意一下什么时候pop什么时候push和递归的出口怎么处理就可以了
void Dijkstra(int s) {
	fill(d, d + maxv, INF);
	d[s] = 0;
	for (int i = 0; i < n; i++) {
		int u = -1, MIN = INF;
		for (int j = 0; j < n; j++) {
			if (visit[j] == false && d[j] < MIN) {
				u = j;
				MIN = d[j];
			}
		}
		if (u == -1) return;
		visit[u] = true;
		for (int v = 0; v < n; v++) {
			if (visit[v] == false && G[u][v] != INF) {
				if (d[u] + G[u][v] < d[v]) {
					d[v] = d[u] + G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if (d[u] + G[u][v] == d[v]) {
					pre[v].push_back(u);
				}
			}
		}
	}
}

void DFS(int v) {
	if (v == st) {
		tempPath.push_back(v);
		numPath++;
		int tempW = 0;
		for (int i = tempPath.size() - 2; i >= 0; i--) {
			int id = tempPath[i];
			tempW += weight[id];
		}
		double tempAvg = 1.0 * tempW / (tempPath.size() - 1);
		if (tempW > maxW) {
			maxW = tempW;
			maxavg = tempAvg;
			path = tempPath;
		}
		else if (tempW == maxW && tempAvg > maxavg) {
			maxavg = tempAvg;
			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();
}

Dijkstra

  1. 只使用Dijkstra算法的话,再具体内部实现上就比较麻烦
  2. 首先需要记录每个最短路上每个节点的个数,要搞清楚节点个数,权重 ,最短路径的个数,前驱节点这四者的继承关系
  3. 然后就是初始化问题,要初始化距离数组dis,路径个数数组num,节点个数数组pt,前驱数组pre(初始化为pre[i] = i),权重数组w。
void Dijkstra(int s) {
	fill(dis, dis + maxn, INF);
	fill(num, num + maxn, 0);
	fill(w, w + maxn, 0);
	fill(pt, pt + maxn, 0);
	dis[s] = 0;
	num[s] = 1;
	w[s] = weight[s];
	for (int i = 0; i < n; i++) pre[i] = i;
	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) {
				min = dis[j];
				u = j;
			}
		}
		if (u == -1) return;
		visit[u] = true;
		for (int v = 0; v < n; v++) {
			if (G[u][v] != INF && visit[v] == false) {
				if (dis[v] > G[u][v] + dis[u]) {
					dis[v] = G[u][v] + dis[u];
					w[v] = w[u] + weight[v];
					pt[v] = pt[u] + 1;
					num[v] = num[u];
					pre[v] = u;
				}
				else if (dis[v] == G[u][v] + dis[u]) {
					num[v] += num[u];
					if (w[v] < w[u] + weight[v]) {
						w[v] = w[u] + weight[v];
						pt[v] = pt[u] + 1;
						pre[v] = u;
					}
					else if (w[v] == w[u] + weight[v]) {
						double uavg = 1.0 * (w[u] + weight[v]) / (pt[u] + 1);
						double vavg = 1.0 * w[v] / pt[v];
						if (uavg > vavg) {
							pt[v] = pt[u] + 1;
							pre[v] = u;

						}
					}
				}
			}
		}
	}
}

void printPath(int v) {
	if (v == 0) {
		cout << indexTocity[v];
		return;
	}
	printPath(pre[v]);
	cout << "->" << indexTocity[v];
}

主函数部分

以只使用Dijkstra算法为例,两个是大同小异的

int main() {
	string start, city1, city2;
	cin >> n >> k >> start;
	cityToIndex[start] = 0;
	indexTocity[0] = start;
	for (int i = 1; i < n; i++) {
		cin >> city1 >> weight[i];
		cityToIndex[city1] = i;
		indexTocity[i] = city1;
	}
	fill(G[0], G[0] + maxn * maxn, INF);
	for (int i = 0; i < k; i++) {
		cin >> city1 >> city2;
		int c1 = cityToIndex[city1];
		int c2 = cityToIndex[city2];
		cin >> G[c1][c2];
		G[c2][c1] = G[c1][c2];
	}
	Dijkstra(0);
	int rom = cityToIndex["ROM"];
	printf("%d %d %d %d\n", num[rom], dis[rom], w[rom], w[rom] / pt[rom]);
	printPath(rom);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值