Frogger最短路径典型题

题目

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

图解胜于一切苍白的文字

图片: 结点1-3的通路
结点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<iostream>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
const int MAX = 0x3f3f3f3f;
double Dist(vector<int> const& a, vector<int> 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<double> dist(n + 1, MAX);
		vector<bool> visited(n + 1, false);
		vector<vector<int> > vec(n + 1, vector<int>(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<iostream>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
const int MAX = 0x3f3f3f3f;
double Dist(vector<int> const& a, vector<int> 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<vector<double> >& edge, int i, int j, vector<double> &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<double> dist(n + 1, MAX);
		vector<vector<double> > edge(n+1,vector<double>(n+1));
		vector<vector<int> > vec(n + 1, vector<int>(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<iostream>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
const int MAX = 0x3f3f3f3f;
double Dist(vector<int> const& a, vector<int> 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<vector<double> > dist(n + 1, vector<double>(n+1,MAX));
		vector<bool> visited(n + 1, false);
		vector<vector<int> > vec(n + 1, vector<int>(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;
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值