結果

問題 No.867 避難経路
ユーザー bal4ubal4u
提出日時 2019-08-17 08:19:45
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 863 ms / 6,000 ms
コード長 3,112 bytes
コンパイル時間 250 ms
コンパイル使用メモリ 32,732 KB
実行使用メモリ 34,896 KB
最終ジャッジ日時 2023-10-24 23:53:00
合計ジャッジ時間 24,506 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
34,896 KB
testcase_01 AC 11 ms
34,896 KB
testcase_02 AC 11 ms
34,896 KB
testcase_03 AC 11 ms
34,896 KB
testcase_04 AC 10 ms
34,896 KB
testcase_05 AC 11 ms
34,896 KB
testcase_06 AC 11 ms
34,896 KB
testcase_07 AC 11 ms
34,896 KB
testcase_08 AC 10 ms
34,896 KB
testcase_09 AC 10 ms
34,896 KB
testcase_10 AC 11 ms
34,896 KB
testcase_11 AC 11 ms
34,896 KB
testcase_12 AC 11 ms
34,896 KB
testcase_13 AC 11 ms
34,896 KB
testcase_14 AC 11 ms
34,896 KB
testcase_15 AC 697 ms
34,896 KB
testcase_16 AC 651 ms
34,896 KB
testcase_17 AC 709 ms
34,896 KB
testcase_18 AC 660 ms
34,896 KB
testcase_19 AC 663 ms
34,896 KB
testcase_20 AC 615 ms
34,896 KB
testcase_21 AC 617 ms
34,896 KB
testcase_22 AC 648 ms
34,896 KB
testcase_23 AC 691 ms
34,896 KB
testcase_24 AC 636 ms
34,896 KB
testcase_25 AC 637 ms
34,896 KB
testcase_26 AC 643 ms
34,884 KB
testcase_27 AC 655 ms
34,896 KB
testcase_28 AC 855 ms
34,896 KB
testcase_29 AC 863 ms
34,896 KB
testcase_30 AC 522 ms
34,896 KB
testcase_31 AC 510 ms
34,896 KB
testcase_32 AC 518 ms
34,896 KB
testcase_33 AC 511 ms
34,896 KB
testcase_34 AC 522 ms
34,896 KB
testcase_35 AC 545 ms
34,896 KB
testcase_36 AC 533 ms
34,896 KB
testcase_37 AC 509 ms
34,896 KB
testcase_38 AC 505 ms
34,896 KB
testcase_39 AC 510 ms
34,896 KB
testcase_40 AC 501 ms
34,896 KB
testcase_41 AC 10 ms
34,896 KB
testcase_42 AC 11 ms
34,896 KB
testcase_43 AC 10 ms
34,896 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:11:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   11 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:19:24: note: in expansion of macro 'gc'
   19 |         int n = 0, c = gc();
      |                        ^~
main.c: In function 'out':
main.c:12:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
   12 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:28:17: note: in expansion of macro 'pc'
   28 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: No.867 避難経路
// 2019.8.17 bal4u

#include <stdio.h>
#include <string.h>

typedef long long ll;

//// 高速入力
#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif

int in() { // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void out(ll n) { // 非負整数の表示(出力)
	int i;
	char b[50];

	if (!n) pc('0');
	else {
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

//// 優先度付きキュー(ダイクストラ法用)
#define MAX 500000
typedef struct { int t, r, c; } QUE; // 移動のコスト、(r,c)
QUE que[MAX]; int qsize;

#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

void min_heapify(int i) {
	int l, r, min;
	l = LEFT(i), r = RIGHT(i);
	if (l < qsize && que[l].t < que[i].t) min = l; else min = i;
	if (r < qsize && que[r].t < que[min].t) min = r;
	if (min != i) {
		QUE qt = que[i]; que[i] = que[min]; que[min] = qt;
		min_heapify(min);
	}
}

void deq() {
	que[0] = que[--qsize];
	min_heapify(0);
}

void enq(int r, int c, int t) {
	int i, min;
	i = qsize++;
	que[i].r = r, que[i].c = c, que[i].t = t;
	while (i > 0 && que[i].t < que[min = PARENT(i)].t) {
		QUE qt = que[i]; que[i] = que[min]; que[min] = qt;
		i = min;
	}
}

//// 本問題関連
#define ABS(x) ((x)>=0?(x):-(x))
#define LIM 120
int H, W;
int a[256][256];
int cost[256][256], dis[256][256]; 
int mv[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};
int ans[LIM+3][256][256];

void dijkstra(int sr, int sc) {
	int i, r, c, t, k, k2, rr, cc, tt;

	for (k = 1; k < LIM; k++) {	k2 = k*k;
		memset(cost, 0x33, sizeof(cost));
		qsize = 0, enq(sr, sc, a[sr][sc]+k2), cost[sr][sc] = a[sr][sc]+k2;
		while (qsize) {
			r = que[0].r, c = que[0].c, t = que[0].t, deq();
			if (cost[r][c] < t) continue;
			for (i = 0; i < 4; i++) {
				rr = r + mv[i][0], cc = c + mv[i][1];
				if (rr == 0 || rr > H || cc == 0 || cc > W) continue;
				tt = t + a[rr][cc] + k2;
				if (tt < cost[rr][cc]) cost[rr][cc] = tt, enq(rr, cc, tt);
			}
		}
		memcpy(ans[k], cost, sizeof(cost));
	}

	qsize = 0;
	memset(cost, 0x33, sizeof(cost));
	for (r = 1; r <= H; r++) for (c = 1; c <= W; c++) dis[r][c] = ABS(r-sr)+ABS(c-sc)+1;
	enq(sr, sc, a[sr][sc]), cost[sr][sc] = a[sr][sc];
	while (qsize) {
		r = que[0].r, c = que[0].c, t = que[0].t, deq();
		if (cost[r][c] < t) continue;
		for (i = 0; i < 4; i++) {
			rr = r + mv[i][0], cc = c + mv[i][1];
			if (rr == 0 || rr > H || cc == 0 || cc > W) continue;
			tt = t + a[rr][cc];
			if (dis[rr][cc] == dis[r][c]+1 && tt < cost[rr][cc]) {
				cost[rr][cc] = tt, enq(rr, cc, tt);
			}
		}
	}
	memcpy(ans[LIM], cost, sizeof(cost));
}

int main()
{
	int k, r, c, gr, gc, Q;

	H = in(), W = in(), gr = in(), gc = in();
	for (r = 1; r <= H; r++) for (c = 1; c <= W; c++) a[r][c] = in();
	dijkstra(gr, gc);
	Q = in(); while (Q--) {
		r = in(), c = in(), k = in();
		if (k < LIM) out(ans[k][r][c]);
		else out(ans[LIM][r][c] + (ll)k*k*dis[r][c]);
	}
	return 0;
}
0