Advertisement
pb_jiang

LC WEEKLY 346 T4 WA

May 20th, 2023
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  2. class Solution {
  3.     using ll = long long;
  4.     using pii = pair<int, int>;
  5.     vector<vector<ll>> dist;
  6. public:
  7.     vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges, int source, int dest, int target) {
  8.         constexpr int INF = INT_MAX / 2;
  9.        
  10.         dist = vector<vector<ll>>(1 << 7, vector<ll>(1 << 7, LLONG_MAX / 2));
  11.         for (int i = 0; i < (1 << 7); ++i) dist[i][i] = 0;
  12.         for (const auto& e: edges)
  13.             dist[e[0]][e[1]] = (e[2] == -1 ? INF : e[2]), dist[e[1]][e[0]] = (e[2] == -1 ? INF : e[2]);
  14.        
  15.         vector<ll> ans(n + 1, LLONG_MAX / 2);
  16.         vector<int> from(n + 1, -1);
  17.         ans[source] = 0;
  18.  
  19.         /*
  20.         for (int u = 1; u < n; ++u) {
  21.             for (const auto& e: edges) {
  22.                 if (e[0] != u && e[1] != u) continue;
  23.                 int s = u, t = e[0] == u ? e[1] : e[0];
  24.                 if (ans[s] != LLONG_MAX / 2 && ans[t] > ans[s] + e[2])
  25.                     ans[t] = ans[s] + e[2], from[t] = s;
  26.             }
  27.         }
  28.         */
  29.        
  30.         set<int> vis;
  31.         using pli = pair<ll, int>;
  32.         mpq<pli> q;
  33.         q.push({0, source});
  34.         while(q.empty() == false) {
  35.             auto [d, u] = q.top(); q.pop();
  36.             if (vis.count(u)) continue;
  37.             vis.insert(u);
  38.             ans[u] = min(ans[u], d);
  39.            
  40.             for (int v = 0; v < n; ++v) {
  41.                 ll w = dist[u][v];
  42.                 if (ans[v] > ans[u] + w) {
  43.                     ans[v] = ans[u] + w;
  44.                     from[v] = u;
  45.                     q.push({ans[v], v});
  46.                 }
  47.             }
  48.         }
  49.  
  50.        
  51.         /*
  52.         for (int k = 1; k <= n; ++k)
  53.             for (int u = 1; u <= n; ++u)
  54.                 for (int v = 1; v <= n; ++v)
  55.              
  56.         for (int i = 0; i < n; ++i)
  57.             cout << "ans[" << i << "]: " << ans[i] << endl;
  58.         */
  59.         if (ans[dest] < target) return {};
  60.         if (ans[dest] == target) {
  61.             for (auto& e: edges) {
  62.                 if (e[2] == -1) e[2] = INF;
  63.             }
  64.             return edges;
  65.         }
  66.        
  67.         for (const auto& e: edges)
  68.             if (e[2] == -1)
  69.                 dist[e[0]][e[1]] = dist[e[1]][e[0]] = 0;
  70.        
  71.         ans = vector<ll>(n + 1, LLONG_MAX / 2);
  72.         vis.clear();
  73.         q.push({0, source});
  74.         while(q.empty() == false) {
  75.             auto [d, u] = q.top(); q.pop();
  76.             if (vis.count(u)) continue;
  77.             vis.insert(u);
  78.             ans[u] = min(ans[u], d);
  79.            
  80.             for (int v = 0; v < n; ++v) {
  81.                 ll w = dist[u][v];
  82.                 if (ans[v] > ans[u] + w) {
  83.                     ans[v] = ans[u] + w;
  84.                     from[v] = u;
  85.                     q.push({ans[v], v});
  86.                 }
  87.             }
  88.         }
  89.  
  90.        
  91.         int block_cnt = 0;
  92.         int now = dest;
  93.         set<pii> tgt;
  94.         while(now != source) {
  95.             // cout << "now: " << now << " from: " << from[now] << endl;
  96.             if (dist[now][from[now]] == 0) ++block_cnt, tgt.insert({min(now, from[now]), max(now, from[now])});
  97.             now = from[now];
  98.         }
  99.         ll got = ans[dest];
  100.         ll req = target - got, avg = (block_cnt != 0 ? req / block_cnt : 0);
  101.         // cout << "block_cnt: " << block_cnt << endl;
  102.        
  103.         for (auto& e: edges) {
  104.             // cout << "e: " << e[0] << ',' << e[1] << ',' << e[2] << endl;
  105.             if (e[2] != -1) continue;
  106.            
  107.             int a = e[0], b = e[1];
  108.             // if (from[a] != b && from[b] != a) continue;
  109.             if (tgt.count({min(a, b), max(a, b)}) == 0) {
  110.                 e[2] = INF;
  111.                 continue;
  112.             }
  113.            
  114.             e[2] = (block_cnt == 1 ? req : avg);
  115.             req -= avg;
  116.             block_cnt--;
  117.         }
  118.         return edges;
  119.     }
  120. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement