Alex_tz307

USACO 2020 January Contest, Gold Problem 1. Time is Mooney

Mar 14th, 2021 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("time.in");
  7. ofstream fout("time.out");
  8.  
  9. class edge {
  10.     public:
  11.         int u, v;
  12.  
  13.         void read() {
  14.             fin >> u >> v;
  15.         }
  16. };
  17.  
  18. const int NMAX = 1 << 10;
  19. const int MAXM = 1 << 11;
  20. const int TMAX = 1 << 10;
  21. int N, M, C, a[NMAX], dp[2][NMAX], ind = 1, ans;
  22. edge edges[MAXM];
  23.  
  24. /// dp[t][u] - valoarea maxima ce o pot obtine daca la timpul t sunt in u.
  25. /// Valorile lui t sunt relevante pana la maxim 1000, pentru ca pentru t > 1000
  26. /// 1000 * t - t * t, care este valoarea ce o pot acumula in cel mai bun caz, devine negativa.
  27.  
  28. void max_self(int &a, int b) {
  29.     a = max(a, b);
  30. }
  31.  
  32. int main() {
  33.     fin >> N >> M >> C;
  34.     for(int i = 1; i <= N; ++i)
  35.         fin >> a[i];
  36.     for(int i = 1; i <= M; ++i)
  37.         edges[i].read();
  38.     for(int i = 0; i < 2; ++i)
  39.         for(int u = 1; u <= N; ++u)
  40.             dp[i][u] = -INF;
  41.     dp[0][1] = 0;
  42.     for(int t = 1; t < TMAX; ++t, ind ^= 1) {
  43.         for(int u = 1; u <= N; ++u)
  44.             dp[ind][u] = -INF;
  45.         for(int i = 1; i <= M; ++i) {
  46.             int u = edges[i].u,
  47.                 v = edges[i].v;
  48.             max_self(dp[ind][v], dp[ind ^ 1][u] + a[v]);
  49.         }
  50.         max_self(ans, dp[ind][1] - C * t * t);
  51.     }
  52.     fout << ans << '\n';
  53. }
  54.  
Add Comment
Please, Sign In to add comment