AOJ0189
来源:互联网 发布:matlab二分法求解编程 编辑:程序博客网 时间:2024/06/06 05:15
题意
求某一办公室到其他办公室的最短距离。
多组输入,n表示n条关系,下面n次每次输入 x y d表示x到y的距离是d。需要注意的是n没有给定,需要根据输入来求。
输出办公室的编号和距离。
思路
任意两点之间的最短距离用floyd算法比较合适。
代码
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int N = 10;const int M = 10;const int INF = 0x3f3f3f3f;int d[N][N];int n;void solve(){ for (int k = 0; k < n; k ++) { for (int i = 0; i < n; i ++) { for (int j = 0; j < n; j ++) { d[i][j] = min(d[i][j], d[i][k]+d[k][j]); } } } int sum, msum = INF; int id = 0; for (int i = 0; i < n; i ++) { sum = 0; for (int j = 0; j < n; j ++) { if (i == j) continue; sum += d[i][j]; } if (sum < msum) { msum = sum; id = i; } } printf("%d %d\n", id, msum);}int main(void){ int m; while ( cin >> m && m ) { for (int i = 0; i < N; i ++) fill(d[i], d[i]+N, INF); n = 0; while ( m-- ) { int a, b, c; scanf("%d%d%d", &a, &b, &c); d[a][b] = d[b][a] = c; n = max(max(n, a+1), b+1); } solve(); } return 0;}
阅读全文
0 0
- AOJ0189
- aoj0189 Convenient Location floyd-warshall最短路
- 《挑战程序设计竞赛》2.5.1 最短路 AOJ0189 2249 2200 POJ3255 2139 3259 3268(5)
- IO流
- 【TensorFlow】LSTM(使用TFLearn预测正弦sin函数)
- C,C++,C#三者区别
- Java Web中的相对路径与绝对路径总结
- 贪吃蛇的玩法升级
- AOJ0189
- java 开发模式之三 : 装饰者模式
- 论文笔记-Computer Models Solving Intelligence Test Problems: Progress and Implications
- 分支中switch语句
- MySQL:Access denied for user 'root'@'localhost' (using password:YES)
- centos搭建hadoop单机模式与伪分布模式
- Math-9.Palindrome Number
- Spring boot遇到的thymeleaf一些问题
- 机器学习:如何找到最优学习率