結果
| 問題 |
No.867 避難経路
|
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-08-17 08:19:45 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 842 ms / 6,000 ms |
| コード長 | 3,112 bytes |
| コンパイル時間 | 532 ms |
| コンパイル使用メモリ | 32,512 KB |
| 実行使用メモリ | 34,980 KB |
| 最終ジャッジ日時 | 2024-09-24 18:54:51 |
| 合計ジャッジ時間 | 24,192 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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 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;
}
bal4u