Submission #114206

# Submission time Handle Problem Language Result Execution time Memory
114206 2019-05-31T09:38:58 Z popovicirobert Soccer (JOI17_soccer) C++14
35 / 100
3000 ms 19932 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

const ll INF = 1e18;
vector < vector <ll> > dst_c;

int dl[] = {-1, 0, 1, 0}, dc[] = {0, -1, 0, 1};

vector <int> x, y;
int h, w, A, B, C, n;

inline bool in(int l, int c) {
    return l >= 0 && c >= 0 && l <= h && c <= w;
}

inline void bfs() {
    queue < pair <int, int> > Q;

    dst_c.resize(h + 1, vector <ll>(w + 1, INF));

    for(int i = 1; i < n; i++) {
        dst_c[x[i]][y[i]] = 0;
        Q.push({x[i], y[i]});
    }

    while(Q.size()) {
        auto cur = Q.front();
        Q.pop();

        for(int i = 0; i < 4; i++) {
            int l = cur.first + dl[i];
            int c = cur.second + dc[i];

            if(in(l, c) && dst_c[l][c] == INF) {
                dst_c[l][c] = dst_c[cur.first][cur.second] + C;
                Q.push({l, c});
            }
        }

    }
}

struct Data {
    int l, c;
    ll cst;

    bool operator <(const Data &other) const {
        if(cst == other.cst) {
            if(l == other.l) return c < other.c;
            return l < other.l;
        }
        return cst < other.cst;
    }
};

inline void del(set <Data> &pq, int l, int c, ll cst) {
    auto it = pq.lower_bound({l, c, cst});
    if(it != pq.end() && it -> l == l && it -> c == c) {
        pq.erase(it);
    }
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> h >> w >> A >> B >> C >> n;

    x.resize(n + 1), y.resize(n + 1);
    for(i = 1; i <= n; i++) {
        cin >> x[i] >> y[i];
    }

    bfs();

    vector < vector <ll> > dst(h + 1, vector <ll>(w + 1, INF));
    vector < vector <bool> > vis(h + 1, vector <bool>(w + 1, 0));

    set <Data> pq;

    pq.insert({x[1], y[1], 0});
    dst[x[1]][y[1]] = 0;

    while(pq.size()) {
        auto it = pq.begin();
        Data cur = {it -> l, it -> c, it -> cst};
        pq.erase(pq.begin());

        if(vis[cur.l][cur.c]) {
            continue;
        }

        vis[cur.l][cur.c] = 1;

        for(i = 0; i < 4; i++) {
            int l = cur.l + dl[i];
            int c = cur.c + dc[i];

            if(in(l, c) && dst[l][c] > cur.cst + C) {
                del(pq, l, c, dst[l][c]);
                dst[l][c] = cur.cst + C;
                pq.insert({l, c, dst[l][c]});
            }
        }

        for(i = 0; i <= h; i++) {
            ll cst = cur.cst + 1LL * A * abs(i - cur.l) + B;

            if(i != x[n] || cur.c != y[n]) {
                cst += dst_c[i][cur.c];
            }

            if(dst[i][cur.c] > cst) {
                del(pq, i, cur.c, dst[i][cur.c]);
                dst[i][cur.c] = cst;
                pq.insert({i, cur.c, cst});
            }
        }
        for(i = 0; i <= w; i++) {
            ll cst = cur.cst + 1LL * A * abs(i - cur.c) + B;

            if(i != y[n] || cur.l != x[n]) {
                cst += dst_c[cur.l][i];
            }

            if(dst[cur.l][i] > cst) {
                del(pq, cur.l, i, dst[cur.l][i]);
                dst[cur.l][i] = cst;
                pq.insert({cur.l, i, cst});
            }
        }
    }

    cout << dst[x[n]][y[n]];

    //cin.close();
    //cout.close();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 203 ms 3932 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 1547 ms 14264 KB Output is correct
4 Correct 1817 ms 15660 KB Output is correct
5 Correct 306 ms 5752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1746 ms 17880 KB Output is correct
2 Correct 1792 ms 18556 KB Output is correct
3 Correct 1069 ms 13560 KB Output is correct
4 Correct 1439 ms 12664 KB Output is correct
5 Correct 1133 ms 13688 KB Output is correct
6 Correct 1343 ms 16760 KB Output is correct
7 Correct 1692 ms 19932 KB Output is correct
8 Correct 1607 ms 19792 KB Output is correct
9 Correct 1723 ms 19676 KB Output is correct
10 Correct 167 ms 3548 KB Output is correct
11 Correct 1933 ms 19916 KB Output is correct
12 Correct 1667 ms 19176 KB Output is correct
13 Correct 1104 ms 14416 KB Output is correct
14 Correct 1830 ms 19848 KB Output is correct
15 Correct 1188 ms 15964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 3932 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 1547 ms 14264 KB Output is correct
4 Correct 1817 ms 15660 KB Output is correct
5 Correct 306 ms 5752 KB Output is correct
6 Correct 1746 ms 17880 KB Output is correct
7 Correct 1792 ms 18556 KB Output is correct
8 Correct 1069 ms 13560 KB Output is correct
9 Correct 1439 ms 12664 KB Output is correct
10 Correct 1133 ms 13688 KB Output is correct
11 Correct 1343 ms 16760 KB Output is correct
12 Correct 1692 ms 19932 KB Output is correct
13 Correct 1607 ms 19792 KB Output is correct
14 Correct 1723 ms 19676 KB Output is correct
15 Correct 167 ms 3548 KB Output is correct
16 Correct 1933 ms 19916 KB Output is correct
17 Correct 1667 ms 19176 KB Output is correct
18 Correct 1104 ms 14416 KB Output is correct
19 Correct 1830 ms 19848 KB Output is correct
20 Correct 1188 ms 15964 KB Output is correct
21 Execution timed out 3098 ms 5364 KB Time limit exceeded
22 Halted 0 ms 0 KB -