Advertisement
Fshl0

ereeee

Apr 29th, 2022
1,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define F first
  3. #define S second
  4. #define pb push_back
  5. #define mp make_pair
  6.  
  7. using namespace std;
  8. typedef long long ll;
  9. typedef pair <ll, ll> pii;
  10.  
  11. const int INF = 1e7;
  12. const ll llINF = 1e14;
  13. const int mxN = 1e6 + 9;
  14. const int mxM = 1e5 + 9;
  15. const int MOD = 1e9 + 7;
  16. const double pi = acos (-1);
  17.  
  18. long long x[mxN], dp[2 * mxN], tmpDP[2 * mxN];
  19.  
  20. struct room {
  21.     int y, h, type;
  22.    
  23.     room (int Y, int H, int t) {
  24.         y = Y, h = H, type = t;
  25.     }
  26. };
  27.  
  28. void solve () {
  29.     int n, m, k;
  30.     cin >> n >> m >> k;
  31.    
  32.     for (int i = 1; i <= n; i++)
  33.         cin >> x[i];
  34.    
  35.     vector <int> next[2 * k + 9];
  36.     map <pair<int, int>, int> hashy;
  37.     vector <room> rooms[n + 1];
  38.     int counter = 0;
  39.    
  40.     vector <pair <int, pair<int, int>>> tmp;
  41.    
  42.     while (k--) {
  43.         int x1, y1;
  44.         cin >> x1 >> y1;
  45.        
  46.         int x2, y2;
  47.         cin >> x2 >> y2;
  48.        
  49.         int h;
  50.         cin >> h;
  51.        
  52.         if (!hashy.count({x1, y1}))
  53.             hashy[{x1, y1}] = counter++;
  54.        
  55.         if (!hashy.count({x2, y2}))
  56.             hashy[{x2, y2}] = counter++;
  57.        
  58.         rooms[x1].pb (room(y1, h, 0));
  59.         rooms[x2].pb (room(y2, h, 1));
  60.         next[hashy[{x1, y1}]].pb (hashy[{x2, y2}]);
  61.         //tmp.pb ({hashy[{x1, y1}], {x1, y1}});
  62.         //tmp.pb ({hashy[{x2, y2}], {x2, y2}});
  63.     }
  64.    
  65.     for (int i = 0; i < 2 * k + 9; i++)
  66.         dp[i] = llINF;
  67.        
  68.     for (auto r: rooms[1]) {
  69.         int id = hashy[{1, r.y}];
  70.         dp[id] = (r.y - 1) * x[1];
  71.     }
  72.    
  73.     for (int i = 1; i <= n; i++) {
  74.         if (rooms[i].size() == 0)
  75.             continue;
  76.        
  77.         sort (rooms[i].begin(), rooms[i].end(), [&] (room r1, room r2) {
  78.             if (r1.y != r2.y)
  79.                 return r1.y < r2.y;
  80.            
  81.             return r1.type > r2.type;
  82.         });
  83.        
  84.         for (int j = 1; j < (int) rooms[i].size(); j++) {
  85.             int curY = rooms[i][j].y, curId = hashy[{i, curY}];
  86.             int preY = rooms[i][j - 1].y, preId = hashy[{i, preY}];
  87.            
  88.             dp[curId] = min (dp[curId], dp[preId] + (curY - preY) * x[i]);
  89.         }
  90.        
  91.         for (int j = (int) rooms[i].size() - 2; j >= 0; j--) {
  92.             int curY = rooms[i][j].y, curId = hashy[{i, curY}];
  93.             int preY = rooms[i][j + 1].y, preId = hashy[{i, preY}];
  94.            
  95.             dp[curId] = min (dp[curId], dp[preId] + (preY - curY) * x[i]);
  96.         }
  97.        
  98.         for (auto r: rooms[i]) {
  99.             if (r.type == 1)
  100.                 continue;
  101.            
  102.             int curId = hashy[{i, r.y}];
  103.             for (auto r2: next[curId])
  104.                 dp[r2] = min (dp[r2], dp[curId] - r.h);
  105.         }
  106.     }
  107.    
  108.     //for (auto u: tmp)
  109.         //cout << dp[u.F] << "    " << u.S.F << ' ' << u.S.S << "\n";
  110.    
  111.     long long res = llINF;
  112.     for (auto u: rooms[n]) {
  113.         int id = hashy[{n, u.y}];
  114.         res = min (res, dp[id] + (m - u.y) * x[n]);
  115.     }
  116.    
  117.     cout << res << "\n";
  118.     return;
  119. }
  120.  
  121. int main () {
  122.     int t = 1;
  123.     cin >> t;
  124.    
  125.     //ios_base::sync_with_stdio (0);
  126.     //cin.tie (0);
  127.    
  128.     //setIO ("billboard");
  129.     //preCalc ();
  130.     while (t--) {
  131.         //initialize common variables
  132.        
  133.         //go solve
  134.         solve ();
  135.     }
  136.    
  137.     return 0;
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement