Advertisement
Fshl0

يصي

Apr 29th, 2022
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 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;
  22.    
  23.     room (int Y) {
  24.         y = Y;
  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 <pair<int, int>> next[2 * k + 9];
  36.     map <pair<int, int>, int> hashy;
  37.     vector <room> rooms[n + 1];
  38.     int counter = 0;
  39.        
  40.     while (k--) {
  41.         int x1, y1;
  42.         cin >> x1 >> y1;
  43.        
  44.         int x2, y2;
  45.         cin >> x2 >> y2;
  46.        
  47.         int h;
  48.         cin >> h;
  49.        
  50.         if (!hashy.count({x1, y1}))
  51.             hashy[{x1, y1}] = counter++;
  52.        
  53.         if (!hashy.count({x2, y2}))
  54.             hashy[{x2, y2}] = counter++;
  55.        
  56.         rooms[x1].pb (room(y1));
  57.         rooms[x2].pb (room(y2));
  58.         next[hashy[{x1, y1}]].pb ({hashy[{x2, y2}], h});
  59.     }
  60.    
  61.     for (int i = 0; i < 2 * k + 9; i++)
  62.         dp[i] = llINF;
  63.        
  64.     for (auto r: rooms[1]) {
  65.         int id = hashy[{1, r.y}];
  66.         dp[id] = (r.y - 1) * x[1];
  67.     }
  68.    
  69.     for (int i = 1; i <= n; i++) {
  70.         if (rooms[i].size() == 0)
  71.             continue;
  72.        
  73.         sort (rooms[i].begin(), rooms[i].end(), [&] (room r1, room r2) {
  74.             if (r1.y != r2.y)
  75.                 return r1.y < r2.y;
  76.            
  77.             return false;
  78.         });
  79.        
  80.         for (int j = 1; j < (int) rooms[i].size(); j++) {
  81.             int curY = rooms[i][j].y, curId = hashy[{i, curY}];
  82.             int preY = rooms[i][j - 1].y, preId = hashy[{i, preY}];
  83.            
  84.             dp[curId] = min (dp[curId], dp[preId] + (curY - preY) * x[i]);
  85.         }
  86.        
  87.         for (int j = (int) rooms[i].size() - 2; j >= 0; j--) {
  88.             int curY = rooms[i][j].y, curId = hashy[{i, curY}];
  89.             int preY = rooms[i][j + 1].y, preId = hashy[{i, preY}];
  90.            
  91.             dp[curId] = min (dp[curId], dp[preId] + (preY - curY) * x[i]);
  92.         }
  93.        
  94.         for (auto r: rooms[i]) {
  95.             int curId = hashy[{i, r.y}];
  96.             for (auto r2: next[curId])
  97.                 dp[r2.F] = min (dp[r2.F], dp[curId] - r2.S);
  98.         }
  99.     }
  100.    
  101.    
  102.     long long res = llINF;
  103.     for (auto u: rooms[n]) {
  104.         int id = hashy[{n, u.y}];
  105.         res = min (res, dp[id] + (m - u.y) * x[n]);
  106.     }
  107.    
  108.     if (res < llINF) {
  109.         cout << res << "\n";
  110.     } else puts ("NO ESCAPE");
  111.    
  112.     return;
  113. }
  114.  
  115. int main () {
  116.     int t = 1;
  117.     cin >> t;
  118.    
  119.     //ios_base::sync_with_stdio (0);
  120.     //cin.tie (0);
  121.    
  122.     //setIO ("billboard");
  123.     //preCalc ();
  124.     while (t--) {
  125.         //initialize common variables
  126.        
  127.         //go solve
  128.         solve ();
  129.     }
  130.    
  131.     return 0;
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement