結果

問題 No.867 避難経路
ユーザー FF256grhyFF256grhy
提出日時 2019-11-19 02:36:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 3,354 bytes
コンパイル時間 2,342 ms
コンパイル使用メモリ 175,824 KB
実行使用メモリ 753,212 KB
最終ジャッジ日時 2024-04-14 08:30:09
合計ジャッジ時間 20,642 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 200 ms
240,708 KB
testcase_01 AC 207 ms
238,912 KB
testcase_02 AC 159 ms
239,172 KB
testcase_03 AC 112 ms
239,556 KB
testcase_04 AC 94 ms
238,792 KB
testcase_05 AC 92 ms
239,812 KB
testcase_06 AC 94 ms
239,300 KB
testcase_07 AC 93 ms
239,680 KB
testcase_08 AC 93 ms
239,936 KB
testcase_09 AC 93 ms
239,044 KB
testcase_10 AC 92 ms
238,788 KB
testcase_11 AC 92 ms
239,424 KB
testcase_12 AC 94 ms
239,556 KB
testcase_13 AC 93 ms
239,172 KB
testcase_14 AC 93 ms
239,044 KB
testcase_15 MLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long   signed int LL;
typedef long long unsigned int LU;
#define incID(i, l, r) for(LL i = (l)    ; i <  (r); ++i)
#define incII(i, l, r) for(LL i = (l)    ; i <= (r); ++i)
#define decID(i, l, r) for(LL i = (r) - 1; i >= (l); --i)
#define decII(i, l, r) for(LL i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n)  decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
template<typename T> bool setmin  (T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmax  (T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
LL mo(LL a, LL b) { assert(b > 0); a %= b; if(a < 0) { a += b; } return a; }
LL fl(LL a, LL b) { assert(b > 0); return (a > 0 ? a / b : (a - b + 1) / b); }
LL ce(LL a, LL b) { assert(b > 0); return (a < 0 ? a / b : (a + b - 1) / b); }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define bit(b, i) (((b) >> (i)) & 1)
#define BC __builtin_popcountll
#define SC static_cast
#define SI(v) SC<int>(v.size())
#define SL(v) SC<LL >(v.size())
#define RF(e, v) for(auto & e: v)
#define ef else if
#define UR assert(false)

// ---- ----

template<typename T> class RPQ : public priority_queue<T, vector<T>, greater<T>> { };
template<typename C> void dijkstra(int s, vector<pair<int, C>> g[], C d[]) {
	RPQ<pair<C, int>> q;
	d[s] = 0;
	q.emplace(d[s], s);
	while(! q.empty()) {
		C   c = q.top().FI;
		int v = q.top().SE;
		q.pop();
		if(d[v] != c) { continue; }
		for(auto & e : g[v]) {
			int ev = e.FI;
			C   ec = e.SE;
			if(setmin(d[ev], d[v] + ec)) { q.emplace(d[ev], ev); }
		}
	}
}

const int M = 250;
const int L = 159;

vector<pair<int, int>> g[(L + 1) * M * M];
int d[(L + 1) * M * M], INF = 1e9;

int main() {
	int h, w, gi, gj;
	cin >> h >> w >> gi >> gj;
	gi--; gj--;
	vector<vector<int>> a(h, vector<int>(w));
	inc(i, h) {
	inc(j, w) {
		cin >> a[i][j];
	}
	}
	
	auto id = [&](int i, int j, int k) {
		return min(k, L) * h * w + i * w + j;
	};
	
	incII(kk, 0, L) {
		g[id(gi, gj, 0)].EB(id(gi, gj, kk), a[gi][gj] + kk * kk);
		inc(i, h) {
		inc(j, w) {
			d[id(i, j, kk)] = INF;
			inc(l, 4) {
				int di[9] = { +1,  0, -1,  0, +1, +1, -1, -1,  0 };
				int dj[9] = {  0, +1,  0, -1, +1, -1, +1, -1,  0 };
				int ii = i + di[l];
				int jj = j + dj[l];
				if(! (inID(ii, 0, h) && inID(jj, 0, w))) { continue; }
				g[id(i, j, kk)].EB(id(ii, jj, kk), a[ii][jj] + kk * kk);
			}
		}
		}
	}
	
	dijkstra(id(gi, gj, 0), g, d);
	
	int q;
	cin >> q;
	inc(qq, q) {
		LL i, j, k;
		cin >> i >> j >> k;
		i--; j--;
		
		LL ans = d[id(i, j, k)];
		if(k > L) { ans += (abs(gi - i) + abs(gj - j) + 1) * (k * k - L * L); }
		
		cout << ans << "\n";
	}
	
	return 0;
}
0