jainbot27

Untitled

Jul 26th, 2020
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target("sse4")
  3. #include <bits/stdc++.h>
  4. #define f first
  5. #define s second
  6. #define pb push_back
  7. #define mp make_pair
  8. #define ts to_string
  9. #define ub upper_bound
  10. #define lb lower_bound
  11. const char nl = '\n';
  12. using namespace std;
  13. using ll = long long;
  14. using vi = vector<int>;
  15. using vl = vector<ll>;
  16. using pii = pair<int, int>;
  17. using pll = pair<ll, ll>;
  18. using str = string;
  19. //The following debugging code was taken from Benjamin Qi's template :)
  20. str ts(char c) { return str(1,c); }
  21. str ts(bool b) { return b ? "true" : "false"; }
  22. str ts(const char* s) { return (str)s; }
  23. str ts(str s) { return s; }
  24. template<class A> str ts(complex<A> c) {
  25.     stringstream ss; ss << c; return ss.str(); }
  26. str ts(vector<bool> v) {
  27.     str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
  28.     res += "}"; return res; }
  29. template<size_t SZ> str ts(bitset<SZ> b) {
  30.     str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
  31.     return res; }
  32. template<class A, class B> str ts(pair<A,B> p);
  33. template<class T> str ts(T v) {
  34.     bool fst = 1; str res = "{";
  35.     for (const auto& x: v) {
  36.         if (!fst) res += ", ";
  37.         fst = 0; res += ts(x);
  38.     }
  39.     res += "}"; return res;
  40. }
  41. template<class A, class B> str ts(pair<A,B> p) {
  42.     return "("+ts(p.f)+", "+ts(p.s)+")"; }
  43. void DBG() { cerr << "]" << endl; }
  44. template<class H, class... T> void DBG(H h, T... t) {
  45.     cerr << ts(h); if (sizeof...(t)) cerr << ", ";
  46.     DBG(t...); }
  47.  
  48. #ifdef LOCAL
  49. #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  50. #else
  51. #define dbg(...) 0
  52. //freopen(".in", "r", stdin);
  53. //freopen(".out", "w", stdout);
  54. #endif
  55. //CODE STARTS HERE-----------------------------------------------------------------------------------------//
  56. const int mxH = 1001;
  57. ll H, G, dp[mxH][mxH][2]; pll h[mxH], g[mxH];
  58. ll dist(ll x, ll y, ll xx, ll yy){
  59.     return (xx-x)*(xx-x) + (yy-y)*(yy-y);
  60. }
  61. signed main(){
  62.     cin.tie(nullptr);
  63.     ios_base::sync_with_stdio(false);
  64.     ifstream cin("checklist.in");
  65.     ofstream cout("checklist.out");
  66.     cin >> H >> G;
  67.     for(int i = 0; i < H; i++){
  68.         cin >> h[i].f >> h[i].s;
  69.     }
  70.     for(int i = 0; i < G; i++){
  71.         cin >> g[i].f >> g[i].s;
  72.     }
  73.     for(int i =0; i <  mxH; i++){
  74.         for(int j =0; j < mxH; j++){
  75.             for(int k = 0; k < 2; k++)
  76.                 dp[i][j][k] = 1e18;
  77.         }
  78.     }
  79.     dp[1][0][0] = 0;
  80.     for(int i=0 ; i <= H; i++){
  81.         for(int j =0; j <= G; j++){
  82.             for(int k = 0; k < 2; k++){
  83.                 if(k == 0 && i == 0) continue;
  84.                 if(k == 1 && j == 0) continue;
  85.                 if(k == 0){
  86.                     if(i < H)
  87.                         dp[i+1][j][0] = min(dp[i+1][j][0], dp[i][j][k] + dist(h[i-1].f, h[i-1].s, h[i].f, h[i].s));
  88.                     if(j < G){
  89.                         dp[i][j+1][1] = min(dp[i][j+1][1], dp[i][j][k] + dist(h[i-1].f, h[i-1].s, g[j].f, g[j].s));
  90.                     }
  91.                 }
  92.                 else{
  93.                     if(i < H)
  94.                         dp[i+1][j][0] = min(dp[i+1][j][0], dp[i][j][k] + dist(g[j-1].f, g[j-1].s, h[i].f, h[i].s));
  95.                     if(j < G){
  96.                         dp[i][j+1][1] = min(dp[i][j+1][1], dp[i][j][k] + dist(g[j-1].f, g[j-1].s, g[j].f, g[j].s));
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     for(int i =0 ; i <= H ; i++){
  103.         for(int j =0 ; j <= G; j++){
  104.             dbg(dp[i][j][0], dp[i][j][1], i, j);
  105.         }
  106.     }
  107.     cout << dp[H][G][0] << nl;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment