結果

問題 No.340 雪の足跡
ユーザー bal4ubal4u
提出日時 2019-07-28 10:46:32
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 2,661 bytes
コンパイル時間 711 ms
コンパイル使用メモリ 30,728 KB
実行使用メモリ 322,064 KB
最終ジャッジ日時 2023-09-15 11:25:22
合計ジャッジ時間 12,608 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
161,460 KB
testcase_01 AC 39 ms
161,584 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 40 ms
161,496 KB
testcase_04 AC 41 ms
161,468 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 41 ms
161,572 KB
testcase_07 AC 40 ms
161,460 KB
testcase_08 AC 40 ms
161,400 KB
testcase_09 AC 42 ms
161,484 KB
testcase_10 AC 40 ms
161,424 KB
testcase_11 AC 48 ms
168,324 KB
testcase_12 AC 49 ms
169,124 KB
testcase_13 AC 56 ms
175,308 KB
testcase_14 AC 195 ms
267,904 KB
testcase_15 AC 222 ms
281,428 KB
testcase_16 AC 181 ms
267,600 KB
testcase_17 AC 250 ms
295,700 KB
testcase_18 AC 226 ms
283,308 KB
testcase_19 AC 251 ms
296,036 KB
testcase_20 AC 89 ms
163,320 KB
testcase_21 AC 52 ms
161,496 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 89 ms
168,412 KB
testcase_24 AC 62 ms
162,572 KB
testcase_25 AC 87 ms
165,780 KB
testcase_26 AC 234 ms
298,828 KB
testcase_27 AC 43 ms
162,652 KB
testcase_28 AC 266 ms
308,632 KB
testcase_29 TLE -
testcase_30 AC 701 ms
317,108 KB
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 AC 272 ms
172,312 KB
testcase_36 AC 274 ms
172,252 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 20000003
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)b2 << 20) | b1;
	p = hash + (int)(t % HASHSIZ);
	while (*p) {
		if (*p == t) return 1;
		if (++p == hashend) p = hash;
	}
	*p = t;
	return 0;
}

//// 優先度付きキュー(ダイクストラ法用)
#define MAX 100000
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);
}

int qmax;
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 9999991
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