結果

問題 No.867 避難経路
ユーザー bal4ubal4u
提出日時 2019-08-17 07:03:41
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 3,157 bytes
コンパイル時間 556 ms
コンパイル使用メモリ 32,800 KB
実行使用メモリ 49,580 KB
最終ジャッジ日時 2023-10-24 23:47:39
合計ジャッジ時間 58,209 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
35,144 KB
testcase_01 AC 10 ms
35,144 KB
testcase_02 AC 11 ms
35,144 KB
testcase_03 AC 10 ms
35,144 KB
testcase_04 AC 10 ms
35,144 KB
testcase_05 AC 10 ms
35,144 KB
testcase_06 AC 11 ms
35,144 KB
testcase_07 AC 10 ms
35,144 KB
testcase_08 AC 11 ms
35,144 KB
testcase_09 AC 10 ms
35,144 KB
testcase_10 AC 10 ms
35,144 KB
testcase_11 AC 10 ms
35,144 KB
testcase_12 AC 10 ms
35,144 KB
testcase_13 AC 10 ms
35,144 KB
testcase_14 AC 11 ms
35,144 KB
testcase_15 AC 1,998 ms
35,144 KB
testcase_16 AC 3,193 ms
35,144 KB
testcase_17 AC 2,528 ms
35,144 KB
testcase_18 AC 2,028 ms
35,144 KB
testcase_19 AC 3,017 ms
35,144 KB
testcase_20 AC 2,728 ms
35,144 KB
testcase_21 AC 2,254 ms
35,144 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 RE -
testcase_29 RE -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 TLE -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 1000000
typedef struct { int t, s, 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)

int sw;
void min_heapify(int i) {
	int l, r, min;

	l = LEFT(i), r = RIGHT(i);
	if (l < qsize && (que[l].t < que[i].t ||
		sw && que[l].t == que[i].t && que[l].s < que[i].s)) 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 s) {
	int i, min;

	i = qsize++;
	que[i].r = r, que[i].c = c, que[i].t = t, que[i].s = s;
	while (i > 0 && (que[i].t < que[min = PARENT(i)].t ||
		sw && que[i].t == que[min].t && que[i].s < que[min].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 dis[256][256][LIM+5];
int mv[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};

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

	memset(dis, 0x33, sizeof(dis));
	for (k = 1; k < LIM; k++) {
		k2 = k*k;
		qsize = 0, enq(sr, sc, a[sr][sc]+k2, 0);
		while (qsize) {
			r = que[0].r, c = que[0].c, t = que[0].t, deq();
			if (dis[r][c][k] < t) continue;
			dis[r][c][k] = t;
			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 < dis[rr][cc][k]) enq(rr, cc, tt, 0);
			}
		}
	}

	qsize = 0, sw = 1, enq(sr, sc, a[sr][sc], 1);
	while (qsize) {
		r = que[0].r, c = que[0].c, t = que[0].t, s = que[0].s, deq();
			if (dis[r][c][LIM] < t || dis[r][c][LIM] == t && dis[r][c][LIM+1] < s) continue;
			dis[r][c][LIM] = t, dis[r][c][LIM+1] = s;
			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 (tt < dis[rr][cc][LIM] ||
					tt == dis[rr][cc][LIM] && s+1 < dis[rr][cc][LIM+1]) enq(rr, cc, tt, s+1);
		}
	}
}

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(dis[r][c][k]);
		else out(dis[r][c][120] + (ll)k*k*((ABS(r-gr)+ABS(c-gc))+1));
	}
	return 0;
}
0