結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
platinum
|
| 提出日時 | 2020-11-05 18:05:25 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,629 bytes |
| コンパイル時間 | 816 ms |
| コンパイル使用メモリ | 79,224 KB |
| 実行使用メモリ | 209,328 KB |
| 最終ジャッジ日時 | 2024-07-22 11:14:36 |
| 合計ジャッジ時間 | 6,151 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 11 TLE * 1 -- * 18 |
ソースコード
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cassert>
#define rep(i,n) for(int i=0; i<(int)(n); i++)
using namespace std;
using LL = long long;
const int Max_N = 500;
const LL Max_C = 1e9;
const LL INF = 1e18;
int dw[4] = {1, 0, -1, 0};
int dh[4] = {0, -1, 0, 1};
struct V{
int h, w;
LL c;
bool used;
bool operator > (const V &v) const{
return c > v.c;
}
V(int h, int w, LL c, bool used): h(h), w(w), c(c), used(used) {}
};
LL dist[Max_N + 5][Max_N + 5][2];
LL cost[Max_N + 5][Max_N + 5];
int main(){
int N, M;
cin >> N >> M;
assert(2 <= N and N <= Max_N);
assert(1 <= M and M <= N * N - 2);
rep(i,M){
int h, w;
LL c;
cin >> h >> w >> c;
assert(1 <= h and h <= N);
assert(1 <= w and w <= N);
assert(1 <= c and c <= Max_C);
h--; w--;
cost[h][w] = c;
}
rep(i,N){
rep(j,N){
rep(k,2) dist[i][j][k] = INF;
}
}
dist[0][0][0] = 0;
priority_queue<V,vector<V>,greater<V>> q;
q.push(V(0, 0, 0, false));
while(!q.empty()){
V v = q.top(); q.pop();
int h = v.h, w = v.w;
LL c = v.c;
if(c > dist[h][w][v.used]) continue;
rep(i,4){
int nh = h + dh[i], nw = w + dw[i];
if(nh < 0 or nh >= N) continue;
if(nw < 0 or nw >= N) continue;
LL nc = c + cost[nh][nw] + 1;
if(nc > dist[nh][nw][v.used]) continue;
dist[nh][nw][v.used] = nc;
V nv(nh, nw, nc, v.used);
q.push(nv);
if(!v.used and cost[nh][nw] > 0){
dist[nh][nw][1] = c + 1;
V nv2(nh, nw, c + 1, true);
q.push(nv2);
}
}
}
cout << min(dist[N - 1][N - 1][0], dist[N - 1][N - 1][1]) << endl;
return 0;
}
platinum