結果

問題 No.1283 Extra Fee
ユーザー ForestedForested
提出日時 2020-11-06 21:53:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 2,889 bytes
コンパイル時間 2,480 ms
コンパイル使用メモリ 214,508 KB
実行使用メモリ 77,672 KB
最終ジャッジ日時 2023-08-10 05:57:46
合計ジャッジ時間 7,806 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 16 ms
7,040 KB
testcase_12 AC 18 ms
7,588 KB
testcase_13 AC 12 ms
6,248 KB
testcase_14 AC 52 ms
16,300 KB
testcase_15 AC 83 ms
23,948 KB
testcase_16 AC 18 ms
7,528 KB
testcase_17 AC 269 ms
65,152 KB
testcase_18 AC 279 ms
71,860 KB
testcase_19 AC 291 ms
75,188 KB
testcase_20 AC 273 ms
70,716 KB
testcase_21 AC 278 ms
71,760 KB
testcase_22 AC 248 ms
64,188 KB
testcase_23 AC 269 ms
77,400 KB
testcase_24 AC 281 ms
77,264 KB
testcase_25 AC 300 ms
77,528 KB
testcase_26 AC 302 ms
77,564 KB
testcase_27 AC 301 ms
77,528 KB
testcase_28 AC 300 ms
77,672 KB
testcase_29 AC 286 ms
70,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Template
#include "bits/stdc++.h"
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T> 
inline bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
struct IOSET {
    IOSET() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
    }
} ioset;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &element : vec) is >> element;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec) {
    for (int i = 0, vec_len = (int)vec.size(); i < vec_len; ++i) {
        os << vec[i] << " \n"[i + 1 == vec_len];
    }
    return os;
}

// Main
struct Edge {
    int to;
    ll cost;
    Edge(int t, ll c) : to(t), cost(c) {}
};
constexpr int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> h(m), w(m), c(m);
    rep(i, m) {
        cin >> h[i] >> w[i] >> c[i];
        --h[i];
        --w[i];
    }
    
    vector<vector<int>> vec(n, vector<int>(n, 0));
    rep(i, m) vec[h[i]][w[i]] = c[i];
    
    vector<vector<Edge>> g(n * n * 2);
    rep(i, n) rep(j, n) rep(k, 4) {
        if (i + dx[k] < 0 || i + dx[k] >= n || j + dy[k] < 0 || j + dy[k] >= n) continue;
        if (!vec[i + dx[k]][j + dy[k]]) {
            g[(i * n + j) * 2 + 0].emplace_back(((i + dx[k]) * n + j + dy[k]) * 2 + 0, 1);
            g[(i * n + j) * 2 + 1].emplace_back(((i + dx[k]) * n + j + dy[k]) * 2 + 1, 1);
        } else {
            g[(i * n + j) * 2 + 0].emplace_back(((i + dx[k]) * n + j + dy[k]) * 2 + 1, 1);
            g[(i * n + j) * 2 + 0].emplace_back(((i + dx[k]) * n + j + dy[k]) * 2 + 0, vec[i + dx[k]][j + dy[k]] + 1);
            g[(i * n + j) * 2 + 1].emplace_back(((i + dx[k]) * n + j + dy[k]) * 2 + 1, vec[i + dx[k]][j + dy[k]] + 1);
        }
    }
    
    vector<ll> dist(n * n * 2, INF);
    priority_queue<pair<ll, int>> pq;
    dist[0] = 0;
    pq.emplace(dist[0], 0);
    while (!pq.empty()) {
        auto [d, v] = pq.top();
        pq.pop();
        d *= -1;
        if (dist[v] != d) continue;
        for (Edge &e : g[v]) {
            if (chmin(dist[e.to], d + e.cost)) pq.emplace(-dist[e.to], e.to);
        }
    }
    
    cout << min(dist[(n * n - 1) * 2], dist[(n * n - 1) * 2 + 1]) << "\n";
}
0