結果
| 問題 | 
                            No.867 避難経路
                             | 
                    
| コンテスト | |
| ユーザー | 
                             chocorusk
                         | 
                    
| 提出日時 | 2019-09-28 11:22:06 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,652 ms / 6,000 ms | 
| コード長 | 2,138 bytes | 
| コンパイル時間 | 1,252 ms | 
| コンパイル使用メモリ | 107,788 KB | 
| 実行使用メモリ | 302,132 KB | 
| 最終ジャッジ日時 | 2024-10-01 19:54:39 | 
| 合計ジャッジ時間 | 46,504 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| 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[551][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<=550; 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<550; 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+50; j++){
				ans=min(ans, dp[j][x][y]+k*k*(j+1));
			}
			printf("%lld\n", ans);
		}
	}
	return 0;
}
            
            
            
        
            
chocorusk