結果

問題 No.17 2つの地点に泊まりたい
ユーザー bal4ubal4u
提出日時 2019-05-27 20:16:25
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,173 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 32,104 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 18:16:45
合計ジャッジ時間 1,628 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:9:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:15:24: note: in expansion of macro 'gc'
   15 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: 17 2つの地点に泊まりたい
// 2019.5.27 bal4u

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

//// 高速入力
#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif
int in()    // 非負整数の入力
{
	int n = 0, c = gc();
	//	while (isspace(c)) c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}


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

	i = qsize++;
	que[i].t = t, que[i].n = n, que[i].a = a, que[i].b = b;
	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 52
int N;
int hi[MAXV], to[MAXV][MAXV], tm[MAXV][MAXV];
char vis[MAXV][MAXV][MAXV];
int S[MAXV];

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

	enq(start, 0, 0, 0);
	while (qsize) {
		n = que[0].n, t = que[0].t, a = que[0].a, b = que[0].b, deq();
		if (n == goal && a > 0 && b > 0) return t;
		if (vis[n][a][b]) continue;
		vis[n][a][b] = 1;
		for (i = 0; i < hi[n]; i++) {
			nx = to[n][i], nt = t + tm[n][i];
			if (!vis[nx][a][b]) enq(nx, nt, a, b);
			if (n > start && n < goal) {
				if (a == 0 && !vis[nx][n][b]) enq(nx, nt + S[n], n, b);
				else if (b == 0 && n != a && !vis[nx][a][n]) enq(nx, nt + S[n], a, n);
			}
		}
	}
	return -1;
}

int main()
{
	int i, k, M, a, b, c;

	N = in(); for (i = 0; i < N; i++) S[i] = in();
	M = in(); while (M--) {
		a = in(), b = in(), c = in();
		k = hi[a]++, to[a][k] = b, tm[a][k] = c;
		k = hi[b]++, to[b][k] = a, tm[b][k] = c;
	}
	printf("%d\n", dijkstra(0, N-1));
	return 0;
}
0