結果

問題 No.1283 Extra Fee
ユーザー KudeKude
提出日時 2020-11-06 22:41:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 2,162 bytes
コンパイル時間 2,253 ms
コンパイル使用メモリ 214,828 KB
実行使用メモリ 76,556 KB
最終ジャッジ日時 2023-08-10 06:03:03
合計ジャッジ時間 7,599 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,388 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 13 ms
7,344 KB
testcase_12 AC 15 ms
7,748 KB
testcase_13 AC 11 ms
6,036 KB
testcase_14 AC 48 ms
15,656 KB
testcase_15 AC 76 ms
23,264 KB
testcase_16 AC 16 ms
7,292 KB
testcase_17 AC 240 ms
69,764 KB
testcase_18 AC 259 ms
69,304 KB
testcase_19 AC 270 ms
72,364 KB
testcase_20 AC 259 ms
67,872 KB
testcase_21 AC 257 ms
68,956 KB
testcase_22 AC 227 ms
61,816 KB
testcase_23 AC 237 ms
74,380 KB
testcase_24 AC 257 ms
74,472 KB
testcase_25 AC 277 ms
74,512 KB
testcase_26 AC 283 ms
74,508 KB
testcase_27 AC 278 ms
74,572 KB
testcase_28 AC 274 ms
74,544 KB
testcase_29 AC 260 ms
76,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

template<class T>
std::vector<unsigned long long> dijkstra(std::vector<std::vector<std::pair<int,T>>>& to, int s=0) {
    const unsigned long long INF = 1002003004005006007;
    struct QueElem {
        int v;
        unsigned long long c;
        bool operator<(const QueElem a) const {return c > a.c;}
        QueElem(int v, unsigned long long c): v(v), c(c) {}
    };
    std::priority_queue<QueElem> q;
    std::vector<unsigned long long> dist(to.size(), INF);
    dist[s] = 0;
    q.emplace(s, 0);
    while(!q.empty()) {
        QueElem qe = q.top(); q.pop();
        int u = qe.v;
        unsigned long long c = qe.c;
        if (c > dist[u]) continue;
        for(auto vc: to[u]) {
            int v = vc.first;
            unsigned long long nc = c + vc.second;
            if (nc < dist[v]) {
                dist[v] = nc;
                q.emplace(v, nc);
            }
        }
    }
    return dist;
}

const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    VVI gcost(n, VI(n));
    rep(_, m) {
        int h, w, c;
        cin >> h >> w >> c;
        --h, --w;
        gcost[h][w] = c;
    }
    int n2 = n * n;
    vector<vector<pair<int,ll>>> to(n2 * 2);
    rep(s, 2) rep(i, n) rep(j, n) {
        int orig = i * n + j + s * n2;
        rep(k, 4) {
            int ni = i + dx[k], nj = j + dy[k];
            if (!(0 <= ni && ni < n && 0 <= nj && nj < n)) continue;
            int dest = ni * n + nj + s * n2;
            to[orig].emplace_back(dest, 1 + gcost[ni][nj]);
            if (s == 0) to[orig].emplace_back(dest + n2, 1);
        }
    }
    auto res = dijkstra(to);
    cout << res.back() << endl;
}
0