// yukicoder: No.1 道のショートカット // 2019.4.7 bal4u // 最短距離に関する問題。bfsでやってみる #include #include #include //// 高速入力 #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; }