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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int INF = 0x3f3f3f3f;
const int N = 110;
int mp[N][N], dis[N][N];
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不能相同
if (ans > mp[i][j] + dis[j][k] + dis[k][i])
ans = mp[i][j] + dis[j][k] + dis[k][i];
for (int i = 1; i <= n; i++) // 原floyd
for (int j = 1; j <= n; j++)
if (mp[i][j] > mp[i][k] + mp[k][j])
mp[i][j] = mp[i][k] + mp[k][j];
}
}
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)
mp[i][j] = dis[i][j] = 0;
else
mp[i][j] = dis[i][j] = INF;
while (m--) {
cin >> a >> b >> c;
if (c < mp[a][b]) // 防重边,怕了怕了
mp[a][b] = dis[a][b] = c;
if (c < mp[b][a])
mp[b][a] = dis[b][a] = c;
}
ans = INF;
floyd();
if (ans == INF)
puts("It's impossible.");
else
printf("%lld\n", ans);
}
}