結果

問題 No.867 避難経路
ユーザー chocoruskchocorusk
提出日時 2019-08-16 22:59:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,839 ms / 6,000 ms
コード長 2,139 bytes
コンパイル時間 1,424 ms
コンパイル使用メモリ 109,456 KB
実行使用メモリ 426,252 KB
最終ジャッジ日時 2023-10-24 04:04:29
合計ジャッジ時間 82,278 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
98,816 KB
testcase_01 AC 31 ms
98,816 KB
testcase_02 AC 29 ms
98,816 KB
testcase_03 AC 31 ms
98,816 KB
testcase_04 AC 30 ms
98,816 KB
testcase_05 AC 32 ms
98,816 KB
testcase_06 AC 31 ms
98,816 KB
testcase_07 AC 30 ms
98,816 KB
testcase_08 AC 31 ms
98,816 KB
testcase_09 AC 32 ms
98,816 KB
testcase_10 AC 32 ms
98,816 KB
testcase_11 AC 31 ms
98,816 KB
testcase_12 AC 30 ms
98,816 KB
testcase_13 AC 31 ms
98,816 KB
testcase_14 AC 32 ms
98,816 KB
testcase_15 AC 3,458 ms
426,036 KB
testcase_16 AC 3,268 ms
426,248 KB
testcase_17 AC 2,960 ms
426,180 KB
testcase_18 AC 3,127 ms
426,252 KB
testcase_19 AC 3,256 ms
426,252 KB
testcase_20 AC 1,466 ms
426,252 KB
testcase_21 AC 1,444 ms
426,252 KB
testcase_22 AC 3,819 ms
426,252 KB
testcase_23 AC 3,613 ms
426,252 KB
testcase_24 AC 4,321 ms
426,252 KB
testcase_25 AC 4,674 ms
426,252 KB
testcase_26 AC 4,839 ms
426,252 KB
testcase_27 AC 4,519 ms
426,252 KB
testcase_28 AC 4,589 ms
426,236 KB
testcase_29 AC 4,453 ms
426,236 KB
testcase_30 AC 1,567 ms
426,236 KB
testcase_31 AC 1,567 ms
426,252 KB
testcase_32 AC 1,562 ms
426,252 KB
testcase_33 AC 1,553 ms
426,252 KB
testcase_34 AC 1,503 ms
425,248 KB
testcase_35 AC 1,509 ms
425,248 KB
testcase_36 AC 1,501 ms
423,116 KB
testcase_37 AC 1,456 ms
416,784 KB
testcase_38 AC 1,456 ms
416,768 KB
testcase_39 AC 1,444 ms
416,796 KB
testcase_40 AC 1,474 ms
416,800 KB
testcase_41 AC 40 ms
166,788 KB
testcase_42 AC 41 ms
167,956 KB
testcase_43 AC 41 ms
169,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#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;
}
0