mr_dot_convict

296D-GregAndGraph-Codeforces-mr.convict

May 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. #pragma GCC      optimize ("Ofast")
  10. #pragma GCC      optimize ("unroll-loops")
  11. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  12.  
  13. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  14. #define PREC     cout.precision (10); cout << fixed
  15. #define x        first
  16. #define y        second
  17. using namespace std;
  18.  
  19. const int N = 505;
  20. const int infi = (int)1e9;
  21. const long long inf = (long long)1e18;
  22.  
  23. int n;
  24. int tmp[N][N], mp[N];
  25. int W[N][N];
  26. long long cost[N + 1];
  27.  
  28. void modFloydWarshall() {
  29.    for (int k = n; k >= 1; --k) {
  30.       cost[k] = 0;
  31.  
  32.       for (int i = 0; i < n; ++i)
  33.          for (int j = 0; j < n; ++j)
  34.             W[i][j] = min(W[i][j], W[i][k - 1] + W[k - 1][j]);
  35.  
  36.       for (int i = 0; i < n; ++i) {
  37.          for (int j = 0; j < n; ++j) {
  38.             if (i < k - 1 || j < k - 1) continue;
  39.             cost[k] += 1ll*W[i][j];
  40.          }
  41.       }
  42.    }
  43. }
  44.  
  45. void solve() {
  46.    for (int i = 0; i < n; ++i)
  47.       for (int j = 0; j < n; ++j)
  48.          W[i][j] = tmp[mp[i]][mp[j]];
  49.    modFloydWarshall();
  50.  
  51.    for (int k = 1; k <= n; ++k) {
  52.       cout << cost[k] << " \n"[k == n];
  53.    }
  54. }
  55.  
  56. void read() {
  57.    for (int i = 0; i < n; ++i) {
  58.       for (int j = 0; j < n; ++j) {
  59.          cin >> tmp[i][j];
  60.       }
  61.    }
  62.    for (int i = 0; i < n; ++i) {
  63.       cin >> mp[i];
  64.       --mp[i];
  65.    }
  66. }
  67.  
  68. signed main() {
  69.    IOS; PREC;
  70.    while (cin >> n) {
  71.       read();
  72.       solve();
  73.    }
  74.  
  75.    return EXIT_SUCCESS;
  76. }
Add Comment
Please, Sign In to add comment