#include "bits/stdc++.h" using namespace std; typedef long long int lint; typedef pair plint; typedef pair pld; #define Alint(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1ll << (n)) #define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) templatebool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } template pair operator+(const pair& l, const pair& r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair& l, const pair& r) { return make_pair(l.first - r.first, l.second - r.second); } const lint MOD = 998244353, INF = 1e18 ; struct edge { lint to, cap, rev; }; vector Graph[2000010]; int level[2000010]; int iter[2000010]; void add_edge(lint from, lint to, lint cap) { Graph[from].push_back({ to, cap, SZ(Graph[to]) }); Graph[to].push_back({ from, 0, SZ(Graph[from]) - 1 }); } void bfs(lint s) { memset(level, -1, sizeof(level)); queue que; que.push(s); level[s] = 0; while (!que.empty()) { lint v = que.front(); que.pop(); REP(i, SZ(Graph[v])) { edge& e = Graph[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } lint dfs(lint v, lint t, lint f) { if (v == t) return f; for (int& i = iter[v]; i < SZ(Graph[v]); i++) { edge& e = Graph[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { lint d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; Graph[e.to][e.rev].cap += d; return d; } } } return 0; } lint max_flow(lint s, lint t) { lint flow = 0; for (;;) { bfs(s); if (level[t] < 0) return flow; memset(iter, 0, sizeof(iter)); lint f; while ((f = dfs(s, t, INF)) > 0) { flow += f; } } } lint N, M, D; lint u[1000], v[1000], p[1000], q[1000], w[1000]; map toidx_t; set st; vector t_point[1001]; const lint MAX = pow(10, 9); int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N >> M >> D; st.insert(0); st.insert(MAX); REP(i, N) { t_point[i].push_back(0); } REP(i, M) { cin >> u[i] >> v[i] >> p[i] >> q[i] >> w[i]; u[i]--; v[i]--; st.insert(p[i]); st.insert(min(q[i] + D, MAX)); t_point[u[i]].push_back(p[i]); t_point[v[i]].push_back(min(q[i] + D, MAX)); } REP(i, N) { t_point[i].push_back(MAX); sort(t_point[i].begin(), t_point[i].end()); } //座圧 lint idx = 0; for (auto itr : st) { toidx_t[itr] = idx; idx++; } //二次元情報(街番号(i)、時間(t))を一次元情報に圧縮する // Info = i*idx + f(t) REP(i, M) { add_edge(u[i] * idx + toidx_t[p[i]], v[i] * idx + toidx_t[min(q[i] + D, MAX)], w[i]); } //同じ街に対して時間に沿った有向辺を張る REP(i, N) { REP(j, SZ(t_point[i]) - 1) { if (t_point[i][j] == t_point[i][j + 1]) continue; add_edge(i * idx + toidx_t[t_point[i][j]], i * idx + toidx_t[t_point[i][j + 1]], INF); } } cout << max_flow(0, N * idx - 1); }