AOJ 0189 Convenient Location (Floyd)

本文介绍了一个基于Floyd算法的经典问题实现——求解某办公室到其他所有办公室的最短总距离。通过多组输入数据,展示了如何利用三维动态规划矩阵更新节点间路径,最终找到使得总距离最小的起始办公室。

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

题意:

求某一个办公室 到其他所有办公室的 总距离最短  办公室数 不超过10

输入:

多组输入,每组第一行为n (1 ≤ n ≤ 45),接下来n行是 (x, y, d),x到y的距离是d
输出:

办公室号 和 最短距离


Floyd水题 - -

AC代码如下:

//
//  AOJ 0189 Convenient Location
//
//  Created by TaoSama on 2015-03-20
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n, dp[15][15];

int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);

	while(cin >> n && n) {
		int V = 0;
		memset(dp, 0x3f, sizeof dp);
		for(int i = 1; i <= n; ++i) {
			int x, y, v; cin >> x >> y >> v;
			dp[x][y] = dp[y][x] = v;
			V = max(V, max(x, y));
		}
		for(int k = 0; k <= V; ++k)
			for(int i = 0; i <= V; ++i)
				for(int j = 0; j <= V; ++j)
					dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);

		int ans = INF, loc;
		for(int i = 0; i <= V; ++i) {
			int t = 0;
			for(int j = 0; j <= V; ++j) {
				if(i == j) continue;
				t += dp[i][j];
			}
			if(t < ans) ans = t, loc = i;
		}
		cout << loc << ' ' << ans << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值