結果

問題 No.1283 Extra Fee
ユーザー 👑 platinumplatinum
提出日時 2020-09-05 00:19:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 256 ms / 2,000 ms
コード長 1,727 bytes
コンパイル時間 749 ms
コンパイル使用メモリ 81,368 KB
実行使用メモリ 16,724 KB
最終ジャッジ日時 2023-08-10 05:48:36
合計ジャッジ時間 7,505 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,556 KB
testcase_01 AC 2 ms
5,384 KB
testcase_02 AC 2 ms
5,380 KB
testcase_03 AC 2 ms
5,424 KB
testcase_04 AC 2 ms
5,520 KB
testcase_05 AC 2 ms
5,372 KB
testcase_06 AC 2 ms
5,520 KB
testcase_07 AC 2 ms
5,440 KB
testcase_08 AC 2 ms
5,424 KB
testcase_09 AC 2 ms
5,748 KB
testcase_10 AC 2 ms
5,388 KB
testcase_11 AC 10 ms
8,200 KB
testcase_12 AC 12 ms
8,336 KB
testcase_13 AC 10 ms
7,980 KB
testcase_14 AC 40 ms
10,852 KB
testcase_15 AC 64 ms
11,072 KB
testcase_16 AC 14 ms
8,468 KB
testcase_17 AC 135 ms
16,576 KB
testcase_18 AC 224 ms
13,856 KB
testcase_19 AC 243 ms
13,976 KB
testcase_20 AC 225 ms
13,764 KB
testcase_21 AC 228 ms
14,016 KB
testcase_22 AC 207 ms
13,684 KB
testcase_23 AC 187 ms
13,960 KB
testcase_24 AC 251 ms
13,776 KB
testcase_25 AC 248 ms
13,836 KB
testcase_26 AC 242 ms
13,844 KB
testcase_27 AC 256 ms
13,920 KB
testcase_28 AC 252 ms
13,896 KB
testcase_29 AC 151 ms
16,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <queue>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
using LL = long long;
const int Max_N = 700;
const LL Max_F = 1e9;
const LL INF = 1e18;

int dh[4]={1,0,-1,0};
int dw[4]={0,1,0,-1};

struct ver{
	int h, w, used;
	LL fee;
	ver(int h, int w, LL fee, int used):
	h(h), w(w), fee(fee), used(used){}
	bool operator>(const ver& v) const{
		if(fee == v.fee) return used > v.used;
		return fee > v.fee;
	}
};

LL c[Max_N][Max_N];
LL dist[Max_N][Max_N][2];

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 f;
		cin >> h >> w >> f;
		assert(1<=h and h<=N);
		assert(1<=w and w<=N);
		assert(1<=f and f<=Max_F);
		h--; w--;
		c[h][w]=f;
	}
	priority_queue<ver,vector<ver>,greater<ver>> q;
	rep(i,N){
		rep(j,N){
			rep(k,2) dist[i][j][k]=INF;
		}
	}
	dist[0][0][0]=0, dist[0][0][1]=0;
	ver st(0,0,0,0);
	q.push(st);
	while(!q.empty()){
		ver v=q.top(); q.pop();
		int h=v.h, w=v.w, used=v.used;
		LL fee=v.fee;
		if(dist[h][w][used]<fee) 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 nf0=fee+c[nh][nw]+1, nf1=fee+1;
			if(!used){
				if(dist[nh][nw][0]>nf0){
					ver next0(nh,nw,nf0,0);
					q.push(next0);
					dist[nh][nw][0]=nf0;
				}
				if(dist[nh][nw][1]>nf1){
					ver next1(nh,nw,nf1,1);
					q.push(next1);
					dist[nh][nw][1]=nf1;
				}
			}
			else{
				if(dist[nh][nw][1]>nf0){
					ver next(nh,nw,nf0,1);
					q.push(next);
					dist[nh][nw][1]=nf0;
				}
			}
		}
	}
	cout << min(dist[N-1][N-1][0],dist[N-1][N-1][1]) << endl;

	return 0;
}
0