# include # include #include # include #include #include #include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include using namespace std; using LL = long long; using ULL = unsigned long long; constexpr long long MOD = 1000000000 + 7; constexpr long long INF = std::numeric_limits::max(); const double PI = acos(-1); #define fir first #define sec second typedef pair Pll; typedef pair> Ppll; typedef pair>> Pbll; typedef pair>> Pvll; typedef pair Vec2; struct Tll { LL first, second, third; }; typedef pair Ptll; #define rep(i,rept) for(LL i=0;i=0;i--) LL h, w, n, m, k, s, t, q, sum, last, ans=INF, cnt,flag[51][2],d[51][310],a[1000][4]; struct Edge { LL to, cost,mon; }; string str; bool f = 0; void YN(bool f) { if (f) cout << "YES" << endl; else cout << "NO" << endl; } void yn(bool f) { if (f) cout << "Yes" << endl; else cout << "No" << endl; } vectorvec[10000]; void add_edge(LL x, LL y, LL z,LL w) { vec[x].push_back(Edge{ y,z,w }); } void DIUX(LL s) { rep(i, 51)rep(j,310 ) d[i][j] = INF; rep(i, 51)rep(j,2)flag[i][j] = INF; d[s][0] = 0; priority_queue, greater> pq; pq.push(Ppll(d[s][0], Pll(s, 0))); // (cost,頂点番号) while (!pq.empty()) { Ppll pp; pp = pq.top(); LL cos = pp.first, vv = pp.second.first, hav = pp.second.second; pq.pop(); if (d[vv][hav] < pp.first||(flag[vv][0] hav + vec[vv][i].mon) { flag[vec[vv][i].to][0] = hav + vec[vv][i].mon; flag[vec[vv][i].to][1] = d[vec[vv][i].to][hav + vec[vv][i].mon]; } pq.push(Ppll(d[vec[vv][i].to][hav + vec[vv][i].mon], Pll(vec[vv][i].to, hav + vec[vv][i].mon))); } } } } int main() { cin >> n >> t >> m; rep(j, 4) rep(i, m) { cin >> a[i][j]; } rep(i, m) add_edge(a[i][0], a[i][1], a[i][3], a[i][2]); DIUX(1); rep(i, 310)ans = min(ans, d[n][i]); cout << (ans==INF?-1:ans) << endl; return 0; }