結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-11-07 00:21:14 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 442 ms / 2,000 ms |
| コード長 | 1,216 bytes |
| コンパイル時間 | 2,433 ms |
| コンパイル使用メモリ | 212,796 KB |
| 最終ジャッジ日時 | 2025-01-15 21:21:25 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int N,M; cin >> N >> M;
vector<vector<int>> A(N, vector<int> (N,0));
rep(i,M){
int h,w,c; cin >> h >> w >> c;
h--; w--;
A[h][w] = c;
}
priority_queue<tuple<ll,int,int,int>, vector<tuple<ll,int,int,int>>,greater<tuple<ll,int,int,int>>> Q;
vector<vector<vector<ll>>> cost(N,vector<vector<ll>>(N, vector<ll> (2,-1)));
Q.push({0,0,0,0});
while(!Q.empty()){
auto [sum,y,x,u] = Q.top(); Q.pop();
if(cost[y][x][u] == -1){
cost[y][x][u] = sum;
rep(i,4){
int ny = y + dy[i];
int nx = x + dx[i];
if(ny < 0 || N <= ny || nx < 0 || N <= nx) continue;
if(cost[ny][nx][u] == -1){
Q.push({sum+A[ny][nx]+1, ny, nx, u});
}
if(u == 0 && cost[ny][nx][1] == -1){
Q.push({sum+1, ny, nx, 1});
}
}
}
}
cout << cost[N-1][N-1][1] << '\n';
}