結果
問題 | No.867 避難経路 |
ユーザー |
![]() |
提出日時 | 2019-08-16 22:59:33 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 3,501 ms / 6,000 ms |
コード長 | 2,139 bytes |
コンパイル時間 | 1,134 ms |
コンパイル使用メモリ | 108,100 KB |
実行使用メモリ | 426,248 KB |
最終ジャッジ日時 | 2024-09-22 21:03:25 |
合計ジャッジ時間 | 66,087 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:85:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 85 | scanf("%d %d %lld", &x, &y, &k); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<int, int> P; typedef pair<ll, P> Pl; const ll INF=1e18; int h, w; int gx, gy; int a[252][252]; ll dp[801][252][252]; ll d[51][252][252]; int dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1}; int main() { cin>>h>>w; cin>>gx>>gy; gx--; gy--; for(int i=0; i<h; i++){ for(int j=0; j<w; j++){ cin>>a[i][j]; } } for(int i=0; i<=800; i++){ for(int j=0; j<h; j++){ for(int k=0; k<w; k++){ dp[i][j][k]=INF; } } } dp[0][gx][gy]=a[gx][gy]; for(int i=0; i<800; i++){ for(int j=0; j<h; j++){ for(int k=0; k<w; k++){ for(int l=0; l<4; l++){ int x1=j+dx[l], y1=k+dy[l]; if(x1<0 || x1>=h || y1<0 || y1>=w) continue; dp[i+1][x1][y1]=min(dp[i][j][k]+a[x1][y1], dp[i+1][x1][y1]); } } } } for(int k=1; k<=50; k++){ for(int i=0; i<h; i++){ for(int j=0; j<w; j++) d[k][i][j]=INF; } d[k][gx][gy]=k*k+a[gx][gy]; priority_queue<Pl, vector<Pl>, greater<Pl>> que; que.push({d[k][gx][gy], {gx, gy}}); while(!que.empty()){ Pl p=que.top(); que.pop(); int x=p.second.first, y=p.second.second; if(d[k][x][y]<p.first) continue; for(int l=0; l<4; l++){ int x1=x+dx[l], y1=y+dy[l]; if(x1<0 || x1>=h || y1<0 || y1>=w) continue; if(d[k][x1][y1]>d[k][x][y]+k*k+a[x1][y1]){ d[k][x1][y1]=d[k][x][y]+k*k+a[x1][y1]; que.push({d[k][x1][y1], {x1, y1}}); } } } } int q; cin>>q; for(int i=0; i<q; i++){ int x, y; ll k; scanf("%d %d %lld", &x, &y, &k); x--; y--; if(k<=50){ printf("%lld\n", d[k][x][y]); }else{ int d0=abs(x-gx)+abs(y-gy); ll ans=INF; for(ll j=d0; j<=d0+300; j++){ ans=min(ans, dp[j][x][y]+k*k*(j+1)); } printf("%lld\n", ans); } } return 0; }