結果
| 問題 | 
                            No.867 避難経路
                             | 
                    
| コンテスト | |
| ユーザー | 
                             bal4u
                         | 
                    
| 提出日時 | 2019-08-17 07:34:22 | 
| 言語 | C  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,102 ms / 6,000 ms | 
| コード長 | 3,320 bytes | 
| コンパイル時間 | 1,046 ms | 
| コンパイル使用メモリ | 32,896 KB | 
| 実行使用メモリ | 34,784 KB | 
| 最終ジャッジ日時 | 2024-09-24 18:52:03 | 
| 合計ジャッジ時間 | 31,293 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 41 | 
コンパイルメッセージ
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');
      |                 ^~
            
            ソースコード
// 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), dis[sr][sc][k] = a[sr][sc]+k2;
		while (qsize) {
			r = que[0].r, c = que[0].c, t = que[0].t, deq();
			if (dis[r][c][k] < 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 < dis[rr][cc][k]) dis[rr][cc][k] = tt, enq(rr, cc, tt, 0);
			}
		}
	}
	qsize = 0, sw = 1;
	for (r = 1; r <= H; r++) for (c = 1; c <= W; c++) {
		dis[r][c][LIM+1] = ABS(r-sr) + ABS(c-sc) + 1;
	}
	enq(sr, sc, a[sr][sc], 1), dis[sr][sc][LIM] = a[sr][sc];
	while (qsize) {
		r = que[0].r, c = que[0].c, t = que[0].t, s = que[0].s, deq();
		if (dis[r][c][LIM+1] != s || dis[r][c][LIM] < 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][LIM+1] == s+1 && tt < dis[rr][cc][LIM]) {
				dis[rr][cc][LIM] = tt, 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 {
//			printf("dis %d, step %d ", dis[r][c][LIM], dis[r][c][LIM+1]);
			out(dis[r][c][LIM] + (ll)k*k*dis[r][c][LIM+1]);
		}
	}
	return 0;
}
            
            
            
        
            
bal4u