結果

問題 No.1283 Extra Fee
ユーザー 沙耶花沙耶花
提出日時 2020-11-06 21:53:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 325 ms / 2,000 ms
コード長 1,329 bytes
コンパイル時間 2,013 ms
コンパイル使用メモリ 219,732 KB
実行使用メモリ 25,732 KB
最終ジャッジ日時 2024-04-27 23:15:37
合計ジャッジ時間 7,111 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 13 ms
6,944 KB
testcase_12 AC 15 ms
6,944 KB
testcase_13 AC 11 ms
6,940 KB
testcase_14 AC 49 ms
6,940 KB
testcase_15 AC 76 ms
7,936 KB
testcase_16 AC 17 ms
6,944 KB
testcase_17 AC 218 ms
23,632 KB
testcase_18 AC 280 ms
18,048 KB
testcase_19 AC 309 ms
18,816 KB
testcase_20 AC 280 ms
17,920 KB
testcase_21 AC 287 ms
17,920 KB
testcase_22 AC 252 ms
16,384 KB
testcase_23 AC 250 ms
19,072 KB
testcase_24 AC 320 ms
19,328 KB
testcase_25 AC 321 ms
19,328 KB
testcase_26 AC 325 ms
19,200 KB
testcase_27 AC 306 ms
19,328 KB
testcase_28 AC 309 ms
19,328 KB
testcase_29 AC 232 ms
25,732 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