Advertisement
DylanHarris

QBBUILD - SPOJ

Dec 28th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cstring>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int a[101][101];
  7.  
  8. #define forn(i) for (int i = 1; i <= n; i++)
  9.  
  10. int main() {
  11.     ios::sync_with_stdio(false); cin.tie(0);
  12.  
  13.     int n; cin >> n;
  14.  
  15.     int s[4];
  16.     for (int &u : s) cin >> u;
  17.  
  18.     forn(i) forn(j) a[i][j] = 1e8;
  19.     forn(i) a[i][i] = 0;
  20.     for (int u, v, w; cin >> u >> v >> w;) {
  21.         a[u][v] = a[v][u] = w;
  22.     }
  23.  
  24.     // Floyd
  25.     forn(k) forn(i) forn(j) {
  26.         if (a[i][j] > a[i][k] + a[k][j]) a[i][j] = a[i][k] + a[k][j];
  27.     }
  28.  
  29.     // Create all permutation of `s` and put them in `permute`
  30.     sort(s, s + 4);
  31.     int permute[96];
  32.     for (int i = 0; i < 24; i++) {
  33.         next_permutation(s, s + 4);
  34.         memcpy(permute + 4 * i, s, 16);
  35.     }
  36.  
  37.     int res = 1e8;
  38.     forn(u) forn(v) {
  39.         for (int i = 0; i < 24; i++) {
  40.             int x = a[u][v];
  41.             x += a[u][permute[0 + 4 * i]];
  42.             x += a[u][permute[1 + 4 * i]];
  43.             x += a[v][permute[2 + 4 * i]];
  44.             x += a[v][permute[3 + 4 * i]];
  45.             res = min(res, x);
  46.         }
  47.     }
  48.     cout << res << endl;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement