結果
| 問題 |
No.867 避難経路
|
| コンテスト | |
| ユーザー |
tempura_pp
|
| 提出日時 | 2019-08-17 00:11:45 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,523 ms / 6,000 ms |
| コード長 | 1,977 bytes |
| コンパイル時間 | 1,103 ms |
| コンパイル使用メモリ | 108,572 KB |
| 実行使用メモリ | 28,936 KB |
| 最終ジャッジ日時 | 2024-09-23 05:13:18 |
| 合計ジャッジ時間 | 37,442 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
struct query{
ll sx,sy,id,k;
};
int main(){
int h,w;
cin>>h>>w;
ll a[h][w];
int gx,gy;
cin>>gx>>gy;
--gx;--gy;
rep(i,h)rep(j,w)cin>>a[i][j];
int q;
cin>>q;
vector<query> v[161];
rep(i,q){
int x,y,k;
cin>>x>>y>>k;
--x;--y;
v[min(k,160)].push_back({x,y,i,k});
}
ll dist[300][300];
int di[]={1,0,-1,0,1};
vector<ll> ans(q);
rep(K,161){
if(v[K].empty())continue;
rep(i,h)rep(j,w){
dist[i][j]=longinf;
}
dist[gx][gy]=K*K+a[gx][gy];
using T =pair<ll,pair<int,int>>;
priority_queue<T,vector<T>, greater<T>> pq;
pq.push({dist[gx][gy],{gx,gy}});
while(pq.size()){
ll d = pq.top().first;
auto p = pq.top().second;
pq.pop();
int x = p.first, y = p.second;
if(dist[x][y]<d)continue;
rep(i,4){
int nx = x+di[i], ny=y+di[i+1];
if(nx<0||nx>=h||ny<0||ny>=w)continue;
if(dist[nx][ny]>d+a[nx][ny]+K*K){
dist[nx][ny]=d+a[nx][ny]+K*K;
pq.push({dist[nx][ny],{nx,ny}});
}
}
}
for(auto Q :v[K]){
ans[Q.id]=dist[Q.sx][Q.sy];
if(K!=Q.k){
ll d = abs(Q.sx-gx)+abs(Q.sy-gy)+1;
ans[Q.id]+=(Q.k*Q.k-K*K)*d;
}
}
}
for(auto e : ans)cout<<e<<"\n";
return 0;
}
tempura_pp