Alex_tz307

USACO 2020 February Contest, Gold Problem 1. Timeline

May 31st, 2021
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. ifstream fin("timeline.in");
  8. ofstream fout("timeline.out");
  9.  
  10. const int MAXN = 1e5;
  11. int low[MAXN], in_deg[MAXN], dp[MAXN];
  12. vector<pair<int,int>> G[MAXN];
  13.  
  14. void max_self(int &x, int y) {
  15.   x = max(x, y);
  16. }
  17.  
  18. int main() {
  19.   int n, m, k;
  20.   fin >> n >> m >> k;
  21.   for (int u = 0; u < n; ++u)
  22.     fin >> low[u];
  23.   for (int i = 0; i < k; ++i) {
  24.     int u, v, x;
  25.     fin >> u >> v >> x;
  26.     --u, --v;
  27.     ++in_deg[v];
  28.     G[u].emplace_back(v, x);
  29.   }
  30.   queue<int> q;
  31.   for (int u = 0; u < n; ++u)
  32.     if (in_deg[u] == 0)
  33.       q.emplace(u);
  34.   vector<int> top_sort;
  35.   while (!q.empty()) {
  36.     int u = q.front();
  37.     q.pop();
  38.     top_sort.emplace_back(u);
  39.     for (auto v : G[u])
  40.       if (--in_deg[v.first] == 0)
  41.         q.emplace(v.first);
  42.   }
  43.   for (int u : top_sort) {
  44.     max_self(dp[u], low[u]);
  45.     for (auto v : G[u])
  46.       max_self(dp[v.first], dp[u] + v.second);
  47.   }
  48.   for (int u = 0; u < n; ++u)
  49.     fout << dp[u] << '\n';
  50.   return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment