DuongNhi99

A - Adventures in Moving Part IV (1-12)

Nov 30th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int oo = 1e9 + 7;;
  5.  
  6. int t, Distance, n;
  7. int dist[105], cost[105];
  8. char s[105];
  9. int dp[105][205];
  10.  
  11. void Read() {
  12.     scanf("%d", &Distance);
  13.     while(getchar() != '\n');
  14.  
  15.     n = 1;
  16.     while(gets(s) && s[0]) {
  17.         sscanf(s, "%d %d", &dist[n], &cost[n]);
  18.         n++;
  19.     }
  20. }
  21.  
  22. void solve() {
  23.     dist[n] = Distance, cost[n] = oo;
  24.     for(int i = 0; i <= n; i++)
  25.         for(int j = 0; j <= 200; j++)
  26.             dp[i][j] = oo;
  27.     dp[0][100] = 0;
  28.  
  29.     for(int i = 1; i <= n; i++) {
  30.         int dis = dist[i] - dist[i-1];
  31.  
  32.         for(int j = dis; j <= 200; j++)
  33.             dp[i][j-dis] = min(dp[i][j-dis], dp[i-1][j]);
  34.  
  35.         for(int j = 1; j <= 200; j++)
  36.             dp[i][j] = min(dp[i][j], dp[i][j-1] + cost[i]);
  37.     }
  38.  
  39.     int ans = oo;
  40.     for(int i = 100; i <= 200; i++)
  41.         ans = min(ans, dp[n][i]);
  42.  
  43.     if(ans == oo) cout << "Impossible" << '\n' << '\n';
  44.     else cout << ans << '\n' << '\n';
  45. }
  46.  
  47. int main() {
  48.     //freopen("A-Adventures in Moving (Part IV).inp", "r", stdin);
  49.     //freopen("A-Adventures in Moving (Part IV).out", "w", stdout);
  50.     ios_base::sync_with_stdio(false);
  51.     cin.tie(NULL); cout.tie(NULL);
  52.  
  53.     scanf("%d", &t);
  54.     while(getchar() != '\n');
  55.     gets(s);
  56.     while(t--) {
  57.         Read();
  58.         solve();
  59.     }
  60.  
  61.     return 0;
  62. }
  63. /*
  64.     init = 100;
  65.  
  66.     dp[0][init - dist[0]] = dp[0][0];
  67.  
  68.     garToNext = dist[start + 1] - dist[start];
  69.     gasInTank = max(gasLeft, gasToNext);
  70.  
  71.     dp[start][gasLeft] = min(gasInTank, f[start + 1][gasInTank - gasToNext] + (gasInTank - gasLeft) * cost[start];
  72. */
  73.  
Advertisement
Add Comment
Please, Sign In to add comment