Frogger最短路径典型题

Frogger最短路径典型题

  • 题目
    • 图解胜于一切苍白的文字
    • 思路
    • Dijkstra(迪杰斯特拉算法)
    • Bellman-Ford(贝尔曼-福特算法)
    • Floyd(弗洛伊德算法)

题目

大意是求一条通路中所有相邻两个结点的最大值,该最大值又是所有通路中的最小值。上图一目了然。
题目链接: link.

图解胜于一切苍白的文字

图片: Frogger最短路径典型题_第1张图片
结点1通往结点3的路径有:

  1. 1->2->3相邻两点间最大距离是 4
  2. 1->4->3相邻两点间最大距离是 5
  3. 1->4->6->3相邻两点间最大距离是 3
  4. 1->5->4->3相邻两点间最大距离是 5
  5. 1->5->4->6->3相邻两点间最大距离是 2
    故所有通路中所有相邻两个结点最大值的最小值是 通路5中:2

思路

这道题是变相的求源节点 **(本题为结点1)**到任意结点的所有路径上相邻两结点的最大距离的最小值。即动态规划的最有子解。

可以利用求最短路径的方法求解该问题。

Dijkstra(迪杰斯特拉算法)

#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 0x3f3f3f3f;
double Dist(vector const& a, vector const& b) {
	double t;
	t = sqrt(pow((double)(a[0] - b[0]), 2) + pow((double)(a[1] - b[1]), 2));
	return t;
}
int main() {
	int n;
	int inc(0);
	while (cin >> n && n) {
		vector dist(n + 1, MAX);
		vector visited(n + 1, false);
		vector > vec(n + 1, vector(2, 0));
		for (int i = 1; i <= n; i++) {
			cin >> vec[i][0] >> vec[i][1];
		}
		dist[1] = 0;
		for (int i = 2; i <= n; i++) {
			dist[i] = Dist(vec[1], vec[i]);
		}
		visited[1] = true;
		int u(1);
		double maxmin(-1);
		for (int m = 0; m < n;m++) {
			double mindist = MAX;
			for (int i = 2; i <= n; i++) {
				if ((!visited[i]) && mindist > dist[i]) {
					mindist = dist[i];
					u = i;
				}
			}
			visited[u] = true;
			for (int j = 2; j <= n; j++) {
				if (!visited[j]) {
					dist[j] = min(dist[j], max( Dist(vec[u], vec[j]),dist[u]));
				}
			}
		}
		cout << fixed << setprecision(3);
		cout << "Scenario #" << ++inc << endl;
		cout << "Frog Distance = " << dist[2] << endl;
		cout << endl;
	}

	
	system("pause");
	return 0;
}

在这里插入图片描述

Bellman-Ford(贝尔曼-福特算法)

#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 0x3f3f3f3f;
double Dist(vector const& a, vector const& b) {
	double t;
	t = sqrt(pow((double)(a[0] - b[0]), 2) + pow((double)(a[1] - b[1]), 2));
	return t;
}
void relax(vector >& edge, int i, int j, vector &dist) {
	if (dist[j] > max(dist[i], edge[i][j]))
		dist[j] = max(dist[i], edge[i][j]);
}
int main() {
	int n;
	int inc(0);
	while (cin >> n && n) {
		vector dist(n + 1, MAX);
		vector > edge(n+1,vector(n+1));
		vector > vec(n + 1, vector(2, 0));
		for (int i = 1; i <= n; i++) {
			cin >> vec[i][0] >> vec[i][1];
		}
		dist[1] = 0;
		for (int i = 1; i <= n; i++) {
			for(int j=1;j<=n;j++)
			edge[i][j] = Dist(vec[i], vec[j]);
		}
		for (int i = 1; i <= n; i++) {
			for (int s = 1; s <= n; s++) {
				for (int t = 1; t <= n; t++) {
					relax(edge, s, t,dist);
				}
			}
		}



		cout << fixed << setprecision(3);
		cout << "Scenario #" << ++inc << endl;
		cout << "Frog Distance = " << dist[2] << endl;
		cout << endl;
	}

	
	system("pause");
	return 0;
}

在这里插入图片描述

Floyd(弗洛伊德算法)

#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 0x3f3f3f3f;
double Dist(vector const& a, vector const& b) {
	double t;
	t = sqrt(pow((double)(a[0] - b[0]), 2) + pow((double)(a[1] - b[1]), 2));
	return t;
}
int main() {
	int n;
	int inc(0);
	while (cin >> n && n) {
		vector > dist(n + 1, vector(n+1,MAX));
		vector visited(n + 1, false);
		vector > vec(n + 1, vector(2, 0));
		for (int i = 1; i <= n; i++) {
			cin >> vec[i][0] >> vec[i][1];
		}
		for (int i = 1; i <= n; i++) {
			for(int j=1;j<=n;j++)
				dist[i][j] = Dist(vec[i], vec[j]);
		}
		for (int k = 1; k <= n; k++) {
			for (int i = 1; i <= n; i++) {
				if (i == k) continue;
				for (int j = 1; j <= n; j++) {
					if (j == k) continue;
					if (dist[i][j] > max(dist[i][k], dist[k][j])) {
						dist[i][j] = max(dist[i][k], dist[k][j]);
					}
				}
			}
		}



		cout << fixed << setprecision(3);
		cout << "Scenario #" << ++inc << endl;
		cout << "Frog Distance = " << dist[1][2] << endl;
		cout << endl;
	}

	
	system("pause");
	return 0;
}

在这里插入图片描述

你可能感兴趣的:(Frogger最短路径典型题)