#include #include #include #include #include using namespace std; using ll = long long int; constexpr ll INF = 1001001001001001; template class MaxFlowGraph{ private: struct Edge{ int to; T cap; int rev; }; using graph = vector>; graph G; vector used; vector level; vector iter; public: MaxFlowGraph(int n){ G = graph(n); used = vector(n, false); level = vector(n); iter = vector(n); } void add_edge(int from, int to, T cap){ G[from].push_back({to, cap, (int)G[to].size()}); G[to].push_back({from, 0, (int)G[from].size()-1}); } void bfs(int s){ fill(level.begin(), level.end(), -1); queue Q; level[s] = 0; Q.push(s); while(Q.size()){ int v = Q.front(); Q.pop(); for(int i = 0; i < G[v].size(); i++){ auto &e = G[v][i]; if(e.cap > 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; Q.push(e.to); } } } } T dfs(int v, int t, T f){ if(v == t) return f; for(int &i = iter[v]; i < G[v].size(); i++){ auto &e = G[v][i]; if(e.cap > 0 && level[v] < level[e.to]){ T d = dfs(e.to, t, min(f, e.cap)); if(d > 0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } T flow(int s, int t){ T maxflow = 0; while(true){ bfs(s); if(level[t] < 0) return maxflow; fill(iter.begin(), iter.end(), 0); int f; while((f = dfs(s, t, INF)) > 0) maxflow += f; } } }; int main(){ int n, m, d; cin >> n >> m >> d; vector u(m), v(m), p(m), q(m), r(m); for(int i = 0; i < m; i++){ cin >> u[i] >> v[i] >> p[i] >> q[i] >> r[i]; u[i]--; v[i]--; } map memo; for(int i = 0; i < m; i++) memo[p[i]] = 1; for(int i = 0; i < m; i++) memo[q[i]+d] = 1; { int i = 0; for(auto &pp: memo){ pp.second = i; i++; } } MaxFlowGraph G(n*memo.size()); const int s = 0; const int t = n*memo.size()-1; for(int i = 0; i < m; i++){ G.add_edge(n*memo[p[i]]+u[i], n*memo[q[i]+d]+v[i], r[i]); } for(int i = 0; i < n; i++){ for(int j = 0; j+1 < memo.size(); j++) G.add_edge(n*j+i, n*(j+1)+i, INF); } cout << G.flow(s, t) << endl; return 0; }