結果

問題 No.1283 Extra Fee
ユーザー 沙耶花沙耶花
提出日時 2020-11-06 21:53:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 1,329 bytes
コンパイル時間 2,285 ms
コンパイル使用メモリ 216,780 KB
実行使用メモリ 25,344 KB
最終ジャッジ日時 2023-08-10 05:57:29
合計ジャッジ時間 8,258 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 14 ms
4,636 KB
testcase_12 AC 18 ms
4,552 KB
testcase_13 AC 12 ms
4,384 KB
testcase_14 AC 52 ms
6,204 KB
testcase_15 AC 89 ms
7,868 KB
testcase_16 AC 19 ms
4,384 KB
testcase_17 AC 251 ms
25,344 KB
testcase_18 AC 301 ms
18,196 KB
testcase_19 AC 330 ms
18,660 KB
testcase_20 AC 310 ms
17,676 KB
testcase_21 AC 309 ms
17,920 KB
testcase_22 AC 290 ms
16,624 KB
testcase_23 AC 279 ms
19,368 KB
testcase_24 AC 349 ms
19,144 KB
testcase_25 AC 347 ms
19,168 KB
testcase_26 AC 337 ms
19,128 KB
testcase_27 AC 334 ms
19,144 KB
testcase_28 AC 339 ms
19,144 KB
testcase_29 AC 270 ms
25,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 10000000000000000

int main(){
	
	int N,M;
	cin>>N>>M;
	
	vector<vector<long long>> cost(N,vector<long long>(N,0));
	
	rep(i,M){
		long long h,w,c;
		cin>>h>>w>>c;
		
		cost[h-1][w-1] = c;
	}
	
	vector dis(N,vector(N,vector<long long>(2,Inf)));
	
	dis[0][0][0] = 0;
	
	priority_queue<pair<long long,vector<long long>>,vector<pair<long long,vector<long long>>>,greater<pair<long long,vector<long long>>>> Q;
	Q.emplace(0,vector<long long>{0,0,0});
	
	vector<int> dx = {0,1,0,-1},dy = {1,0,-1,0};
	
	while(Q.size()>0){
		long long D = Q.top().first;
		int y = Q.top().second[0];
		int x = Q.top().second[1];
		int used = Q.top().second[2];
		Q.pop();
		
		if(dis[y][x][used]!=D)continue;
		
		rep(i,4){
			int yy = y+dy[i],xx = x+dx[i];
			if(yy<0||yy>=N||xx<0||xx>=N)continue;
			long long c = 1;
			if(used==0){
				if(dis[yy][xx][1]>dis[y][x][0]+c){
					dis[yy][xx][1] = dis[y][x][0]+c;
					Q.emplace(dis[yy][xx][1],vector<long long>{yy,xx,1});
				}
			}
			c += cost[yy][xx];
			if(dis[yy][xx][used]>dis[y][x][used]+c){
				dis[yy][xx][used] = dis[y][x][used] + c;
				Q.emplace(dis[yy][xx][used],vector<long long>{yy,xx,used});
			}
		}
	}
	
	cout<<dis.back().back().back()<<endl;
	
    return 0;
}
0