結果

問題 No.1 道のショートカット
ユーザー bal4ubal4u
提出日時 2019-04-20 11:03:05
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 2,587 bytes
コンパイル時間 1,619 ms
コンパイル使用メモリ 30,744 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 22:18:34
合計ジャッジ時間 2,976 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 0 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 0 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 0 ms
4,376 KB
testcase_08 AC 0 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 0 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 0 ms
4,376 KB
testcase_18 AC 0 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 0 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 0 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 0 ms
4,380 KB
testcase_30 AC 0 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 0 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 0 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:12:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   12 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:18:24: 備考: in expansion of macro ‘gc’
   18 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: No.1 道のショートカット
// 2019.4.7 bal4u
// 最短距離に関する問題。ダイクストラ法でやってみる

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.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 10000
typedef struct { int t, n, money; } 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 money)
{
	int i, min;
	QUE qt;

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


// 本問題関連
#define INF   0x55555555
#define MAXN  55
#define MAXV  1505
int C; // 使える金額の上限
int N; // 町数
int V; // 道路の本数
int hi[MAXN], *to[MAXN], *money[MAXN], *tim[MAXN];
int dst[MAXN][303];
int S[MAXV], T[MAXV], Y[MAXV], M[MAXV];

int dijkstra(int start, int goal)
{
	int i, n, t, m, nx, tx, mx;

	memset(dst, INF, sizeof(dst));
	enq(start, 0, 0);
	while (qsize) {
		n = que[0].n, t = que[0].t, m = que[0].money, deq();
		if (n == goal) return t;
		if (t >= dst[n][m]) continue;
		dst[n][m] = t;

		for (i = 0; i < hi[n]; i++) {
			mx = m + money[n][i];
			if (mx > C) continue;
			nx = to[n][i], tx = t + tim[n][i];
			if (tx < dst[nx][mx]) enq(nx, tx, mx);
		}
	}
	return -1;
}

int main()
{
	int i, k, s;

	N = in(), C = in(), V = in();
	for (i = 0; i < V; i++) S[i] = in()-1, hi[S[i]]++;
	for (i = 0; i < V; i++) T[i] = in()-1;
	for (i = 0; i < V; i++) Y[i] = in();
	for (i = 0; i < V; i++) M[i] = in();
	for (i = 0; i < N; i++) if (hi[i]) {
		to[i] = malloc(sizeof(int)*hi[i]);
		money[i] = malloc(sizeof(int)*hi[i]);
		tim[i] = malloc(sizeof(int)*hi[i]);
	}
	memset(hi, 0, sizeof(hi));
	for (i = 0; i < V; i++) {
		s = S[i];
		k = hi[s]++;
		to[s][k] = T[i], money[s][k] = Y[i], tim[s][k] = M[i];
	}
	printf("%d\n", dijkstra(0, N-1));
	return 0;
}
0