码迷,mamicode.com
首页 > 其他好文 > 详细

AOJ 0189 Convenient Location (Floyd)

时间:2017-08-17 22:49:41      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:otto   for   algorithm   pad   iostream   out   edit   ace   一个   

题意:

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

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

#include <iostream>
#include <stdio.h>
#include <algorithm> 
#include <cstring>
#include <string>
using namespace std;
const int INF = 0x3f3f3f3f;

int n, dp[15][15];

int main()
{
	while (cin >> n && n) 
	{
		int V = 0;
		memset(dp, INF, 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;
}

AOJ 0189 Convenient Location (Floyd)

标签:otto   for   algorithm   pad   iostream   out   edit   ace   一个   

原文地址:http://www.cnblogs.com/demian/p/7384704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!