結果

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

テストケース

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

ソースコード

diff #

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

#include <stdio.h>
#include <stdlib.h>
#include <string.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, 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 MAXN  55
int C; // 使える金額の上限
int N; // 町数
int V; // 道路の本数
int hi[MAXN], *to[MAXN], *money[MAXN], *tim[MAXN];
char vis[MAXN];
int S[MAXN], T[MAXN], Y[MAXN], M[MAXN];

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

	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 (vis[n]) continue;
		vis[n] = 1;

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

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

	N = in(), C = in(), V = in();
	for (i = 0; i < V; i++) S[i] = in(), hi[S[i]]++;
	for (i = 0; i < V; i++) T[i] = in();
	for (i = 0; i < V; i++) Y[i] = in();
	for (i = 0; i < V; i++) M[i] = in();
	for (i = 1; 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(1, N));
	return 0;
}
0