結果

問題 No.1283 Extra Fee
ユーザー tkmst201tkmst201
提出日時 2021-02-16 18:18:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 1,466 bytes
コンパイル時間 2,059 ms
コンパイル使用メモリ 220,512 KB
実行使用メモリ 20,292 KB
最終ジャッジ日時 2024-04-27 23:48:16
合計ジャッジ時間 5,343 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 9 ms
6,944 KB
testcase_12 AC 10 ms
6,940 KB
testcase_13 AC 7 ms
6,944 KB
testcase_14 AC 29 ms
6,944 KB
testcase_15 AC 42 ms
7,424 KB
testcase_16 AC 10 ms
6,940 KB
testcase_17 AC 149 ms
18,904 KB
testcase_18 AC 147 ms
17,024 KB
testcase_19 AC 159 ms
17,664 KB
testcase_20 AC 147 ms
16,640 KB
testcase_21 AC 147 ms
17,024 KB
testcase_22 AC 127 ms
15,616 KB
testcase_23 AC 139 ms
17,920 KB
testcase_24 AC 148 ms
17,920 KB
testcase_25 AC 160 ms
18,048 KB
testcase_26 AC 158 ms
18,176 KB
testcase_27 AC 163 ms
18,048 KB
testcase_28 AC 159 ms
18,176 KB
testcase_29 AC 145 ms
20,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

int main() {
	int N, M;
	cin >> N >> M;
	vector C(N, vector<int>(N));
	REP(i, M) {
		int h, w, c;
		scanf("%d %d %d", &h, &w, &c);
		C[h - 1][w - 1] = c;
	}
	vector dp(N, vector(N, vector(2, longINF)));
	using tup = tuple<ll, int, int>;
	priority_queue<tup, vector<tup>, greater<tup>> pq;
	dp[0][0][0] = 0;
	pq.emplace(0, 0, 0);
	while (!pq.empty()) {
		auto [c, p, f] = pq.top(); pq.pop();
		int y = p / N, x = p % N;
		if (c != dp[y][x][f]) continue;
		int dx[] {1,0,-1,0}, dy[] {0,1,0,-1};
		REP(dir, 4) {
			const int ny = y + dy[dir], nx = x + dx[dir];
			if (ny < 0 || ny >= N || nx < 0 || nx >= N) continue;
			if (chmin(dp[ny][nx][f], dp[y][x][f] + C[ny][nx] + 1)) pq.emplace(dp[ny][nx][f], ny * N + nx, f);
			if (!f && chmin(dp[ny][nx][1], dp[y][x][f] + 1)) pq.emplace(dp[ny][nx][1], ny * N + nx, 1);
		}
	}
	
	cout << dp[N - 1][N - 1][1] << endl;
}
0