結果

問題 No.1283 Extra Fee
ユーザー snrnsidysnrnsidy
提出日時 2021-12-03 04:46:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 1,444 bytes
コンパイル時間 2,119 ms
コンパイル使用メモリ 209,416 KB
実行使用メモリ 12,256 KB
最終ジャッジ日時 2024-04-28 00:44:57
合計ジャッジ時間 5,559 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 10 ms
6,940 KB
testcase_12 AC 10 ms
6,940 KB
testcase_13 AC 7 ms
6,944 KB
testcase_14 AC 28 ms
7,976 KB
testcase_15 AC 43 ms
8,008 KB
testcase_16 AC 10 ms
6,940 KB
testcase_17 AC 136 ms
12,164 KB
testcase_18 AC 145 ms
9,292 KB
testcase_19 AC 156 ms
9,284 KB
testcase_20 AC 149 ms
9,228 KB
testcase_21 AC 149 ms
9,224 KB
testcase_22 AC 135 ms
8,800 KB
testcase_23 AC 130 ms
9,248 KB
testcase_24 AC 146 ms
9,352 KB
testcase_25 AC 163 ms
9,332 KB
testcase_26 AC 163 ms
9,404 KB
testcase_27 AC 163 ms
9,400 KB
testcase_28 AC 162 ms
9,404 KB
testcase_29 AC 150 ms
12,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
     
using namespace std;

long long int dist[501][501][2];
int dy[4] = {-1,0,1,0};
int dx[4] = {0,1,0,-1};
int n,m,h,w,c;
long long int cost[501][501];
int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> n >> m;

	for(int i=0;i<m;i++)
	{
		cin >> h >> w >> c;
		cost[h][w] = c;
	}

	priority_queue <pair<long long int,pair<pair<int,int>,int>>> pque;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			dist[i][j][0] = 1e18;
			dist[i][j][1] = 1e18;
		}
	}
	dist[1][1][0] = 0;
	pque.push(make_pair(0,make_pair(make_pair(1,1),0)));

	while(!pque.empty())
	{
		int y = pque.top().second.first.first;
		int x = pque.top().second.first.second;
		int c = pque.top().second.second;
		if(dist[y][x][c] < -pque.top().first)
		{
			pque.pop();
			continue;
		}
		pque.pop();

		for(int k=0;k<4;k++)
		{
			int ny = y + dy[k];
			int nx = x + dx[k];
			if(ny<=0 || ny>n || nx<=0 || nx>n) continue;
			if(c==0)
			{
				if(dist[ny][nx][1] > dist[y][x][c] + 1)
				{
					dist[ny][nx][1] = dist[y][x][c] + 1;
					pque.push(make_pair(-dist[ny][nx][1],make_pair(make_pair(ny,nx),1)));
				}
			}
			if(dist[ny][nx][c] > dist[y][x][c] + cost[ny][nx] + 1)
			{
				dist[ny][nx][c] = dist[y][x][c] + cost[ny][nx] + 1;
				pque.push(make_pair(-dist[ny][nx][c],make_pair(make_pair(ny,nx),c)));
			}			
		}
	}

	long long int res = dist[n][n][0];
	res = min(res,dist[n][n][1]);

	cout << res << '\n';

	return 0;
}
0