結果

問題 No.1283 Extra Fee
ユーザー finefine
提出日時 2020-11-07 03:16:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 2,382 bytes
コンパイル時間 1,805 ms
コンパイル使用メモリ 185,464 KB
実行使用メモリ 39,816 KB
最終ジャッジ日時 2024-04-27 23:32:50
合計ジャッジ時間 5,490 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 9 ms
6,944 KB
testcase_12 AC 11 ms
6,944 KB
testcase_13 AC 7 ms
6,940 KB
testcase_14 AC 30 ms
9,088 KB
testcase_15 AC 48 ms
12,416 KB
testcase_16 AC 11 ms
6,940 KB
testcase_17 AC 165 ms
36,896 KB
testcase_18 AC 170 ms
33,972 KB
testcase_19 AC 182 ms
35,388 KB
testcase_20 AC 164 ms
33,380 KB
testcase_21 AC 172 ms
33,704 KB
testcase_22 AC 145 ms
30,588 KB
testcase_23 AC 151 ms
36,444 KB
testcase_24 AC 173 ms
36,368 KB
testcase_25 AC 188 ms
36,520 KB
testcase_26 AC 183 ms
36,468 KB
testcase_27 AC 186 ms
36,404 KB
testcase_28 AC 186 ms
36,416 KB
testcase_29 AC 183 ms
39,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';
constexpr int dh[] = {1, -1, 0, 0};
constexpr int dw[] = {0, 0, 1, -1};

struct State {
    int at;
    ll cost;
    bool used;
    //int prev;
    State(int at, ll cost, bool used) : at(at), cost(cost), used(used) {}
    bool operator>(const State& s) const {
        return cost > s.cost;
    }
};

struct Edge {
    int to;
    ll cost;
    Edge(int to, ll cost) : to(to), cost(cost) {}
};

using Graph = vector< vector<Edge> >; //隣接リスト

const ll INF = 1e15;
//const int NONE = -1;

//sは始点、mincは最短経路のコスト、prevsは最短経路をたどる際の前の頂点
void dijkstra(int s, const Graph& graph, vector< vector<ll> >& minc){
    minc.assign(2, vector<ll>(graph.size(), INF));

    priority_queue<State, vector<State>, greater<State> > pq;
    pq.emplace(s, 0, false);
    minc[false][s] = 0;

    while(!pq.empty()) {
        State cur = pq.top();
        pq.pop();
        if (minc[cur.used][cur.at] < cur.cost) continue;

        for(const Edge& e : graph[cur.at]) {
            if (!cur.used) {
                ll cost = cur.cost + 1;
                if (minc[true][e.to] > cost) {
                    minc[true][e.to] = cost;
                    pq.emplace(e.to, cost, true);
                }
            }

            ll cost = cur.cost + e.cost;
            if (minc[cur.used][e.to] <= cost) continue;
            minc[cur.used][e.to] = cost;
            pq.emplace(e.to, cost, cur.used);
        }
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    vector< vector<ll> > memo(n, vector<ll>(n, 0));
    for (int i = 0; i < m; i++) {
        int h, w;
        ll c;
        cin >> h >> w >> c;
        --h; --w;
        memo[h][w] = c;
    }

    Graph g(n * n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < 4; k++) {
                int nh = i + dh[k], nw = j + dw[k];
                if (nh < 0 || nh >= n || nw < 0 || nw >= n) continue;
                g[i * n + j].emplace_back(nh * n + nw, memo[nh][nw] + 1);
            }
        }
    }

    vector< vector<ll> > minc;
    dijkstra(0, g, minc);
    int goal = (n - 1) * n + (n - 1);
    cout << min(minc[0][goal], minc[1][goal]) << newl; 

    return 0;
}
0