Advertisement
Alex_tz307

USACO 2019 January Contest, Gold Problem 3. Shortcut

May 30th, 2021
1,039
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3.  
  4. using namespace std;
  5. using int64 = long long;
  6.  
  7. ifstream fin("shortcut.in");
  8. ofstream fout("shortcut.out");
  9.  
  10. const int MAXN = 1e4;
  11. const int MAXM = 5e4;
  12. int64 c[MAXN];
  13. vector<pair<int,int>> G[MAXN];
  14. int dp[MAXN], p[MAXN];
  15. vector<int> tree[MAXN];
  16.  
  17. void min_self(int &x, int y) {
  18.   x = min(x, y);
  19. }
  20.  
  21. void max_self(int64 &x, int64 y) {
  22.   x = max(x, y);
  23. }
  24.  
  25. void Dijkstra(int n) {
  26.   for (int u = 1; u < n; ++u)
  27.     dp[u] = p[u] = INF;
  28.   priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
  29.   pq.emplace(0, 0);
  30.   while (!pq.empty()) {
  31.     int d, u;
  32.     tie(d, u) = pq.top();
  33.     pq.pop();
  34.     if (dp[u] != d)
  35.       continue;
  36.     for (auto it : G[u]) {
  37.       int v, w;
  38.       tie(v, w) = it;
  39.       if (dp[v] >= d + w)
  40.         min_self(p[v], u);
  41.       if (dp[v] > d + w) {
  42.         dp[v] = d + w;
  43.         p[v] = u;
  44.         pq.emplace(dp[v], v);
  45.       }
  46.     }
  47.   }
  48. }
  49.  
  50. void dfs(int u) {
  51.   for (int v : tree[u]) {
  52.     dfs(v);
  53.     c[u] += c[v];
  54.   }
  55. }
  56.  
  57. int main() {
  58.   int n, m, T;
  59.   fin >> n >> m >> T;
  60.   for (int u = 0; u < n; ++u)
  61.     fin >> c[u];
  62.   for (int i = 0; i < m; ++i) {
  63.     int u, v, w;
  64.     fin >> u >> v >> w;
  65.     --u, --v;
  66.     G[u].emplace_back(v, w);
  67.     G[v].emplace_back(u, w);
  68.   }
  69.   Dijkstra(n);
  70.   for (int u = 1; u < n; ++u)
  71.     tree[p[u]].emplace_back(u);
  72.   dfs(0);
  73.   int64 ans = 0;
  74.   for (int u = 1; u < n; ++u)
  75.     max_self(ans, c[u] * (dp[u] - T));
  76.   fout << ans << '\n';
  77.   fin.close();
  78.   fout.close();
  79.   return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement