You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.4 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int INF = 0x3f3f3f3f;
const int N = 110;
2 years ago
int dis[N][N], g[N][N];
2 years ago
int n, m, a, b, c, ans;
void floyd() {
for (int k = 1; k <= n; k++) {
for (int i = 1; i < k; i++) // 枚举ij
for (int j = i + 1; j < k; j++) // 注意ijk不能相同
2 years ago
if (ans > dis[i][j] + g[j][k] + g[k][i])
ans = dis[i][j] + g[j][k] + g[k][i];
2 years ago
for (int i = 1; i <= n; i++) // 原floyd
for (int j = 1; j <= n; j++)
2 years ago
if (dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
2 years ago
}
}
signed main() {
while (cin >> n >> m && (~n && ~m)) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i == j)
2 years ago
dis[i][j] = g[i][j] = 0;
2 years ago
else
2 years ago
dis[i][j] = g[i][j] = INF;
2 years ago
while (m--) {
cin >> a >> b >> c;
2 years ago
if (c < dis[a][b]) // 防重边,怕了怕了
dis[a][b] = g[a][b] = c;
if (c < dis[b][a])
dis[b][a] = g[b][a] = c;
2 years ago
}
ans = INF;
floyd();
if (ans == INF)
puts("It's impossible.");
else
printf("%lld\n", ans);
}
}