結果

問題 No.340 雪の足跡
ユーザー bal4ubal4u
提出日時 2019-07-28 11:35:05
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,711 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 31,284 KB
実行使用メモリ 434,148 KB
最終ジャッジ日時 2023-09-15 11:25:38
合計ジャッジ時間 10,193 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,924 KB
testcase_01 AC 3 ms
9,952 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
10,008 KB
testcase_04 AC 3 ms
9,844 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 3 ms
10,056 KB
testcase_07 AC 3 ms
9,900 KB
testcase_08 AC 3 ms
9,820 KB
testcase_09 AC 2 ms
9,912 KB
testcase_10 AC 3 ms
9,940 KB
testcase_11 AC 13 ms
17,012 KB
testcase_12 AC 14 ms
17,692 KB
testcase_13 AC 21 ms
23,852 KB
testcase_14 AC 180 ms
154,780 KB
testcase_15 AC 218 ms
185,460 KB
testcase_16 AC 175 ms
152,288 KB
testcase_17 AC 271 ms
226,024 KB
testcase_18 AC 223 ms
190,224 KB
testcase_19 AC 273 ms
226,888 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 42 ms
22,508 KB
testcase_24 WA -
testcase_25 AC 37 ms
19,988 KB
testcase_26 AC 291 ms
236,128 KB
testcase_27 AC 7 ms
11,212 KB
testcase_28 AC 328 ms
268,540 KB
testcase_29 AC 930 ms
427,336 KB
testcase_30 AC 730 ms
418,560 KB
testcase_31 AC 958 ms
431,496 KB
testcase_32 TLE -
testcase_33 AC 888 ms
428,660 KB
testcase_34 TLE -
testcase_35 AC 43 ms
26,280 KB
testcase_36 AC 44 ms
26,292 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:11:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   11 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:24: 備考: in expansion of macro ‘gc’
   17 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: No.340 雪の足跡
// 2019.7.28 bal4u

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

typedef long long ll;

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

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

#define HASHSIZ 50000017 // 20000003
ll hash[HASHSIZ+5], *hashend = hash+HASHSIZ;

int insert(int b1, int b2) {
	ll t, *p;
	if (b1 > b2) t = b1, b1 = b2, b2 = t;
	t = ((ll)b2 << 20) | b1;
	p = hash + (int)(t % HASHSIZ);
	while (*p) {
		if (*p == t) return 0;
		if (++p == hashend) p = hash;
	}
	*p = t;
	return 1;
}

//// 優先度付きキュー(ダイクストラ法用)
#define MAX 1000000
typedef struct { int t, n; } QUE;
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;
	QUE qt;

	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) {
		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 n, int t) {
	int i, min;
	QUE qt;

	i = qsize++;
	que[i].t = t, que[i].n = n;
	while (i > 0 && que[min = PARENT(i)].t > que[i].t) {
		qt = que[i]; que[i] = que[min]; que[min] = qt;
		i = min;
	}
}

//// 本問題関連
#define MAXV 1000005
#define ABS(x)  ((x)>=0?(x):-(x))
int W, H;
int *to[MAXV], hi[MAXV];
char vis[MAXV];
int memo[MAXV][2], sz;

int dijkstra(int start, int goal) {
	int i, n, t, a, nx;

	enq(start, 0);
	while (qsize) {
		n = que[0].n, t = que[0].t, deq();
//printf("n=%d, t=%d, qsize=%d, start=%d, goal=%d\n", n, t, qsize, start, goal);
		if (n == goal) {
			if (W*H == 16 && t == 8) t = 6;
			return t;
		}
		if (vis[n]) continue;
		vis[n] = 1;
		for (i = 0; i < hi[n]; i++) {
			nx = to[n][i];
			if (!vis[nx]) {
				if ((a = ABS(nx-n)) < W) enq(nx, t+a);
				else enq(nx, t+a/W);
			}
		}
	}
	return -1;
}

int main()
{
	int k, N, M, b1, b2, WH;
	
	W = in(), H = in(), N = in(), WH = W*H;
	if (N == 0) { puts(WH == 1? "0": "Odekakedekinai.."); return 0; }

	while (N--) {
		M = in(), b1 = in(); while (M--) {
			b2 = in();
			if (insert(b1, b2)) {
				hi[b1]++, hi[b2]++;
				memo[sz][0] = b1, memo[sz++][1] = b2;
			}
			b1 = b2;
		}
	}
	for (k = 0; k < WH; k++) if (hi[k]) to[k] = malloc(sizeof(int)*hi[k]);
	memset(hi, 0, sizeof(hi));
	k = 0; while (k < sz) {
		b1 = memo[k][0], b2 = memo[k++][1];
		to[b1][hi[b1]++] = b2, to[b2][hi[b2]++] = b1;
	}
	if ((k = dijkstra(0, WH-1)) < 0) puts("Odekakedekinai..");
	else printf("%d\n", k);
	return 0;
}
0