結果

問題 No.1283 Extra Fee
ユーザー 👑 platinumplatinum
提出日時 2020-11-05 19:50:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 241 ms / 2,000 ms
コード長 1,666 bytes
コンパイル時間 862 ms
コンパイル使用メモリ 78,776 KB
実行使用メモリ 13,448 KB
最終ジャッジ日時 2024-04-27 23:07:40
合計ジャッジ時間 4,915 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 8 ms
6,944 KB
testcase_12 AC 12 ms
6,944 KB
testcase_13 AC 9 ms
6,940 KB
testcase_14 AC 37 ms
8,052 KB
testcase_15 AC 58 ms
8,220 KB
testcase_16 AC 13 ms
6,944 KB
testcase_17 AC 119 ms
12,276 KB
testcase_18 AC 232 ms
9,192 KB
testcase_19 AC 226 ms
9,444 KB
testcase_20 AC 219 ms
9,244 KB
testcase_21 AC 232 ms
9,244 KB
testcase_22 AC 208 ms
9,020 KB
testcase_23 AC 172 ms
9,320 KB
testcase_24 AC 224 ms
9,460 KB
testcase_25 AC 233 ms
9,584 KB
testcase_26 AC 236 ms
9,432 KB
testcase_27 AC 234 ms
9,480 KB
testcase_28 AC 241 ms
9,712 KB
testcase_29 AC 122 ms
13,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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, 0));
  while(!q.empty()){
  	V v = q.top(); q.pop();
  	int h = v.h, w = v.w;
  	LL c = v.c;
  	bool used = v.used;
  	if(c > dist[h][w][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][used]){
  			dist[nh][nw][used] = nc;
  			V nv(nh, nw, nc, used);
  			q.push(nv);
  		}
  		if(!used and cost[nh][nw] > 0 and dist[nh][nw][1] > c + 1){
  			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;
}
0