Advertisement
samuel21119

UVa 10603

Mar 5th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int A, B, C, t, sum, mx = 2139062143;
  5. int a, b, c, pour;
  6. int target;
  7. int dp[201][201], ans[201];
  8. struct abc {
  9.     int a, b;
  10.     int p;
  11.     abc() {}
  12.     abc(int a, int b, int p) : a(a), b(b), p(p) {}
  13. }tmp;
  14. int main() {
  15.     ios::sync_with_stdio(0);
  16.     cin.tie(0);
  17.     cin >> t;
  18.     while (t--) {
  19.         cin >> A >> B >> C >> target;
  20.         sum = C;
  21.         memset(dp, 127, sizeof(dp));
  22.         memset(ans, 127, sizeof(ans));
  23.         queue<abc> q;
  24.         q.push({0, 0, 0});
  25.         while (!q.empty()) {
  26.             tmp = q.front();
  27.             q.pop();
  28.             a = tmp.a;
  29.             b = tmp.b;
  30.             c = sum - a - b;
  31.             pour = tmp.p;
  32.             if (ans[target] <= pour)
  33.                 continue;
  34.             if (dp[a][b] <= pour)
  35.                 continue;
  36.             dp[a][b] = pour;
  37.             ans[a] = min(ans[a], pour);
  38.             ans[b] = min(ans[b], pour);
  39.             ans[c] = min(ans[c], pour);
  40.             if (a > 0 && b < B) {
  41.                 if (a < B - b)
  42.                     q.push({0, a + b, pour + a});
  43.                 else
  44.                     q.push({a - (B - b), B, pour + (B - b)});
  45.             }
  46.             if (a > 0 && c < C) {
  47.                 if (a < C - c)
  48.                     q.push({0, b, pour + a});
  49.                 else
  50.                     q.push({a - (C - c), b, pour + (C - c)});
  51.             }
  52.             if (b > 0 && a < A) {
  53.                 if (b < A - a)
  54.                     q.push({a + b, 0, pour + b});
  55.                 else
  56.                     q.push({A, b - (A - a), pour + (A - a)});
  57.             }
  58.             if (b > 0 && c < C) {
  59.                 if (b < C - c)
  60.                     q.push({a, 0, pour + b});
  61.                 else
  62.                     q.push({a, b - (C - c), pour + (C - c)});
  63.             }
  64.             if (c > 0 && a < A) {
  65.                 if (c < A - a)
  66.                     q.push({a + c, b, pour + c});
  67.                 else
  68.                     q.push({A, b, pour + (A - a)});
  69.             }
  70.             if (c > 0 && b < B) {
  71.                 if (c < B - b)
  72.                     q.push({a, b + c, pour + c});
  73.                 else
  74.                     q.push({a, B, pour + (B - b)});
  75.             }
  76.         }
  77.         if (ans[target] == mx)
  78.             while (ans[target] == mx) target--;
  79.         cout << ans[target] << ' ' << target << '\n';
  80.     }
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement