#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(flow, e.avail)); 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); vector> logs; // mp[(街, 時刻, 発か)] := id map, int> mp; int idx = 0; for(int i=0; i()); ptr.assign(n, 0); for(auto [u, v, p, q, w] : logs) { auto k0 = make_tuple(u, p, true), k1 = make_tuple(v, q, false); add_edge(mp[k0], mp[k1], w); } for(auto&& [key0, _] : mp) { auto&& [u0, t0, b0] = key0; if(u0 == 1 && b0) { add_edge(src, mp[key0], inf); } if(u0 == N && !b0) { add_edge(mp[key0], dst, inf); } for(auto&& [key1, __] : mp) { auto [u1, t1, b1] = key1; if(!b0 && b1 && u0 == u1 && t0+d <= t1) { add_edge(mp[key0], mp[key1], inf); } } } i64 maxi = max_flow(src, dst); printf("%ld\n", maxi); return 0; }