#include #define REP(i,n) for(int i=0;i<(int)(n);i++) #define ALL(x) (x).begin(),(x).end() using namespace std; typedef int Weight; Weight INF = 1000000000; struct Edge{ int src, dest; Weight weight; }; typedef vector Edges; typedef vector Graph; typedef vector Array; typedef vector Matrix; int N, C, V, s[2048], t[2048], y[2048], m[2048]; int getId(int i, int c) { return i * (C + 1) + c; } void add_edge(Graph &g, int src, int dest, Weight weight) { g[src].push_back((Edge){src, dest, weight}); } void dijkstra(Graph &g, Array &d, int s) { d.assign(g.size(), INF); d[s] = 0; typedef pair P; priority_queue, greater

> que; que.push(P(0, s)); while (!que.empty()) { Weight dist = que.top().first; int v = que.top().second; que.pop(); if (d[v] < dist) continue; REP(i, g[v].size()) { Edge e = g[v][i]; if (d[e.dest] > d[v] + e.weight) { d[e.dest] = d[v] + e.weight; que.push(P(d[e.dest], e.dest)); } } } } int main() { cin >> N >> C >> V; REP(i,V) cin >> s[i]; REP(i,V) cin >> t[i]; REP(i,V) cin >> y[i]; REP(i,V) cin >> m[i]; Graph g(N * (C + 1)); REP(i,N) REP(j,C) add_edge(g, getId(i, j+1), getId(i, j), 0); REP(i,V) REP(j,C+1) { int nj = j - y[i]; if (nj < 0) continue; add_edge(g, getId(s[i]-1, j), getId(t[i]-1, nj), m[i]); } Array d; dijkstra(g, d, getId(0, C)); int res = d[getId(N-1, 0)]; if (res == INF) cout << -1 << endl; else cout << res << endl; return 0; }