Flickyyy

B-A'2

Aug 30th, 2022
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define all(a) a.begin(), a.end()
  4. #define forn(i, a, b, delta) for (ll i = a; i < b; i += delta)
  5. #define FAST ios_base::sync_with_stdio(false); cin.tie(nullptr)
  6. using ll = long long;
  7. const ll INFIN = 1'000'000'000LL * 3'000LL;
  8. ll n;
  9.  
  10. vector<ll> path(ll u, ll v, const vector< vector<ll> >& p) {
  11.     if (p[u][v] == -1) return {};
  12.     vector<ll> left, right;
  13.     ll mid = p[u][v];
  14.     left = path(u, mid, p);
  15.     right = path(mid, v, p);
  16.     left.push_back(mid);
  17.     for (auto& e : right) {
  18.         left.push_back(e);
  19.     }
  20.     return left;
  21. }
  22.  
  23. ll test(vector< vector<ll> > weight_copy, vector< vector<ll> > weight, pair<ll, ll> delet) {
  24.     ll ans = 0;
  25.     vector< vector<ll> > weight2(n, vector<ll>(n));
  26.     forn(i, 0, n, 1) {
  27.         forn(j, 0, n, 1) {
  28.             weight2[i][j] = (weight_copy[i][j] <= 0 ? INFIN : weight_copy[i][j]);
  29.         }
  30.     }
  31.     weight2[delet.first][delet.second] = INFIN;
  32.     weight2[delet.second][delet.first] = INFIN;
  33.     forn(i, 0, n, 1) {
  34.         forn(u, 0, n, 1) {
  35.             forn(v, 0, n, 1) {
  36.                 ll mb = weight2[u][i] + weight2[i][v];
  37.                 if (mb < weight2[u][v] and weight2[u][i] < INFIN and weight2[i][v] < INFIN) {
  38.                     //update:
  39.                     weight2[u][v] = mb;
  40.                 }
  41.             }
  42.         }
  43.     }
  44.  
  45.     ans = 0;
  46.     forn(v, 1, n, 1) {
  47.         if (weight[0][v] != weight2[0][v]) ans += 1;
  48.     }
  49.     return ans;
  50. }
  51.  
  52. int main() {
  53.     FAST;
  54.      cin >> n;
  55.     vector< vector<ll> > weight(n, vector<ll>(n)), p(n, vector<ll>(n, -1));
  56.     forn(i, 0, n, 1) {
  57.         forn(j, 0, n, 1) {
  58.             ll w; cin >> w;
  59.             weight[i][j] = (w <= 0 ? INFIN : w);
  60.         }
  61.     }
  62.     auto weight_copy = weight;
  63.     forn(i, 0, n, 1) {
  64.         forn(u, 0, n, 1) {
  65.             forn(v, 0, n, 1) {
  66.                 ll mb = weight[u][i] + weight[i][v];
  67.                 if (mb < weight[u][v] and weight[u][i] < INFIN and weight[i][v] < INFIN) {
  68.                     //update:
  69.                     weight[u][v] = mb;
  70.                     p[u][v] = i;
  71.                 }
  72.             }
  73.         }
  74.     }
  75.  
  76.     map<pair<ll, ll>, ll> uses;
  77.     forn(i, 1, n, 1) {
  78.         if (weight[0][i] == INFIN) continue;
  79.         vector<ll> travel;
  80.         travel.push_back(0);
  81.         for (auto&& e : path(0, i, p)) travel.push_back(e);
  82.         travel.push_back(i);
  83.         forn(j, 1, travel.size(), 1) {
  84.             uses[{travel[j - 1], travel[j]}] += 1;
  85.         }
  86.     }
  87.  
  88.     ll ans = 0;
  89.     pair<ll, ll> delet;
  90.     //cerr << "map of uses:\n";
  91.     for (auto [a, b] : uses) {
  92.         if (ans < b) {
  93.             ans = max(ans, test(weight_copy, weight, a));
  94.             delet = a;
  95.         }
  96.         //cerr << "{ " << a.first << ", " << a.second << " } " << " : " << b << endl;
  97.     }
  98.     //cout << ans << endl;
  99.  
  100.     cout << ans << endl;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment