結果

問題 No.1283 Extra Fee
ユーザー outlineoutline
提出日時 2020-12-09 22:35:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 2,517 bytes
コンパイル時間 1,558 ms
コンパイル使用メモリ 145,032 KB
実行使用メモリ 10,420 KB
最終ジャッジ日時 2024-04-27 23:45:10
合計ジャッジ時間 4,995 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 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,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 9 ms
6,940 KB
testcase_12 AC 11 ms
6,944 KB
testcase_13 AC 8 ms
6,940 KB
testcase_14 AC 28 ms
6,944 KB
testcase_15 AC 43 ms
7,184 KB
testcase_16 AC 11 ms
6,940 KB
testcase_17 AC 121 ms
10,016 KB
testcase_18 AC 141 ms
8,192 KB
testcase_19 AC 150 ms
8,256 KB
testcase_20 AC 139 ms
8,072 KB
testcase_21 AC 143 ms
8,320 KB
testcase_22 AC 127 ms
7,804 KB
testcase_23 AC 124 ms
8,284 KB
testcase_24 AC 142 ms
8,320 KB
testcase_25 AC 154 ms
8,436 KB
testcase_26 AC 155 ms
8,312 KB
testcase_27 AC 156 ms
8,272 KB
testcase_28 AC 159 ms
8,452 KB
testcase_29 AC 131 ms
10,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

constexpr ll inf = 1e+17;
using Data = tuple<ll, int, int>;
ll dist[505][505][2];
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};

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

    int N, M;
    cin >> N >> M;
    vector<vector<int>> c(N, vector<int>(N));
    for(int i = 0; i < M; ++i){
        int h, w;
        cin >> h >> w;
        --h, --w;
        cin >> c[h][w];
    }
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < N; ++j){
            dist[i][j][0] = dist[i][j][1] = inf;
        }
    }
    dist[0][0][0] = 0;
    priority_queue<Data, vector<Data>, greater<Data>> que;
    que.emplace(0, 0, 0);
    while(!que.empty()){
        auto [d, num, f] = que.top();
        que.pop();
        int x = num / N, y = num % N;
        if(d > dist[x][y][f])   continue;
        for(int i = 0; i < 4; ++i){
            int nx = x + dx[i], ny = y + dy[i];
            if(nx < 0 || nx >= N || ny < 0 || ny >= N)  continue;
            if(f == 1){
                if(chmin(dist[nx][ny][1], dist[x][y][1] + 1 + c[nx][ny])){
                    que.emplace(dist[nx][ny][1], nx * N + ny, 1);
                }
            }
            else{
                if(chmin(dist[nx][ny][1], dist[x][y][0] + 1)){
                    que.emplace(dist[nx][ny][1], nx * N + ny, 1);
                }
                if(chmin(dist[nx][ny][0], dist[x][y][0] + 1 + c[nx][ny])){
                    que.emplace(dist[nx][ny][0], nx * N + ny, 0);
                }
            }
        }
    }
    cout << min(dist[N - 1][N - 1][0], dist[N - 1][N - 1][1]) << endl;

    return 0;
}
0