#include #include #include using namespace std; using i64 = int64_t; struct edge { int to; i64 avail; int rev; }; constexpr i64 inf = 987'654'321'987'654'321LL; int n; vector> G; vector ptr; vector used; void add_edge(int u, int v, i64 c) { G[u].push_back(edge{v, c, ptr[v]++}); G[v].push_back(edge{u, 0, ptr[u]++}); } i64 dfs(int v, int t, i64 flow) { if(v == t) { return flow; } used[v] = true; for(edge &e : G[v]) { edge &x = G[e.to][e.rev]; if(!used[e.to] && e.avail) { i64 res = dfs(e.to, t, min(e.avail, flow)); if(res) { e.avail -= res; x.avail += res; return res; } } } return 0; } i64 max_flow(int s, int t) { i64 res = 0; for(;;) { used.assign(n, false); i64 flow = dfs(s, t, inf); if(flow == 0) { return res; } res += flow; } } int main(void) { int N, M, d; scanf("%d%d%d", &N, &M, &d); int idx = 0; map, int> mp; // mp[(街,時刻,発か)] := id vector> logs; for(int i=0; i()); ptr.assign(n, 0); int src = idx, dst = src + 1; for(auto tup : logs) { int u, v, p, q, w; tie(u, v, p, q, w) = tup; auto k0 = make_tuple(u, p, true), k1 = make_tuple(v, q, false); add_edge(mp[k0], mp[k1], w); if(u == 1) { add_edge(src, mp[k0], inf); } if(v == N) { add_edge(mp[k1], dst, inf); } } for(auto kv0 : mp) { tuple k0; int v0; tie(k0, v0) = kv0; int u, p, f0; tie(u, p, f0) = k0; for(auto kv1 : mp) { tuple k1; int v1; tie(k1, v1) = kv1; int v, q, f1; tie(v, q, f1) = k1; if(u == v && !f0 && f1 && p+d <= q) { add_edge(mp[k0], mp[k1], inf); } } } i64 maxi = max_flow(src, dst); printf("%ld\n", maxi); return 0; }