'commit'
This commit is contained in:
57
TangDou/XiTi/1410.cpp
Normal file
57
TangDou/XiTi/1410.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// AcWing 1128 信使【最短路】
|
||||
// https://blog.csdn.net/qq_43964401/article/details/109039446
|
||||
|
||||
|
||||
const int N = 110;
|
||||
int n, m;
|
||||
int d[N][N];
|
||||
|
||||
#define INF 0x3f3f3f3f
|
||||
|
||||
//floyd核心算法
|
||||
void floyd() {
|
||||
for (int k = 1; k <= n; k++)
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
|
||||
}
|
||||
|
||||
int main() {
|
||||
//输入+输出重定向
|
||||
freopen("../1410.txt", "r", stdin);
|
||||
|
||||
cin >> n >> m;
|
||||
|
||||
//初始化
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
if (i == j) d[i][j] = 0;
|
||||
else d[i][j] = INF;
|
||||
|
||||
//输入数据
|
||||
while (m--) {
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
d[a][b] = c, d[b][a] = c; //双向,无向图
|
||||
}
|
||||
//核心计算
|
||||
floyd();
|
||||
|
||||
//哪条路线最长
|
||||
int res = 0;
|
||||
for (int i = 1; i <= n; i++)
|
||||
res = max(res, d[1][i]);
|
||||
|
||||
//如果有不存在的,就返回-1
|
||||
if (res >= INF) puts("-1");
|
||||
else printf("%d\n", res);
|
||||
|
||||
//关闭文件
|
||||
fclose(stdin);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user