結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 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,380 KB
testcase_16 RE -
testcase_17 AC 0 ms
4,376 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,376 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 0 ms
4,376 KB
testcase_43 AC 1 ms
4,376 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
// 最短距離に関する問題。bfsでやってみる

#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 INF   0x55555555
#define MAXN  55
typedef struct{ int node, money, t; } Q;
Q q[100000]; int top, end;

int C; // 使える金額の上限
int N; // 町数
int V; // 道路の本数
int hi[MAXN], *to[MAXN], *money[MAXN], *tim[MAXN];
int total[MAXN];
int S[MAXN], T[MAXN], Y[MAXN], M[MAXN];

int bfs(int start, int goal)
{
	int i, s, m, t, nxt;

	memset(total, INF, sizeof(total));
	q[0].node = start, q[0].money = 0, q[0].t = 0, end = 1;
	while (top != end) {
		s = q[top].node, m = q[top].money, t = q[top++].t;

		if (t >= total[s]) continue;
		total[s] = t;
		if (s == goal) continue;

		for (i = 0; i < hi[s]; i++) {
			if (m + money[s][i] > C) continue;
			nxt = to[s][i];
			if (t + tim[s][i] < total[nxt]) {
				q[end].node = nxt, q[end].money = m + money[s][i];
				q[end++].t = t + tim[s][i];
			}
		}
	}
	return total[goal] < INF? total[goal]: (-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", bfs(1, N));
	return 0;
}
0