POJ 2253 floyd 最短路

本文介绍了一种改进的Floyd算法,用于求解特定条件下的最短路径问题。通过修改传统Floyd算法的方程,避免了不必要的二分查找过程,提高了计算效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本想二分答案,后来发现只要修改floyd方程即可

#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef  long long LL;
const double PI = acos(-1.0);

template <class T> inline  T MAX(T a, T b){if (a > b) return a;return b;}
template <class T> inline  T MIN(T a, T b){if (a < b) return a;return b;}

const int N = 222;
const int M = 11111;
const LL MOD = 1000000007LL;
const int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
const int INF = 0x3f3f3f3f;

double dist[N][N], op[N][N], d[N];
int vis[N];

int n;
struct node
{
	double x, y;
}pt[N];

double cal(double x1, double y1, double x2, double y2)
{
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}



int main()
{
	int cas = 1;
	while (scanf("%d", &n) != EOF && n)
	{
		int i, j, k;
		double mid, l, r, t;
		for (i = 0; i < n; ++i)
		{
			scanf("%lf%lf", &pt[i].x, &pt[i].y);
		}
		for (i = 0; i < n; ++i)
			for (j = 0; j < n; ++j)
			{
				if (i == j) dist[i][j] = 0.0;
				if (i < j ) dist[i][j] = cal(pt[i].x, pt[i].y, pt[j].x, pt[j].y);
				if (i > j) dist[i][j] = dist[j][i];
			}

			for (k = 0; k < n; ++k)
				for (i = 0; i < n; ++i)
					for (j = i + 1; j < n; ++j)
					{
						if (dist[i][j] > dist[i][k] && dist[i][j] > dist[k][j])
						{
							if (dist[i][k] < dist[k][j])
								dist[i][j] = dist[j][i] = dist[k][j];
							else
								dist[i][j] = dist[j][i] = dist[i][k];
						}
					}
		printf("Scenario #%d\n", cas++);
		printf("Frog Distance = %.3lf\n\n", dist[0][1]);
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值