#include #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) using namespace std; typedef long long ll; const int INF = 1e8; // const ll INF = 1LL << 60; typedef pair P; struct edge { int to, cap, rev, p, q; }; vector G[1010]; bool used[1010]; int n, m, day; // G[e.to][e.rev] で逆辺の構造体にアクセスできる void add_edge(int from, int to, int cap, int p, int q) { G[from].push_back((edge){to, cap, (int)G[to].size(), p, q}); G[to].push_back((edge){from, 0, (int)G[from].size() - 1, p, q}); } int dfs(int v, int t, int f, int current_time) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (!used[e.to] && e.cap > 0 && e.p >= current_time) { int d = dfs(e.to, t, min(f, e.cap), e.q + day); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int main_flow(int s, int t) { int flow = 0; for (;;) { memset(used, 0, sizeof(used)); int f = dfs(s, t, INF, 0); if (f == 0) return flow; flow += f; } } int main() { cin >> n >> m >> day; rep(i, m) { int from, to, cost, p, q; cin >> from >> to >> p >> q >> cost; from--; to--; add_edge(from, to, cost, p, q); } // rep(i, n) { // rep(j, G[i].size()) { // cout << i << " " << j << " " << G[i][j].rev << endl; // } // } cout << main_flow(0, n - 1) << endl; }