#include #include using namespace atcoder; #ifdef LOCAL #include #define dbg(...) debug::dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) void(0) #endif using namespace std; // #define int ll #define rep(i, a, b) for(int i=static_cast(a), i##_end__=static_cast(b); i < i##_end__; i++) #define rep_r(i, a, b) for(int i=static_cast(a), i##_end__=static_cast(b); i >= i##_end__; i--) #define fore(i, a) for(auto& i: a) #define all(x) std::begin(x), std::end(x) using ll = long long; // __int128; using ull = unsigned long long; template int siz(const C& c) { return static_cast(c.size()); } template constexpr int siz(const T (&)[N]) { return static_cast(N); } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template constexpr T pow2(T x){ return x * x; } template constexpr T divceil(T x, S div) { return (x % div == 0) ? x / div : (x >= 0) ? (x / div) + 1 : -((-x) / div); } template constexpr T divfloor(T x, S div){ return (x % div == 0) ? x / div : (x >= 0) ? (x / div) : -((-x) / div) - 1; } template inline void oneline(T x){ bool f = true; for(auto i: x){ if(f) f = false; else cout << " "; cout << i; } cout << endl; } inline void yesno(bool x){ cout << (x ? "Yes" : "No") << endl; } constexpr long long INF = 1LL << 60; // 1.15e18 constexpr int MOD = (int)1e9 + 7; void _main() { int N, M, d; cin >> N >> M >> d; vector>> G(N); auto S = 2 * M; auto T = 2 * M + 1; mf_graph mf(T + 1); rep(id, 0, M){ int u, v, p, q, w; cin >> u >> v >> p >> q >> w; u--; v--; G[u].push_back({v, p, q, w, id}); if(u == 0 ) mf.add_edge(S, id, w); if(v == N - 1) mf.add_edge(id, T, w); } rep(i, 0, N){ for(auto [fv, fp, fq, fw, fid]: G[i]){ auto new_id = M + fid; mf.add_edge(fid, new_id, fw); for(auto [tv, tp, tq, tw, tid]: G[fv]){ if(fq + d <= tp) mf.add_edge(new_id, tid, INF); } } } auto maxflow = mf.flow(S, T); cout << maxflow << endl; dbg(G); dbg(N, M, d, S, T, mf.edges()); } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(15); _main(); }