Greg and Graph (Floyd)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 5e2 + 5; int dist[N][N], n, x[N]; ll ans[N]; bitset <N> vis; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> dist[i][j]; } } for (int i = 1; i <= n; i++) { cin >> x[i]; vis[x[i]] = 1; } for (int tail = n; tail >= 1; tail--) { int k = x[tail]; vis[k] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) continue; dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); if (!vis[i] && !vis[j]) ans[tail] += dist[i][j]; } } } for (int i = 1; i <= n; i++) { cout << ans[i] << " \n"[i == n]; } return 0; }