結果
問題 |
No.1283 Extra Fee
|
ユーザー |
![]() |
提出日時 | 2020-11-12 19:38:20 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 303 ms / 2,000 ms |
コード長 | 1,849 bytes |
コンパイル時間 | 1,983 ms |
コンパイル使用メモリ | 180,492 KB |
実行使用メモリ | 13,184 KB |
最終ジャッジ日時 | 2024-11-16 07:12:13 |
合計ジャッジ時間 | 6,815 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
#include <bits/stdc++.h> //#include <chrono> //#pragma GCC optimize("O3") using namespace std; #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() #define fi first #define se second using ll = long long; using vec = vector<ll>; using mat = vector<vec>; ll N,M,H,W,Q,K,A,B; string S; typedef pair<ll, ll> P; const ll INF = (1LL<<60); template<class T> bool chmin(T &a, const T &b){ if(a > b) {a = b; return true;} else return false; } template<class T> bool chmax(T &a, const T &b){ if(a < b) {a = b; return true;} else return false; } int main(){ cin>>N>>M; mat grid(N, vec(N, 0)); rep(i, M){ int h, w; cin>>h>>w; --h; --w; cin>>grid[h][w]; } vector<mat> dist(2, mat(N, vec(N, INF))); dist[0][0][0] = 0; priority_queue<P, vector<P>, greater<> > pque; pque.emplace(0, (0 * N + 0) * N + 0); vec dh = {-1, 0, 1, 0}, dw = {0, -1, 0, 1}; while(!pque.empty()){ P p = pque.top(); pque.pop(); ll d = p.first, flag = p.se >= N * N, h = (p.se % (N * N)) / N, w = p.se % N; if(dist[flag][h][w] < d) continue; //cout<<d<<' '<<flag<<' '<<h<<' '<<w<<endl; rep(i, 4){ int nh = h + dh[i], nw = w + dw[i]; if(nh >= 0 && nh < N && nw >= 0 && nw < N){ if(chmin(dist[flag][nh][nw], d + 1 + grid[nh][nw])){ pque.emplace(dist[flag][nh][nw], (flag * N + nh) * N + nw); } if(flag == 0 && grid[nh][nw] && chmin(dist[1][nh][nw], d + 1)){ pque.emplace(dist[1][nh][nw], (N + nh) * N + nw); } } } } cout<<min(dist[0][N-1][N-1], dist[1][N-1][N-1])<<endl; }