답안 #114212

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
114212 2019-05-31T10:10:49 Z popovicirobert Soccer (JOI17_soccer) C++14
100 / 100
523 ms 21244 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, t;
    ll cst;

    bool operator <(const Data &other) const {
        return cst > other.cst;
    }
};

const int MAXH = 500;

ll dst[MAXH + 1][MAXH + 1][5];
bool vis[MAXH + 1][MAXH + 1][5];

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, j;
    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();

    priority_queue <Data> pq;

    for(i = 0; i <= h; i++) {
        for(j = 0; j <= w; j++) {
            for(int d = 0; d < 5; d++) {
                dst[i][j][d] = INF;
            }
        }
    }

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

    while(pq.size()) {
        auto cur = pq.top();
        pq.pop();

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

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

        if(cur.t != 4) {
            ll cst = cur.cst + B;

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

            if(dst[cur.l][cur.c][4] > cst) {
                dst[cur.l][cur.c][4] = cst;
                pq.push({cur.l, cur.c, 4, cst});
            }
        }

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

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

                if(in(l, c) && dst[l][c][i] > cur.cst + A) {
                    dst[l][c][i] = cur.cst + A;
                    pq.push({l, c, i, dst[l][c][i]});
                }
            }
        }
        else {
            int l = cur.l + dl[cur.t], c = cur.c + dc[cur.t];

            if(in(l, c) && dst[l][c][cur.t] > cur.cst + A) {
                dst[l][c][cur.t] = cur.cst + A;
                pq.push({l, c, cur.t, dst[l][c][cur.t]});
            }
        }
    }

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

    //cin.close();
    //cout.close();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 6776 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 302 ms 19680 KB Output is correct
4 Correct 309 ms 19864 KB Output is correct
5 Correct 56 ms 7032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 358 ms 19680 KB Output is correct
2 Correct 396 ms 19680 KB Output is correct
3 Correct 260 ms 17128 KB Output is correct
4 Correct 254 ms 19688 KB Output is correct
5 Correct 266 ms 19308 KB Output is correct
6 Correct 306 ms 19684 KB Output is correct
7 Correct 362 ms 19712 KB Output is correct
8 Correct 310 ms 19684 KB Output is correct
9 Correct 347 ms 19652 KB Output is correct
10 Correct 42 ms 7160 KB Output is correct
11 Correct 350 ms 19664 KB Output is correct
12 Correct 341 ms 19688 KB Output is correct
13 Correct 234 ms 17128 KB Output is correct
14 Correct 389 ms 19824 KB Output is correct
15 Correct 289 ms 19300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 6776 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 302 ms 19680 KB Output is correct
4 Correct 309 ms 19864 KB Output is correct
5 Correct 56 ms 7032 KB Output is correct
6 Correct 358 ms 19680 KB Output is correct
7 Correct 396 ms 19680 KB Output is correct
8 Correct 260 ms 17128 KB Output is correct
9 Correct 254 ms 19688 KB Output is correct
10 Correct 266 ms 19308 KB Output is correct
11 Correct 306 ms 19684 KB Output is correct
12 Correct 362 ms 19712 KB Output is correct
13 Correct 310 ms 19684 KB Output is correct
14 Correct 347 ms 19652 KB Output is correct
15 Correct 42 ms 7160 KB Output is correct
16 Correct 350 ms 19664 KB Output is correct
17 Correct 341 ms 19688 KB Output is correct
18 Correct 234 ms 17128 KB Output is correct
19 Correct 389 ms 19824 KB Output is correct
20 Correct 289 ms 19300 KB Output is correct
21 Correct 77 ms 7356 KB Output is correct
22 Correct 523 ms 19844 KB Output is correct
23 Correct 461 ms 15364 KB Output is correct
24 Correct 468 ms 16628 KB Output is correct
25 Correct 392 ms 19684 KB Output is correct
26 Correct 369 ms 19872 KB Output is correct
27 Correct 176 ms 14808 KB Output is correct
28 Correct 216 ms 15256 KB Output is correct
29 Correct 355 ms 18076 KB Output is correct
30 Correct 197 ms 15100 KB Output is correct
31 Correct 393 ms 19728 KB Output is correct
32 Correct 484 ms 21244 KB Output is correct
33 Correct 309 ms 19688 KB Output is correct
34 Correct 486 ms 19904 KB Output is correct
35 Correct 181 ms 15116 KB Output is correct