結果
問題 |
No.1283 Extra Fee
|
ユーザー |
![]() |
提出日時 | 2020-11-10 23:38:39 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 337 ms / 2,000 ms |
コード長 | 1,740 bytes |
コンパイル時間 | 1,396 ms |
コンパイル使用メモリ | 129,740 KB |
最終ジャッジ日時 | 2025-01-15 22:03:03 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #include <time.h> #include <stack> #include <array> #define popcount __builtin_popcount using namespace std; using ll=long long; typedef pair<ll, ll> P; int main() { int n, m; cin>>n>>m; int c[505][505]={}; for(int i=0; i<m; i++){ int h, w, ci; cin>>h>>w>>ci; h--; w--; c[h][w]=ci; } const ll INF=1e18; ll dp[2][505][505]; for(int i=0; i<n; i++) for(int j=0; j<n; j++) for(int k=0; k<2; k++) dp[k][i][j]=INF; dp[0][0][0]=0; using PP=pair<P, P>; priority_queue<PP, vector<PP>, greater<PP>> que; que.push({{0, 0}, {0, 0}}); int dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1}; while(!que.empty()){ PP p=que.top(); que.pop(); int t=p.first.second, x=p.second.first, y=p.second.second; if(dp[t][x][y]<p.first.first) continue; for(int k=0; k<4; k++){ int x1=x+dx[k], y1=y+dy[k]; if(x1<0 || x1>=n || y1<0 || y1>=n) continue; if(dp[t][x1][y1]>dp[t][x][y]+c[x1][y1]+1){ dp[t][x1][y1]=dp[t][x][y]+c[x1][y1]+1; que.push({{dp[t][x1][y1], t}, {x1, y1}}); } if(t==0 && dp[1][x1][y1]>dp[0][x][y]+1){ dp[1][x1][y1]=dp[0][x][y]+1; que.push({{dp[1][x1][y1], 1}, {x1, y1}}); } } } cout<<dp[1][n-1][n-1]<<endl; return 0; }