POJ 2253 floyd 最短路

本想二分答案,后来发现只要修改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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1) { JOptionPane.showMessageDialog(this, "请选择要删除的图书!", "提示", JOptionPane.WARNING_MESSAGE); } else { 这是一道经典的最短路问题,可以使用Dijkstra算法或者Floyd算法来解决。 D String bookId = bookInfoTable.getValueAt(selectedRow, 0).toString(); int option = JOptionPane.showConfirmDialog(this, "确定要删除编号为 " + bookId + " 的图书吗?", "提示", JOptionPane.YES_NO_OPTION); if (optionijkstra算法的基本思想是从起点开始,依次遍历所有节点,每次选择距离起点最 == JOptionPane.YES_OPTION) { deleteBookInfo(bookId); loadBookInfo(); } } } else if (e.getSource() == backButton) { new MainFrame(); setVisible(false); } } private void loadBookInfo() { 近的节点进行松弛操作,直到到达终点或者所有节点都被遍历完。 Floyd算法则 tableModel.setRowCount(0); Connection conn = null; Statement stmt = null; ResultSet rs = null; try { 是通过动态规划求解所有节点之间的最短路径。具体来说,利用一个二维数组存 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book_management?useSSL=false&serverTimezone=UTC", "root", "root"); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM book_info"); while (rs.next())储任意两个节点之间的最短距离,然后依次考虑增加中间节点,更新所有节点 { Object[] rowData = new Object[6]; rowData[0] = rs.getInt("book_id"); rowData[1]之间的距离,直到所有节点都被考虑过。 无论使用哪种算法,都需要先构建 = rs.getString("book_name"); rowData[2] = rs.getString("author"); rowData[3] = rs.getString("publisher"); rowData[4] = rs.getDouble("price"); rowData[5] = rs.getInt("quantity"); tableModel.addRow(row出图的邻接矩阵或邻接表,然后根据算法的要求进行处理即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值