結果

問題 No.1283 Extra Fee
ユーザー Mr.SpockMr.Spock
提出日時 2021-01-01 15:05:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,629 bytes
コンパイル時間 2,081 ms
コンパイル使用メモリ 176,136 KB
実行使用メモリ 13,328 KB
最終ジャッジ日時 2024-04-19 11:42:45
合計ジャッジ時間 4,839 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,552 KB
testcase_01 AC 4 ms
7,552 KB
testcase_02 AC 5 ms
7,552 KB
testcase_03 AC 5 ms
7,424 KB
testcase_04 WA -
testcase_05 AC 4 ms
7,424 KB
testcase_06 AC 5 ms
7,296 KB
testcase_07 AC 5 ms
7,424 KB
testcase_08 AC 4 ms
7,424 KB
testcase_09 AC 4 ms
7,552 KB
testcase_10 AC 5 ms
7,424 KB
testcase_11 AC 12 ms
7,936 KB
testcase_12 AC 13 ms
7,808 KB
testcase_13 AC 10 ms
7,424 KB
testcase_14 AC 31 ms
7,808 KB
testcase_15 AC 47 ms
7,808 KB
testcase_16 AC 13 ms
7,808 KB
testcase_17 AC 146 ms
13,268 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 136 ms
9,344 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 153 ms
13,328 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 = 1e9;
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