結果
| 問題 |
No.867 避難経路
|
| コンテスト | |
| ユーザー |
heno239
|
| 提出日時 | 2019-08-16 22:53:52 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 3,132 bytes |
| コンパイル時間 | 1,387 ms |
| コンパイル使用メモリ | 124,324 KB |
| 実行使用メモリ | 776,032 KB |
| 最終ジャッジ日時 | 2024-09-22 20:19:02 |
| 合計ジャッジ時間 | 123,518 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 15 MLE * 26 |
ソースコード
#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;
}
heno239