結果
| 問題 | No.1283 Extra Fee | 
| コンテスト | |
| ユーザー |  Flkanjin | 
| 提出日時 | 2022-01-05 17:16:18 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 442 ms / 2,000 ms | 
| コード長 | 3,445 bytes | 
| コンパイル時間 | 1,750 ms | 
| コンパイル使用メモリ | 157,724 KB | 
| 最終ジャッジ日時 | 2025-01-27 08:49:31 | 
| ジャッジサーバーID (参考情報) | judge3 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 30 | 
ソースコード
#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;
}
            
            
            
        