1627E - Not Escaping - CodeForces Solution


data structures dp implementation shortest paths two pointers *2200

Please click on ads to support us..

C++ Code:

# include <bits/stdc++.h>
using namespace std;

# define int long long
# define f(i,a,b) for(int i = a; i <= b; i ++)
# define g(i,b,a) for(int i = b; i >= a; i --)
# define CI const int

CI maxn = 2e5 + 7;

/*
Why???Why???Why???Why???Why???Why???Why???Why???Why???Why???
Why???Why???Why???Why???Why???Why???Why???Why???Why???Why???
Why???Why???Why???Why???Why???Why???Why???Why???Why???Why???
Why???Why???Why???Why???Why???Why???Why???Why???Why???Why???
Why???Why???Why???Why???Why???Why???Why???Why???Why???Why???
Why???Why???Why???Why???Why???Why???Why???Why???Why???Why??? 
*/

int n, m, k;
int x[maxn];
int a[maxn], b[maxn], c[maxn], d[maxn], h[maxn];
vector <int> row[maxn];
int dp[maxn];

struct Node {
    int x, y, id;
    vector <pair <int, int> > ladder;
    bool operator < (const Node &p) const {
        return x != p.x ? x < p.x : y < p.y;
    }
}t[maxn * 2];

int tot;
int mp[maxn * 2];

void solve() {
    cin >> n >> m >> k;
    f (i, 1, n)
        scanf("%lld", &x[i]);
    f (i, 1, k)
        scanf("%lld %lld %lld %lld %lld", &a[i], &b[i], &c[i], &d[i], &h[i]);
    f (i, 1, 2 * k + 2)
        t[i].ladder.clear();
    tot = 0;
    f (i, 1, k) {
        ++ tot; t[tot] = {a[i], b[i], tot};
        ++ tot; t[tot] = {c[i], d[i], tot};
        t[tot - 1].ladder.push_back({tot, h[i]});
    }
    ++ tot; t[tot] = {1, 1, tot};
    ++ tot; t[tot] = {n, m, tot};
    sort(t + 1, t + tot + 1);
    f (i, 1, tot)
        mp[t[i].id] = i;
    f (i, 1, tot)
        for (auto &x : t[i].ladder)
            x.first = mp[x.first];
    f (i, 1, n)
        row[i].clear();
    f (i, 1, tot)
        row[t[i].x].push_back(i);
    f (i, 1, tot)
        dp[i] = 2e18;
    dp[1] = 0;
    f (i, 1, n) {
        f (j, 1, (int) row[i].size() - 1) {
            int p = row[i][j - 1];
            int q = row[i][j];
            dp[q] = min(dp[q], dp[p] + abs(t[p].y - t[q].y) * x[i]);
        }
        g (j, (int) row[i].size() - 1, 1) {
            int p = row[i][j - 1];
            int q = row[i][j];
            dp[p] = min(dp[p], dp[q] + abs(t[p].y - t[q].y) * x[i]);
        }
        for (int p : row[i])
            for (auto q : t[p].ladder)
                dp[q.first] = min(dp[q.first], dp[p] - q.second);
    }
    if (dp[tot] >= 1e18) printf("NO ESCAPE\n");
    else printf("%lld\n", dp[tot]);
}

signed main() {
    int t; cin >> t;
    while (t --) solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words
1681A - Game with Cards
151C - Win or Freeze
1585A - Life of a Flower
1662A - Organizing SWERC
466C - Number of Ways
1146A - Love "A"
1618D - Array and Operations
1255A - Changing Volume