結果

問題 No.867 避難経路
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2019-08-18 10:04:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,572 bytes
コンパイル時間 2,366 ms
コンパイル使用メモリ 216,296 KB
実行使用メモリ 55,784 KB
最終ジャッジ日時 2024-04-08 22:52:25
合計ジャッジ時間 80,917 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
34,412 KB
testcase_01 AC 14 ms
34,412 KB
testcase_02 AC 13 ms
34,412 KB
testcase_03 AC 14 ms
34,412 KB
testcase_04 AC 14 ms
34,412 KB
testcase_05 AC 14 ms
34,412 KB
testcase_06 AC 14 ms
34,412 KB
testcase_07 AC 15 ms
34,412 KB
testcase_08 AC 14 ms
34,412 KB
testcase_09 AC 13 ms
34,412 KB
testcase_10 AC 14 ms
34,412 KB
testcase_11 AC 13 ms
34,412 KB
testcase_12 AC 14 ms
34,412 KB
testcase_13 AC 14 ms
34,412 KB
testcase_14 AC 14 ms
34,412 KB
testcase_15 AC 949 ms
42,792 KB
testcase_16 AC 902 ms
42,792 KB
testcase_17 AC 970 ms
42,848 KB
testcase_18 AC 915 ms
42,904 KB
testcase_19 AC 903 ms
42,824 KB
testcase_20 AC 254 ms
41,868 KB
testcase_21 AC 256 ms
42,244 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 13 ms
34,404 KB
testcase_42 AC 13 ms
34,404 KB
testcase_43 AC 14 ms
34,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
int dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














int H, W, gx, gy, A[251][251], Q, X[501010], Y[501010], K[501010];
ll ans[1010101];
vector<int> query[1010101];
int threshold = 1000;
//---------------------------------------------------------------------------------------------------
ll dst[251][251]; bool vis[251][251]; int idx[251][251];
void _main() {
	cin >> H >> W >> gy >> gx; gx--; gy--;
	rep(y, 0, H) rep(x, 0, W) cin >> A[y][x];
	cin >> Q;
	rep(i, 0, Q) cin >> Y[i] >> X[i] >> K[i], X[i]--, Y[i]--;

	rep(q, 0, Q) query[K[q]].push_back(q);

	// small pattern
	rep(k, 1, threshold) {
		if (query[k].size() == 0) continue;

		rep(y, 0, H) rep(x, 0, W) dst[y][x] = infl, vis[y][x] = false;
		min_priority_queue<pair<ll, int>> que;

		dst[gy][gx] = k * k + A[gy][gx];
		que.push({ dst[gy][gx], gy * W + gx });

		while (!que.empty()) {
			auto q = que.top(); que.pop();

			ll cst = q.first;
			int y = q.second / W;
			int x = q.second % W;

			if (vis[y][x]) continue;
			vis[y][x] = true;

			rep(d, 0, 4) {
				int xx = x + dx[d];
				int yy = y + dy[d];
				if (0 <= xx and xx < W and 0 <= yy and yy < H) {
					if (chmin(dst[yy][xx], cst + A[yy][xx] + k * k)) {
						que.push({ dst[yy][xx] , yy * W + xx});
					}
				}
			}
		}

		fore(q, query[k]) ans[q] = dst[Y[q]][X[q]];
	}

	queue<pair<int, int>> que;
	rep(y, 0, H) rep(x, 0, W) dst[y][x] = infl, vis[y][x] = false;

	que.push({ gx, gy });
	dst[gy][gx] = A[gy][gx]; vis[gy][gx] = true; idx[gy][gx] = 0;

	while (!que.empty()) {
		int x, y;
		tie(x, y) = que.front(); que.pop();

		rep(d, 0, 4) {
			int xx = x + dx[d];
			int yy = y + dy[d];
			if (0 <= xx and xx < W and 0 <= yy and yy < H) {
				if (!vis[yy][xx]) {
					que.push({ xx, yy });
					vis[yy][xx] = true;
					idx[yy][xx] = idx[gy][gx] + 1;
				}
				if(idx[y][x] + 1 == idx[yy][xx]) chmin(dst[yy][xx], dst[y][x] + A[yy][xx]);
			}
		}
	}

	// big pattern
	rep(k, threshold, 1010101) fore(q, query[k]) {
		int x = X[q], y = Y[q];
		ans[q] = dst[y][x] + 1LL * (abs(y - gy) + abs(x - gx) + 1) * k * k;
	}

	rep(i, 0, Q) printf("%lld\n", ans[i]);
}



















0