結果

問題 No.867 避難経路
ユーザー betrue12betrue12
提出日時 2019-08-16 23:12:57
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3,448 ms / 6,000 ms
コード長 2,595 bytes
コンパイル時間 1,916 ms
コンパイル使用メモリ 186,328 KB
実行使用メモリ 78,720 KB
最終ジャッジ日時 2024-09-22 21:57:43
合計ジャッジ時間 59,239 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
using namespace std;
int H, W, gx, gy;
bool inside(int i, int j){
return i>=0 && i<H && j>=0 && j<W;
}
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int main(){
cin >> H >> W >> gx >> gy;
gx--; gy--;
int A[250][250];
for(int i=0; i<H; i++) for(int j=0; j<W; j++) cin >> A[i][j];
int64_t mins[250][250];
const int64_t OFFSET = 1e12;
for(int i=0; i<H; i++) for(int j=0; j<W; j++) mins[i][j] = 1e18;
mins[gx][gy] = OFFSET + A[gx][gy];
typedef pair<int64_t, pair<int, int>> PP;
priority_queue<PP, vector<PP>, greater<PP>> que;
que.push({mins[gx][gy], {gx, gy}});
while(que.size()){
auto p = que.top(); que.pop();
int64_t d = p.first;
int i = p.second.first, j = p.second.second;
if(d > mins[i][j]) continue;
for(int k=0; k<4; k++){
int x = i+dx[k], y = j+dy[k];
if(inside(x, y) && mins[x][y] > d + OFFSET + A[x][y]){
mins[x][y] = d + OFFSET + A[x][y];
que.push({mins[x][y], {x, y}});
}
}
}
int Q;
cin >> Q;
map<int, vector<vector<int>>> qs;
for(int i=0; i<Q; i++){
int x, y, k;
cin >> x >> y >> k;
qs[k].push_back({i, x-1, y-1});
}
vector<int64_t> ans(Q);
int64_t dist[250][250];
for(auto& q : qs){
int64_t K = q.first;
int64_t K2 = K*K;
if(K <= 500){
for(int i=0; i<H; i++) for(int j=0; j<W; j++) dist[i][j] = 1e18;
dist[gx][gy] = K2 + A[gx][gy];
priority_queue<PP, vector<PP>, greater<PP>> que;
que.push({dist[gx][gy], {gx, gy}});
while(que.size()){
auto p = que.top(); que.pop();
int64_t d = p.first;
int i = p.second.first, j = p.second.second;
if(d > dist[i][j]) continue;
for(int k=0; k<4; k++){
int x = i+dx[k], y = j+dy[k];
if(inside(x, y) && dist[x][y] > d + K2 + A[x][y]){
dist[x][y] = d + K2 + A[x][y];
que.push({dist[x][y], {x, y}});
}
}
}
for(auto& v : q.second){
ans[v[0]] = dist[v[1]][v[2]];
}
}else{
for(auto& v : q.second){
int i = v[1], j = v[2];
ans[v[0]] = mins[i][j] - (OFFSET - K2) * (abs(i-gx) + abs(j-gy) + 1);
}
}
}
for(int i=0; i<Q; i++) cout << ans[i] << "\n";
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0