結果

問題 No.867 避難経路
ユーザー heno239heno239
提出日時 2019-08-16 23:13:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4,461 ms / 6,000 ms
コード長 3,886 bytes
コンパイル時間 2,213 ms
コンパイル使用メモリ 130,680 KB
実行使用メモリ 458,640 KB
最終ジャッジ日時 2023-10-24 04:47:31
合計ジャッジ時間 112,197 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
56,068 KB
testcase_01 AC 9 ms
10,716 KB
testcase_02 AC 9 ms
10,696 KB
testcase_03 AC 9 ms
10,732 KB
testcase_04 AC 9 ms
10,736 KB
testcase_05 AC 10 ms
10,696 KB
testcase_06 AC 9 ms
10,696 KB
testcase_07 AC 9 ms
10,732 KB
testcase_08 AC 9 ms
10,748 KB
testcase_09 AC 9 ms
10,736 KB
testcase_10 AC 10 ms
10,696 KB
testcase_11 AC 9 ms
10,696 KB
testcase_12 AC 9 ms
10,752 KB
testcase_13 AC 9 ms
10,732 KB
testcase_14 AC 10 ms
10,696 KB
testcase_15 AC 4,078 ms
450,544 KB
testcase_16 AC 3,827 ms
432,520 KB
testcase_17 AC 4,073 ms
458,640 KB
testcase_18 AC 3,871 ms
443,052 KB
testcase_19 AC 3,834 ms
431,720 KB
testcase_20 AC 3,937 ms
435,628 KB
testcase_21 AC 3,619 ms
405,308 KB
testcase_22 AC 3,793 ms
432,876 KB
testcase_23 AC 4,082 ms
457,184 KB
testcase_24 AC 3,911 ms
410,788 KB
testcase_25 AC 4,246 ms
437,052 KB
testcase_26 AC 4,275 ms
435,164 KB
testcase_27 AC 4,283 ms
446,720 KB
testcase_28 AC 4,218 ms
448,944 KB
testcase_29 AC 4,461 ms
451,612 KB
testcase_30 AC 3,334 ms
385,520 KB
testcase_31 AC 3,875 ms
423,140 KB
testcase_32 AC 3,741 ms
413,924 KB
testcase_33 AC 3,668 ms
407,476 KB
testcase_34 AC 3,395 ms
384,764 KB
testcase_35 AC 3,535 ms
384,568 KB
testcase_36 AC 3,610 ms
382,712 KB
testcase_37 AC 3,383 ms
376,328 KB
testcase_38 AC 3,451 ms
376,236 KB
testcase_39 AC 3,472 ms
376,220 KB
testcase_40 AC 3,382 ms
376,228 KB
testcase_41 AC 16 ms
69,228 KB
testcase_42 AC 17 ms
71,344 KB
testcase_43 AC 17 ms
71,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000009;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef vector<string> svec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;

struct CHT {
	vector<LP> v;
	vector<LP> memo;
	int l;
	bool check(LP a, LP b, LP c) {
		return (b.first - a.first)*(c.second - b.second) >= (b.second - a.second)*(c.first - b.first);
	}
	void build(vector<LP> a) {
		l = 0;
		per(i, (int)a.size()) {
			while (v.size() >= 2 && check(v[v.size() - 2], v[v.size() - 1], a[i]))v.pop_back();
			v.push_back(a[i]);
		}
	}
	void build() {
		per(i, (int)memo.size()) {
			while (v.size() >= 2 && check(v[v.size() - 2], v[v.size() - 1], memo[i]))v.pop_back();
			v.push_back(memo[i]);
		}
		memo.clear();
	}
	void add(LP a) {
		memo.push_back(a);
	}
	ll f(LP a, ll x) {
		return a.first*x + a.second;
	}
	ll query(ll x) {
		while (l + 1 < v.size() && f(v[l], x) > f(v[l + 1], x))l++;
		return f(v[l], x);
	}
};

const int sz = 505;

int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };

int a[250][250];
int dist[250][250][sz+1];

CHT cht[250][250];

struct query {
	int x, y, k, id;
};

int isans[250][250][250];

typedef pair<int, P> speP;
void solve() {
	int h, w; cin >> h >> w;
	int sx, sy = 0;
	cin >> sx >> sy; sx--; sy--;
	rep(i, h) {
		rep(j, w) {
			cin >> a[i][j];
			rep(k, sz + 1) {
				dist[i][j][k] = mod;
			}
		}
	}
	dist[sx][sy][1] = a[sx][sy];
	//cht[sx][sy].add({ 1,a[sx][sy] });
	Rep(k,2, sz + 1) {
		rep(i, h) {
			rep(j, w) {
				if (dist[i][j][k-1] == mod)continue;
				cht[i][j].add({ k-1,dist[i][j][k - 1] });
				rep(t, 4) {
					int x = i + dx[t];
					int y = j + dy[t];
					if (x < 0 || y < 0 || x >= h || y >= w)continue;
					dist[x][y][k] = min(dist[x][y][k], dist[i][j][k - 1] + a[x][y]);
				}
			}
		}
	}
	rep(i, h) {
		rep(j, w) {
			cht[i][j].build();
		}
	}

	for (int k = 1; k < 250; k++) {
		rep(i, h)rep(j, w)isans[k][i][j] = mod;
		isans[k][sx][sy] = k * k + a[sx][sy];
		priority_queue<speP, vector<speP>, greater<speP>> q;
		q.push({ isans[k][sx][sy], { sx,sy } });
		while (!q.empty()) {
			speP p = q.top(); q.pop();
			int x = p.second.first, y = p.second.second;
			int val = p.first;
			if (val > isans[k][x][y])continue;
			rep(j, 4) {
				int nx = x + dx[j],ny = y + dy[j];
				if (nx < 0 || ny < 0 || nx >= h || ny >= w)continue;
				int nd = val + k * k + a[nx][ny];
				if (nd < isans[k][nx][ny]) {
					isans[k][nx][ny] = nd;
					q.push({ nd,{nx,ny} });
				}
			}
		}
	}


	int q; cin >> q;
	vector<pair<P,P>> qmemo;
	rep(i, q) {
		int x, y, k;
		cin >> x >> y >> k; x--; y--;
		qmemo.push_back({ {k,i},{x,y} });
	}
	sort(qmemo.begin(), qmemo.end());
	vector<ll> ans(q);
	rep(i, q) {
		ll k = qmemo[i].first.first;
		int id = qmemo[i].first.second;
		int x = qmemo[i].second.first;
		int y = qmemo[i].second.second;
		ans[id] = cht[x][y].query(k*k);
		if (k < 250) {
			ans[id] = min(ans[id], (ll)isans[k][x][y]);
		}
	}
	rep(i, q) {
		cout << ans[i] << endl;
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init();
	solve();
	//cout << "finish" << endl;
	//stop
	return 0;
}
0