結果

問題 No.1283 Extra Fee
ユーザー 👑 platinumplatinum
提出日時 2020-09-05 01:51:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 443 ms / 2,000 ms
コード長 1,872 bytes
コンパイル時間 936 ms
コンパイル使用メモリ 95,704 KB
実行使用メモリ 25,264 KB
最終ジャッジ日時 2024-04-27 23:06:50
合計ジャッジ時間 7,060 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 11 ms
6,940 KB
testcase_12 AC 15 ms
6,980 KB
testcase_13 AC 13 ms
6,940 KB
testcase_14 AC 59 ms
10,832 KB
testcase_15 AC 97 ms
12,852 KB
testcase_16 AC 21 ms
8,980 KB
testcase_17 AC 157 ms
16,208 KB
testcase_18 AC 377 ms
22,780 KB
testcase_19 AC 411 ms
24,608 KB
testcase_20 AC 397 ms
23,584 KB
testcase_21 AC 389 ms
23,832 KB
testcase_22 AC 340 ms
22,088 KB
testcase_23 AC 388 ms
25,020 KB
testcase_24 AC 443 ms
24,888 KB
testcase_25 AC 438 ms
25,140 KB
testcase_26 AC 442 ms
25,264 KB
testcase_27 AC 433 ms
25,144 KB
testcase_28 AC 439 ms
25,140 KB
testcase_29 AC 160 ms
15,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;
using LL = long long;
using P = pair<int,int>;
const int Max_N = 500;
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);
	map<P,int> mp;
	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);
		assert(h!=1 or w!=1);
		assert(h!=N or w!=N);
		h--; w--;
		assert(mp[P(h,w)]==0);
		mp[P(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