//#pragma warning(disable:4996) //#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 //< in.txt > out.txt using namespace std; //std::ios::sync_with_stdio(false); //std::cin.tie(0); const long long MOD = 1e9 + 7; const long long INF = 1e18; typedef long long LL; typedef long double LD; //typedef boost::multiprecision::cpp_int bigint; typedef pair PLL; typedef pair PI; typedef pair pdl; typedef pair pdd; typedef vector VLL; typedef vector VVLL; typedef vector VI; typedef vector> VVI; typedef unsigned long long ULL; template using pqueue = priority_queue, function>; template inline void chmin(T& a, T b) { a = min(a, b); } template inline void chmax(T& a, T b) { a = max(a, b); } void input(); void solve(); void daminput(); void naive(); void outputinput(); int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); input(); //daminput(); solve(); //naive(); //outputinput(); return 0; } ////////////////////////////////////////////////// ////////////////////////////////////////////////// int N, M, d; VI U, V, P, Q, W; void input() { cin >> N >> M >> d; U.resize(M); V.resize(M); P.resize(M); Q.resize(M); W.resize(M); for (int m = 0; m < M; m++) { cin >> U[m] >> V[m] >> P[m] >> Q[m] >> W[m]; } } void daminput() { } template class MaxFEdge { public: int to; F cap; int rev; MaxFEdge(int _to, F _cap, int _rev) :to(_to), cap(_cap), rev(_rev) {} }; template class Dinic { public: int V; vector>> G; VI itr, level; Dinic(int _v) :V(_v) { G.resize(V); } void AddEdge(int src, int to, F cap) { G[src].push_back(MaxFEdge(to, cap, G[to].size())); G[to].push_back(MaxFEdge(src, 0, G[src].size() - 1)); } void bfs(int s) { level.assign(V, -1); queue q; level[s] = 0; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (auto& e : G[v]) { if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; q.push(e.to); } } } } F dfs(int v, int t, F f) { if (v == t)return f; F res = 0; for (int& i = itr[v]; i < (int)G[v].size(); i++) { MaxFEdge& e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { F d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; res += d; f -= d; } } } return res; } F MaxFlow(int s, int t) { F res = 0; F f = 0; F inf = numeric_limits::max(); while (true) { bfs(s); if (level[t] < 0)return res; itr.assign(V, 0); while (true) { f = dfs(s, t, inf); if (f <= 0)break; res += f; } } } }; void solve() { vector vers; { set versset; versset.insert(PLL(1, 0)); versset.insert(PLL(N, (LL)1e9+(LL)d)); for (int m = 0; m < M; m++) { versset.insert(PI(U[m], P[m])); versset.insert(PI(V[m], Q[m]+d)); } for (PLL pll : versset) { vers.push_back(pll); } sort(vers.begin(), vers.end(), [](PLL a, PLL b) { if (a.first != b.first)return a.first < b.first; else return a.second < b.second; }); } auto conv = [&](PLL a) { int s = -1, e = vers.size(); while (e - s > 1) { int m = (e + s) / 2; if (vers[m].first < a.first)s = m; else if (vers[m].first > a.first)e = m; else if (vers[m].second <= a.second)s = m; else e = m; } return s; }; Dinic D(vers.size()); for (int m = 0; m < M; m++) { int s = conv(PLL(U[m], P[m])); int e = conv(PLL(V[m], Q[m]+d)); D.AddEdge(s, e, W[m]); } for (int n = 0; n < vers.size()-1; n++) { if (vers[n].first == vers[n + 1].first) { D.AddEdge(n, n + 1, 1e15); } } LL maxflow = D.MaxFlow(0, vers.size() - 1); cout << maxflow << "\n"; } void naive() { } void outputinput() { }