#include #define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++) #define rep(i, begin, end) for(int i = int(begin); i < int(end); i++) #define lint long long auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;}; auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;}; void debug_impl() { std::cerr << std::endl; } template void debug_impl(Head head, Tail... tail){ std::cerr << " " << head; debug_impl(tail...); } #define debug(...) std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\ debug_impl(__VA_ARGS__);\ std::cerr << std::noboolalpha; template struct dinic_edge { int to; Value cap; int rev; dinic_edge(int to, Value cap, int rev): to(to), cap(cap), rev(rev){} }; template std::ostream& operator<< (std::ostream& os, const dinic_edge& e) { return os << "dinic_edge{" << "to:" << e.to << "," << "cap:" << e.cap << "," << "rev:" << e.rev << "}"; } template class dinic { const int n, source, sink; std::vector ckd; std::vector level; std::vector>> graph; static constexpr Value inf = std::numeric_limits::max(); void bfs () { std::queue que; que.emplace(source); level.at(source) = 0; while (!que.empty()) { auto crr = que.front(); que.pop(); for (auto const& e : graph.at(crr)) { if (e.cap == 0) continue; int nxt = e.to; if (level.at(nxt) != -1) continue; que.push(nxt); level.at(nxt) = level.at(crr) + 1; } } } auto dfs (int crr, Value f = inf) -> Value { if (crr == sink) return f; ckd.at(crr) = true; for (auto& e : graph.at(crr)) { auto nxt = e.to; if (ckd.at(nxt) || e.cap == 0 || level.at(crr) >= level.at(nxt)) continue; auto d = dfs(nxt, std::min(f, e.cap)); if (d > 0) { e.cap -= d; graph.at(nxt).at(e.rev).cap += d; return d; } } level.at(crr) = -1; return 0; } public: dinic (int n, int source, int sink) : n(n), source(source), sink(sink), graph(n) {} void insert(int u, int v, Value c) { int k = graph.at(u).size(); int l = graph.at(v).size(); graph.at(u).emplace_back(v, c, l); graph.at(v).emplace_back(u, 0, k); } Value build() { Value ret = 0; while (true) { level.assign(n, -1); bfs(); if (level.at(sink) == -1) return ret; ckd.assign(n, false); while (true) { Value f = dfs(source); if (f == 0) break; ret += f; } } } }; template std::ostream& operator<< (std::ostream& os, const std::pair& pair){ return os << "(" << pair.first << "," << pair.second << ")"; } template std::istream& operator>> (std::iostream& is, std::pair& pair) { return is >> pair.first >> pair.second; } template std::istream& operator>> (std::istream& is, std::vector& v) { for (auto & x : v) is >> x; return is; } template std::ostream& operator<< (std::ostream& os, const std::vector& v) { auto n = v.size(); os << "{"; for (size_t i = 0; i < n; i++) os << (i > 0 ? "," : "") << v[i]; return os << "}"; } template auto make_vector_impl(size_t sz, T t) {return std::vector(sz, t);} template = nullptr> auto make_vector(size_t sz, U u) {return make_vector_impl(sz, T(u));} template = nullptr> auto make_vector(size_t sz) {return std::vector(sz);} template = nullptr> auto make_vector(size_t a, Args... args) {return make_vector_impl(a, make_vector(args...));} template auto& at(T& t, Size_t i) {return t.at(i);} template auto& at(T& t, Size_t i, Args... args) {return at(t.at(i), args...);} int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); int n, m, d; std::cin >> n >> m >> d; std::vector u(m), v(m), p(m), q(m), w(m); rep(i, 0, m) { std::cin >> u.at(i) >> v.at(i) >> p.at(i) >> q.at(i) >> w.at(i); u.at(i)--, v.at(i)--, q.at(i) += d; } constexpr long long linf = 1LL << 60; auto s = 2 * m, t = 2 * m + 1; auto dnc = dinic(2 * m + 2, s, t); rep (i, 0, m) { if (u.at(i) == 0) dnc.insert(s, i, linf); if (v.at(i) == n - 1) dnc.insert(m + i, t, linf); dnc.insert(i, m + i, w.at(i)); } auto events = make_vector<2, std::pair>(n, 0); rep(i, 0, m) { events.at(u.at(i)).emplace_back(p.at(i), i); events.at(v.at(i)).emplace_back(q.at(i), m + i); } rep(i, 0, n) { auto& v = events.at(i); std::sort(v.begin(), v.end(), [](auto p, auto q){ if (p.first < q.first) return true; if (p.first > q.first) return false; return p.second > q.second; }); int k = v.size(); rep(j, 0, k - 1) { dnc.insert(v.at(j).second, v.at(j + 1).second, linf); } } std::cout << dnc.build() << std::endl; return 0; }