結果

問題 No.867 避難経路
ユーザー heno239heno239
提出日時 2019-08-16 22:53:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,132 bytes
コンパイル時間 1,482 ms
コンパイル使用メモリ 124,996 KB
実行使用メモリ 775,936 KB
最終ジャッジ日時 2023-10-24 03:19:35
合計ジャッジ時間 148,506 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
17,204 KB
testcase_01 AC 8 ms
17,208 KB
testcase_02 AC 7 ms
16,972 KB
testcase_03 AC 7 ms
17,200 KB
testcase_04 AC 8 ms
17,172 KB
testcase_05 AC 7 ms
16,972 KB
testcase_06 AC 7 ms
17,164 KB
testcase_07 AC 7 ms
17,200 KB
testcase_08 AC 7 ms
17,200 KB
testcase_09 AC 8 ms
17,204 KB
testcase_10 AC 8 ms
16,972 KB
testcase_11 AC 7 ms
17,208 KB
testcase_12 AC 8 ms
17,208 KB
testcase_13 AC 8 ms
17,200 KB
testcase_14 AC 8 ms
17,196 KB
testcase_15 TLE -
testcase_16 MLE -
testcase_17 MLE -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 MLE -
testcase_21 MLE -
testcase_22 MLE -
testcase_23 MLE -
testcase_24 MLE -
testcase_25 MLE -
testcase_26 MLE -
testcase_27 MLE -
testcase_28 MLE -
testcase_29 MLE -
testcase_30 MLE -
testcase_31 MLE -
testcase_32 MLE -
testcase_33 MLE -
testcase_34 MLE -
testcase_35 MLE -
testcase_36 MLE -
testcase_37 MLE -
testcase_38 MLE -
testcase_39 MLE -
testcase_40 MLE -
testcase_41 AC 5 ms
10,556 KB
testcase_42 AC 7 ms
14,756 KB
testcase_43 AC 7 ms
14,756 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 = 1000;

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;
};

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();
		}
	}
	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);
	}
	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