結果

問題 No.340 雪の足跡
ユーザー bal4ubal4u
提出日時 2019-07-28 10:21:30
言語 C
(gcc 12.3.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,652 bytes
コンパイル時間 504 ms
コンパイル使用メモリ 30,972 KB
実行使用メモリ 63,336 KB
最終ジャッジ日時 2023-09-15 11:24:31
合計ジャッジ時間 9,027 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
19,284 KB
testcase_01 AC 6 ms
17,508 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 6 ms
19,176 KB
testcase_04 AC 6 ms
19,092 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 6 ms
19,180 KB
testcase_07 AC 6 ms
19,112 KB
testcase_08 AC 6 ms
19,088 KB
testcase_09 AC 5 ms
19,280 KB
testcase_10 AC 6 ms
19,128 KB
testcase_11 AC 14 ms
26,184 KB
testcase_12 AC 14 ms
26,628 KB
testcase_13 AC 21 ms
31,328 KB
testcase_14 AC 93 ms
58,060 KB
testcase_15 AC 88 ms
58,240 KB
testcase_16 AC 71 ms
57,892 KB
testcase_17 AC 100 ms
58,344 KB
testcase_18 AC 93 ms
58,300 KB
testcase_19 AC 98 ms
58,368 KB
testcase_20 AC 56 ms
21,068 KB
testcase_21 AC 18 ms
19,224 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 55 ms
26,120 KB
testcase_24 AC 29 ms
20,288 KB
testcase_25 AC 52 ms
23,440 KB
testcase_26 AC 81 ms
58,344 KB
testcase_27 AC 8 ms
20,400 KB
testcase_28 AC 94 ms
58,272 KB
testcase_29 AC 923 ms
58,796 KB
testcase_30 AC 501 ms
58,708 KB
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 AC 235 ms
30,504 KB
testcase_36 AC 230 ms
30,508 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:10:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:16:24: 備考: in expansion of macro ‘gc’
   16 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

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

#include <stdio.h>
#include <string.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 5000011
long long hash[HASHSIZ+5], *hashend = hash+HASHSIZ;

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

//// 優先度付きキュー(ダイクストラ法用)
#define MAX 10000000
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
int W, H;
int to[MAXV][4];
char vis[MAXV];

int dijkstra(int start, int goal) {
	int i, n, t, 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) return t;
		if (vis[n]) continue;
		vis[n] = 1;
		for (i = 0; i < 4; i++) if ((nx = to[n][i]) >= 0) {
			if (!vis[nx]) enq(nx, t+1);
		}
	}
	return -1;
}

void setup(int b1, int b2) {
	if (insert(b1, b2)) return;
	if (b1 < b2) {
		if (b2-b1 < W) {
			while (b1 < b2) to[b1][1] = b1+1, to[b1+1][3] = b1, b1++;
		} else {
			while (b1 < b2) to[b1][0] = b1+W, to[b1+W][2] = b1, b1+=W;
		}
	} else {
		if (b1-b2 < W) {
			while (b2 < b1) to[b2][1] = b2+1, to[b2+1][3] = b2, b2++;
		} else {
			while (b2 < b1) to[b2][0] = b2+W, to[b2+W][2] = b2, b2+=W;
		}
	}
}

int main()
{
	int k, N, M, b1, b2;

	W = in(), H = in(), N = in();
	if (N == 0) { puts(W*H == 1? "0": "Odekakedekinai.."); return 0; }
	memset(to, -1, sizeof(to));
	while (N--) {
		M = in(), b1 = in(); while (M--) {
			b2 = in(), setup(b1, b2), b1 = b2;
		}
	}
	if ((k = dijkstra(0, W*H-1)) < 0) puts("Odekakedekinai..");
	else printf("%d\n", k);
	return 0;
}
0