結果

問題 No.867 避難経路
ユーザー kwm_tkwm_t
提出日時 2023-05-01 23:01:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,640 bytes
コンパイル時間 3,294 ms
コンパイル使用メモリ 224,956 KB
実行使用メモリ 128,768 KB
最終ジャッジ日時 2024-04-30 20:56:23
合計ジャッジ時間 76,716 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 4 ms
6,940 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 5 ms
6,940 KB
testcase_10 AC 4 ms
6,944 KB
testcase_11 AC 4 ms
6,944 KB
testcase_12 AC 4 ms
6,944 KB
testcase_13 AC 4 ms
6,940 KB
testcase_14 AC 4 ms
6,940 KB
testcase_15 AC 2,221 ms
128,640 KB
testcase_16 AC 2,174 ms
128,768 KB
testcase_17 AC 2,319 ms
128,640 KB
testcase_18 AC 2,177 ms
128,640 KB
testcase_19 AC 2,099 ms
128,640 KB
testcase_20 AC 2,040 ms
128,640 KB
testcase_21 AC 2,098 ms
128,768 KB
testcase_22 AC 2,144 ms
128,768 KB
testcase_23 AC 2,273 ms
128,768 KB
testcase_24 AC 2,096 ms
128,768 KB
testcase_25 AC 2,070 ms
128,640 KB
testcase_26 AC 2,101 ms
128,640 KB
testcase_27 AC 2,101 ms
128,640 KB
testcase_28 AC 2,152 ms
128,640 KB
testcase_29 AC 2,224 ms
128,640 KB
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 AC 14 ms
6,944 KB
testcase_42 AC 4 ms
6,944 KB
testcase_43 AC 4 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<long long,pair<int,int>>
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; }
const int dx[] = { 1,0,-1,0 };
const int dy[] = { 0,-1,0,1 };
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int h, w; cin >> h >> w;
	int gx, gy; cin >> gx >> gy;
	gx--, gy--;
	vector a(h, vector<int>(w));
	rep(i, h)rep(j, w)cin >> a[i][j];
	vector dp(250, vector(h, vector<long long>(w, LINF)));
	rep(i, 250) {
		long long k = i;
		dp[i][gx][gy] = k * k + a[gx][gy];
		priority_queue<P, vector<P>, greater<P>> q;
		q.push({ dp[i][gx][gy], {gx, gy} });
		while (!q.empty()) {
			auto tmp = q.top();
			q.pop();
			long long cost = tmp.first;
			int x = tmp.second.first;
			int y = tmp.second.second;
			if (cost > dp[i][x][y]) continue;
			rep(j, 4) {
				int nx = x + dx[j];
				int ny = y + dy[j];
				if ((nx < 0) || (nx >= h) || (ny < 0) || (ny >= w))continue;
				long long ncost = cost + k * k + a[nx][ny];
				if (chmin(dp[i][nx][ny], ncost))q.push({ ncost, { nx, ny } });
			}
		}
	}
	vector dp2(h, vector<long long>(w, LINF));
	{
		set<pair<int, int>>st;
		dp2[gx][gy] = a[gx][gy];
		st.insert({ gx,gy });
		while (!st.empty()) {
			set<pair<int, int>>st2;
			for (auto e : st) {
				int x = e.first;
				int y = e.second;
				rep(j, 4) {
					int nx = x + dx[j];
					int ny = y + dy[j];
					if ((nx < 0) || (nx >= h) || (ny < 0) || (ny >= w))continue;
					if (LINF == dp2[nx][ny])st2.insert({ nx, ny });
					if (!st2.count({ nx,ny })) continue;
					long long ncost = dp2[x][y] + a[nx][ny];
					chmin(dp2[nx][ny],ncost);
				}
			}
			swap(st2, st);
		}
	}
	int q; cin >> q;
	while (q--) {
		int x, y; cin >> x >> y;
		x--, y--;
		long long k; cin >> k;
		if (k > 250) {
			long long ans = dp2[x][y];
			long long d = abs(gx - x) + abs(gy - y) + 1;
			ans += k * k * d;
			cout << ans << endl;
		}
		else {
			cout << dp[k][x][y] << endl;
		}
	}
	return 0;
}
0