結果

問題 No.1283 Extra Fee
ユーザー nawawannawawan
提出日時 2020-11-06 23:29:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,488 bytes
コンパイル時間 1,153 ms
コンパイル使用メモリ 108,196 KB
実行使用メモリ 81,324 KB
最終ジャッジ日時 2023-09-29 19:41:13
合計ジャッジ時間 6,259 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,480 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 16 ms
6,436 KB
testcase_12 AC 21 ms
7,224 KB
testcase_13 AC 16 ms
5,888 KB
testcase_14 AC 73 ms
15,652 KB
testcase_15 AC 139 ms
23,348 KB
testcase_16 AC 24 ms
7,220 KB
testcase_17 AC 353 ms
57,592 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
typedef pair<int, int> P;
struct edge{
    int to;
    long long cost;
};
struct dijkstra{//priority_queueを用いる
    vector<vector<edge>> G;
    vector<long long> d;
    long long INF;
    int n;
    dijkstra(int N){
        G.resize(N);
        INF = 1e18;
        d.resize(N, INF);
        n = N;
    }
    void add(int x, int y, long long cost1){
        edge e2 = {y, cost1};
        G[x].push_back(e2);
    }
    void solve(int t){
        priority_queue<P, vector<P>, greater<P>> que;
        int s = t;//スタート地点
        d[s] = 0;
        que.push(P(0, s));
        while(!que.empty()){
            P p = que.top();
            que.pop();
            int v = p.second;
            if(d[v] < p.first) continue;
            for(int i = 0; i < G[v].size(); i++){
                edge e = G[v][i];
                if(d[e.to] > d[v] + e.cost){
                    d[e.to] = d[v] + e.cost;
                    que.push(P(d[e.to], e.to));
                }
            }
        }
    }
};
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, M;
    cin >> N >> M;
    vector<int> h(M), w(M), c(M);
    set<P> s;
    for(int i = 0; i < M; i++) {
        cin >> h[i] >> w[i] >> c[i];
        h[i]--;
        w[i]--;
        s.insert(P(h[i], w[i]));
    }
    dijkstra d1(N * N), d2(N * N);
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            if(s.count(P(i, j))) continue;
            for(int k = 0; k < 4; k++){
                int nx = i + dx[k], ny = j + dy[k];
                if(0 <= nx && nx < N && 0 <= ny && ny < N){
                    d1.add(nx * N + ny, i * N + j, 1);
                    d2.add(nx * N + ny, i * N + j, 1);
                }
            }
        }
    }
    for(int i = 0; i < M; i++){
        for(int j = 0; j < 4; j++){
            int nx = h[i] + dx[j], ny = w[i] + dy[j];
            if(0 <= nx && nx < N && 0 <= ny && ny < N){
                d1.add(nx * N + ny, h[i] * N + w[i], 1 + c[i]);
                d2.add(nx * N + ny, h[i] * N + w[i], 1 + c[i]);
            }
        }
    }
    d1.solve(0);
    d2.solve(N * N - 1);
    long long ans = d1.d[N * N - 1];
    for(int i = 0; i < M; i++){
        ans = min(ans, d1.d[h[i] * N + w[i]] + d2.d[h[i] * N + w[i]] - c[i] * 2);
    }
    cout << ans << endl;
}
0