結果

問題 No.1283 Extra Fee
ユーザー ForestedForested
提出日時 2020-11-06 21:53:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 296 ms / 2,000 ms
コード長 2,889 bytes
コンパイル時間 2,364 ms
コンパイル使用メモリ 217,656 KB
実行使用メモリ 77,584 KB
最終ジャッジ日時 2024-04-27 23:16:09
合計ジャッジ時間 6,819 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 14 ms
7,168 KB
testcase_12 AC 15 ms
7,808 KB
testcase_13 AC 9 ms
6,944 KB
testcase_14 AC 45 ms
16,384 KB
testcase_15 AC 73 ms
24,136 KB
testcase_16 AC 15 ms
7,680 KB
testcase_17 AC 258 ms
65,552 KB
testcase_18 AC 255 ms
71,820 KB
testcase_19 AC 263 ms
75,276 KB
testcase_20 AC 253 ms
70,604 KB
testcase_21 AC 260 ms
71,820 KB
testcase_22 AC 231 ms
64,328 KB
testcase_23 AC 240 ms
77,456 KB
testcase_24 AC 263 ms
77,484 KB
testcase_25 AC 277 ms
77,584 KB
testcase_26 AC 289 ms
77,552 KB
testcase_27 AC 278 ms
77,564 KB
testcase_28 AC 296 ms
77,580 KB
testcase_29 AC 266 ms
70,676 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