結果

問題 No.1283 Extra Fee
ユーザー Mr.SpockMr.Spock
提出日時 2021-01-01 15:06:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,630 bytes
コンパイル時間 1,756 ms
コンパイル使用メモリ 181,932 KB
実行使用メモリ 13,592 KB
最終ジャッジ日時 2024-04-27 23:45:45
合計ジャッジ時間 4,442 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,416 KB
testcase_01 AC 3 ms
7,144 KB
testcase_02 AC 3 ms
7,424 KB
testcase_03 AC 3 ms
7,336 KB
testcase_04 AC 3 ms
7,388 KB
testcase_05 AC 2 ms
7,460 KB
testcase_06 AC 2 ms
7,276 KB
testcase_07 AC 2 ms
7,392 KB
testcase_08 AC 3 ms
7,372 KB
testcase_09 AC 3 ms
7,284 KB
testcase_10 AC 2 ms
7,408 KB
testcase_11 AC 9 ms
7,764 KB
testcase_12 AC 10 ms
7,652 KB
testcase_13 AC 7 ms
7,364 KB
testcase_14 AC 23 ms
7,808 KB
testcase_15 AC 36 ms
7,828 KB
testcase_16 AC 9 ms
7,340 KB
testcase_17 AC 113 ms
13,532 KB
testcase_18 AC 125 ms
9,188 KB
testcase_19 AC 129 ms
9,260 KB
testcase_20 AC 120 ms
9,124 KB
testcase_21 AC 123 ms
9,144 KB
testcase_22 AC 109 ms
8,904 KB
testcase_23 AC 104 ms
9,220 KB
testcase_24 AC 115 ms
9,200 KB
testcase_25 AC 132 ms
9,340 KB
testcase_26 AC 134 ms
9,348 KB
testcase_27 AC 135 ms
9,336 KB
testcase_28 AC 137 ms
9,312 KB
testcase_29 AC 122 ms
13,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using T = tuple<ll, ll, ll, ll>;
const ll dx[] = {0, 0, 1, -1};
const ll dy[] = {1, -1, 0, 0};
priority_queue<T, vector<T>, greater<T>>p;
ll dp[505][505][2];
ll n, t;
const ll nax = 1e18;
bool islegal(ll i, ll j) {
	return (i >= 0 && i < n && j >= 0 && j < n);
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> t;
	vector<vector<ll>>v(n, vector<ll>(n, 0));
	for (ll i = 0; i < t; i++) {
		ll x, y, val;
		cin >> x >> y >> val;
		--x, --y;
		v[x][y] += val;
	}
	for (ll i = 0; i < n; i++) {
		for (ll j = 0; j < n; j++) {
			v[i][j] += 1;
		}
	}
	for (ll i = 0; i < 501; i++) {
		for (ll k = 0; k < 501; k++) {
			for (ll q = 0; q < 2; q++) {
				dp[i][k][q] = nax;
			}
		}
	}
	dp[0][0][0] = 0;
	p.push({0, 0, 0, 0});
	while (!p.empty()) {
		T r = p.top();
		p.pop();
		ll cur = get<0>(r);
		ll x = get<1>(r);
		ll y = get<2>(r);
		ll state = get<3>(r);
		if (dp[x][y][state] != cur)continue;
		for (ll k = 0; k < 4; k++) {
			ll nx = x + dx[k];
			ll ny = y + dy[k];
			//cout << nx << " " << ny << endl;
			if (islegal(nx, ny)) {
				if (dp[nx][ny][state] > cur + v[nx][ny]) {
					dp[nx][ny][state] = cur + v[nx][ny];
					p.push({dp[nx][ny][state], nx, ny, state});
				}
				if (state == 0) {
					if (dp[nx][ny][1] > cur + 1) {
						dp[nx][ny][1] = cur + 1;
						p.push({dp[nx][ny][1], nx, ny, 1});
					}
				}
			}
		}
	}
	// for (ll i = 0; i < n; i++) {
	// 	for (ll j = 0; j < n; j++) {
	// 		cout << dp[i][j][0] << " " << dp[i][j][1] << endl;
	// 	}
	// }
	cout << min(dp[n - 1][n - 1][1], dp[n - 1][n - 1][0]) << endl;
}
0