結果

問題 No.1 道のショートカット
ユーザー bal4ubal4u
提出日時 2019-04-07 16:44:35
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 2,569 bytes
コンパイル時間 203 ms
コンパイル使用メモリ 30,840 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-22 13:29:13
合計ジャッジ時間 5,770 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 0 ms
4,380 KB
testcase_04 AC 0 ms
4,380 KB
testcase_05 AC 0 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 0 ms
4,380 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 1 ms
4,380 KB
testcase_16 RE -
testcase_17 AC 1 ms
4,380 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 AC 0 ms
4,376 KB
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

//// 高速入力
#if 0
#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, 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
int C; // 使える金額の上限
int N; // 町数
int V; // 道路の本数
int hi[MAXN], *to[MAXN], *money[MAXN], *tim[MAXN];
int dst[MAXN][303];
int S[MAXN], T[MAXN], Y[MAXN], M[MAXN];

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