Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize ("O3")
- #pragma GCC target("sse4")
- #include <bits/stdc++.h>
- #define f first
- #define s second
- #define pb push_back
- #define mp make_pair
- #define ts to_string
- #define ub upper_bound
- #define lb lower_bound
- const char nl = '\n';
- using namespace std;
- using ll = long long;
- using vi = vector<int>;
- using vl = vector<ll>;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using str = string;
- //The following debugging code was taken from Benjamin Qi's template :)
- str ts(char c) { return str(1,c); }
- str ts(bool b) { return b ? "true" : "false"; }
- str ts(const char* s) { return (str)s; }
- str ts(str s) { return s; }
- template<class A> str ts(complex<A> c) {
- stringstream ss; ss << c; return ss.str(); }
- str ts(vector<bool> v) {
- str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
- res += "}"; return res; }
- template<size_t SZ> str ts(bitset<SZ> b) {
- str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
- return res; }
- template<class A, class B> str ts(pair<A,B> p);
- template<class T> str ts(T v) {
- bool fst = 1; str res = "{";
- for (const auto& x: v) {
- if (!fst) res += ", ";
- fst = 0; res += ts(x);
- }
- res += "}"; return res;
- }
- template<class A, class B> str ts(pair<A,B> p) {
- return "("+ts(p.f)+", "+ts(p.s)+")"; }
- void DBG() { cerr << "]" << endl; }
- template<class H, class... T> void DBG(H h, T... t) {
- cerr << ts(h); if (sizeof...(t)) cerr << ", ";
- DBG(t...); }
- #ifdef LOCAL
- #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
- #else
- #define dbg(...) 0
- //freopen(".in", "r", stdin);
- //freopen(".out", "w", stdout);
- #endif
- //CODE STARTS HERE-----------------------------------------------------------------------------------------//
- const int mxH = 1001;
- ll H, G, dp[mxH][mxH][2]; pll h[mxH], g[mxH];
- ll dist(ll x, ll y, ll xx, ll yy){
- return (xx-x)*(xx-x) + (yy-y)*(yy-y);
- }
- signed main(){
- cin.tie(nullptr);
- ios_base::sync_with_stdio(false);
- ifstream cin("checklist.in");
- ofstream cout("checklist.out");
- cin >> H >> G;
- for(int i = 0; i < H; i++){
- cin >> h[i].f >> h[i].s;
- }
- for(int i = 0; i < G; i++){
- cin >> g[i].f >> g[i].s;
- }
- for(int i =0; i < mxH; i++){
- for(int j =0; j < mxH; j++){
- for(int k = 0; k < 2; k++)
- dp[i][j][k] = 1e18;
- }
- }
- dp[1][0][0] = 0;
- for(int i=0 ; i <= H; i++){
- for(int j =0; j <= G; j++){
- for(int k = 0; k < 2; k++){
- if(k == 0 && i == 0) continue;
- if(k == 1 && j == 0) continue;
- if(k == 0){
- if(i < H)
- 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));
- if(j < G){
- 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));
- }
- }
- else{
- if(i < H)
- 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));
- if(j < G){
- 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));
- }
- }
- }
- }
- }
- for(int i =0 ; i <= H ; i++){
- for(int j =0 ; j <= G; j++){
- dbg(dp[i][j][0], dp[i][j][1], i, j);
- }
- }
- cout << dp[H][G][0] << nl;
- }
Advertisement
Add Comment
Please, Sign In to add comment