結果

問題 No.1283 Extra Fee
ユーザー FlkanjinFlkanjin
提出日時 2022-01-05 17:16:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 365 ms / 2,000 ms
コード長 3,445 bytes
コンパイル時間 1,710 ms
コンパイル使用メモリ 163,916 KB
実行使用メモリ 53,228 KB
最終ジャッジ日時 2024-04-28 00:46:00
合計ジャッジ時間 7,463 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 15 ms
6,016 KB
testcase_12 AC 17 ms
6,528 KB
testcase_13 AC 14 ms
5,376 KB
testcase_14 AC 57 ms
12,032 KB
testcase_15 AC 91 ms
17,280 KB
testcase_16 AC 19 ms
6,400 KB
testcase_17 AC 233 ms
48,036 KB
testcase_18 AC 327 ms
49,408 KB
testcase_19 AC 350 ms
51,508 KB
testcase_20 AC 337 ms
48,548 KB
testcase_21 AC 337 ms
49,280 KB
testcase_22 AC 302 ms
44,416 KB
testcase_23 AC 293 ms
53,228 KB
testcase_24 AC 352 ms
53,228 KB
testcase_25 AC 360 ms
53,120 KB
testcase_26 AC 363 ms
53,120 KB
testcase_27 AC 361 ms
53,120 KB
testcase_28 AC 365 ms
53,120 KB
testcase_29 AC 253 ms
51,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10L;

template<class T, class S> inline bool chmax(T &a, const S &b){
    if(a < b){a = b; return true;}
    return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
    if(b < a){a = b; return true;}
    return false;
}
template<class T, class Container> inline bool exist(Container &s, const T &e){
    return (s.find(e) != std::end(s));
}
template<class T> inline bool inside(T x, T lx, T rx){
    return (std::clamp(x, lx, rx) == x);
}
template<class T> inline bool inside(T x, T y, T lx, T rx, T ly, T ry){
    return inside(x, lx, rx) && inside(y, ly, ry);
}






struct edge{
    int to, cost;
    edge(int To, int Cost): to(To), cost(Cost){}
};

void dijkstra(int s, std::vector<std::vector<edge>> &G, std::vector<long long> &d, std::vector<int> &prv){
    //pair<int, int> first: 距離 second: 頂点
    std::priority_queue<std::pair<long long, int>,
                        std::vector<std::pair<long long, int>>,
                        std::greater<std::pair<long long, int>>> que;
    int V{int(G.size())};
    d.resize(V, LINF+3);
    prv.resize(V, NIL);
    d[s] = 0;
    que.emplace(0, s);

    while(!que.empty()){
        std::pair<long long, int> p{que.top()}; que.pop();
        int v{p.second};
        if(d[v] < p.first) continue;
        for(edge &e: G[v]){
            if(d[e.to] > d[v] + e.cost){
                d[e.to] = d[v] + e.cost;
                prv[e.to] = v;
                que.emplace(d[e.to], e.to);
            }
        }
    }
}

int vertex(int h, int w, int N){
    return h*N + w;
}

int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};

int main(){
    int N, M; std::cin >> N >> M;
    int n{N * N};
    std::vector<std::vector<edge>> G(2*n);
    std::vector cst(N, std::vector<int>(N, 1));
    {
        int h, w, c;
        for(int i{0}; i < M; ++i){
            std::cin >> h >> w >> c;
            cst[h-1][w-1] += c;
        }
    }
    for(int i{0}; i < N; ++i){
        for(int j{0}; j < N; ++j){
            int vtx{vertex(i, j, N)};
            for(int d{0}; d < 4; ++d){
                int ni{i + dx[d]}, nj{j + dy[d]};
                if(!inside(ni, nj, 0, N-1, 0, N-1)) continue;
                int nvtx{vertex(ni, nj, N)};
                G[vtx].emplace_back(nvtx, cst[ni][nj]);
                G[n+vtx].emplace_back(n+nvtx, cst[ni][nj]);
                if(cst[ni][nj] > 1) G[vtx].emplace_back(n+nvtx, 1);
            }
        }
    }
    std::vector<long long> d;
    std::vector<int> prv;
    dijkstra(0, G, d, prv);
    std::cout << std::min(d[n-1], d[2*n-1]) << std::endl;
    return 0;
}
0