結果

問題 No.1283 Extra Fee
ユーザー 👑 kmjpkmjp
提出日時 2020-11-06 22:07:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 1,618 bytes
コンパイル時間 2,164 ms
コンパイル使用メモリ 205,092 KB
実行使用メモリ 9,996 KB
最終ジャッジ日時 2023-08-10 05:58:57
合計ジャッジ時間 5,618 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,408 KB
testcase_01 AC 2 ms
5,576 KB
testcase_02 AC 2 ms
5,528 KB
testcase_03 AC 2 ms
5,700 KB
testcase_04 AC 2 ms
5,452 KB
testcase_05 AC 2 ms
5,496 KB
testcase_06 AC 2 ms
5,436 KB
testcase_07 AC 2 ms
5,424 KB
testcase_08 AC 2 ms
5,520 KB
testcase_09 AC 2 ms
5,512 KB
testcase_10 AC 2 ms
5,456 KB
testcase_11 AC 8 ms
6,324 KB
testcase_12 AC 10 ms
6,272 KB
testcase_13 AC 7 ms
6,236 KB
testcase_14 AC 26 ms
7,196 KB
testcase_15 AC 39 ms
8,044 KB
testcase_16 AC 9 ms
6,216 KB
testcase_17 AC 104 ms
9,908 KB
testcase_18 AC 132 ms
8,596 KB
testcase_19 AC 138 ms
8,396 KB
testcase_20 AC 132 ms
8,308 KB
testcase_21 AC 133 ms
8,372 KB
testcase_22 AC 119 ms
8,244 KB
testcase_23 AC 111 ms
8,276 KB
testcase_24 AC 128 ms
8,476 KB
testcase_25 AC 144 ms
8,440 KB
testcase_26 AC 142 ms
8,332 KB
testcase_27 AC 141 ms
8,344 KB
testcase_28 AC 142 ms
8,332 KB
testcase_29 AC 115 ms
9,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
int C[500][500];

ll D[500][500][2];
void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	FOR(i,M) {
		cin>>y>>x>>r;
		C[y-1][x-1]=r;
	}
	FOR(y,N) FOR(x,N) FOR(i,2) D[y][x][i]=1LL<<60;
	D[0][0][0]=0;
	priority_queue<pair<ll,int>> Q;
	Q.push({0,0});
	while(Q.size()) {
		ll co=-Q.top().first;
		int cy=Q.top().second/500%500;
		int cx=Q.top().second%500;
		int turn=Q.top().second/500/500;
		Q.pop();
		if(D[cy][cx][turn]!=co) continue;
		
		if(cy==N-1&&cx==N-1) {
			cout<<co<<endl;
			return;
		}
		co++;
		int d[]={1,0,-1,0};
		FOR(i,4) {
			int ty=cy+d[i];
			int tx=cx+d[i^1];
			if(tx<0||ty<0||tx>=N||ty>=N) continue;
			if(D[ty][tx][turn]>co+C[ty][tx]) {
				D[ty][tx][turn]=co+C[ty][tx];
				Q.push({-D[ty][tx][turn],turn*500*500+ty*500+tx});
			}
			if(turn==0&&D[ty][tx][1]>co) {
				D[ty][tx][1]=co;
				Q.push({-D[ty][tx][1],1*500*500+ty*500+tx});
			}
			
			
		}
		
	}
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0