結果
| 問題 | No.1283 Extra Fee | 
| コンテスト | |
| ユーザー |  oevl | 
| 提出日時 | 2020-11-06 23:40:07 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 386 ms / 2,000 ms | 
| コード長 | 1,174 bytes | 
| コンパイル時間 | 2,063 ms | 
| コンパイル使用メモリ | 209,292 KB | 
| 最終ジャッジ日時 | 2025-01-15 21:17:21 | 
| ジャッジサーバーID (参考情報) | judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 30 | 
ソースコード
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
using Tup = tuple<long long, int, int, int>;
const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
const long long INF = 1e18;
int main() {
    int n, m;
    cin >> n >> m;
    vector cost(n, vector<long long>(n));
    for(int i = 0; i < m; ++i) {
        int h, w, c;
        cin >> h >> w >> c;
        h--, w--;
        cost[h][w] = c;
    }
    priority_queue<Tup, vector<Tup>, greater<Tup>> que;
    vector dist(n, vector(n, vector<long long>(2, INF)));
    auto push = [&](long long c, int y, int x, int t) {
        if(dist[y][x][t] <= c) return;
        que.emplace(c, y, x, t);
        dist[y][x][t] = c;
    };
    push(0, 0, 0, 0);
    while(!que.empty()) {
        auto[c, y, x, t] = que.top(); que.pop();
        for(int i = 0; i < 4; ++i) {
            int ny = y + dy[i], nx = x + dx[i];
            if(ny < 0 || nx < 0 || ny >= n || nx >= n) continue;
            if(t == 0) push(dist[y][x][t] + 1, ny, nx, 1);
            push(dist[y][x][t] + cost[ny][nx] + 1, ny, nx, t);
        }
    }
    cout << dist[n - 1][n - 1][1] << '\n';
    return 0;
}
            
            
            
        