// URL: https://yukicoder.me/problems/no/654 import std.algorithm, std.array, std.container, std.math, std.range, std.typecons, std.string; version(unittest) {} else void main() { int N, M, d; io.getV(N, M, d); int[] u, v, p, q, w; io.getC(M, u, v, p, q, w); u[] -= 1; v[] -= 1; auto dep = new int[][](N), arr = new int[][](N); foreach (i; 0..M) { dep[u[i]] ~= p[i]; arr[v[i]] ~= q[i]; } auto st = 0, tt = 10^^9; dep[0] ~= st; arr[N-1] ~= tt; foreach (i; 0..N) { dep[i] = dep[i].sort.uniq.array; arr[i] = arr[i].sort.uniq.array; } auto deph = new int[int][](N), arrh = new int[int][](N), c = 0; foreach (i; 0..N) foreach (pi; dep[i]) deph[i][pi] = c++; foreach (i; 0..N) foreach (qi; arr[i]) arrh[i][qi] = c++; auto g = GraphW!(long, 10L^^18)(c); foreach (i; 0..M) g.addEdge(deph[u[i]][p[i]], arrh[v[i]][q[i]], w[i]); foreach (i; 0..N) if (!dep[i].empty) foreach (j; 0..dep[i].length-1) g.addEdge(deph[i][dep[i][j]], deph[i][dep[i][j+1]], g.inf); foreach (i; 0..N-1) foreach (qi; arr[i]) { auto np = dep[i].assumeSorted.upperBound(qi+d-1); if (!np.empty) g.addEdge(arrh[i][qi], deph[i][np.front], g.inf); } foreach (i; 0..arr[N-1].length-1) g.addEdge(arrh[N-1][arr[N-1][i]], arrh[N-1][arr[N-1][i+1]], g.inf); io.put(g.dinic(deph[0][st], arrh[N-1][tt]).flow); } struct Graph { alias Node = int; Node n; Node[][] g; alias g this; this(Node n) { this.n = n; g = new Node[][](n); } void addEdge(Node u, Node v) { g[u] ~= v; } void addEdgeB(Node u, Node v) { g[u] ~= v; g[v] ~= u; } } struct GraphW(W = int, W i = 10^^9) { alias Node = int, Wt = W, inf = i; struct Edge { Node src, dst; Wt wt; alias cap = wt; } Node n; Edge[][] g; alias g this; this(Node n) { this.n = n; g = new Edge[][](n); } void addEdge(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); } void addEdgeB(Node u, Node v, Wt w) { g[u] ~= Edge(u, v, w); g[v] ~= Edge(v, u, w); } } struct GraphM(W = int, W i = 10^^9) { alias Node = int, Wt = W, inf = i; Node n; Wt[][] g; alias g this; this(int n) { this.n = n; g = new Wt[][](n, n); } static GraphM!(W, i) init(Node n) { auto g = GraphM!(W, i)(n); foreach (i; 0..n) { g[i][] = inf; g[i][i] = 0; } return g; } } struct Dinic(Graph) { alias Node = Graph.Node, Wt = Graph.Wt; Graph g; alias g this; Wt flow = 0; this(Graph g, Node s, Node t) { this.g = g; auto adj = withRev(), level = new int[](n); auto levelize() { level[] = -1; level[s] = 0; auto q = DList!Node(s); while (!q.empty) { auto u = q.front; q.removeFront(); if (u == t) break; foreach (ref e; adj[u]) if (e.cap > e.flow && level[e.dst] < 0) { q.insertBack(e.dst); level[e.dst] = level[u] + 1; } } return level[t]; } Wt augment(Node u, Wt cur) { if (u == t) return cur; foreach (ref e; adj[u]) { auto r = &adj[e.dst][e.rev]; if (e.cap > e.flow && level[u] < level[e.dst]) { auto f = augment(e.dst, min(cur, e.cap - e.flow)); if (f > 0) { e.flow += f; r.flow -= f; return f; } } } return 0; } Wt f = 0; while (levelize >= 0) while ((f = augment(s, g.inf)) > 0) flow += f; } private { struct EdgeR { Node src, dst; Wt cap, flow; Node rev; } EdgeR[][] withRev() { auto r = new EdgeR[][](n); foreach (gi; g) foreach (e; gi) { r[e.src] ~= EdgeR(e.src, e.dst, e.cap, 0, cast(Node)(r[e.dst].length)); r[e.dst] ~= EdgeR(e.dst, e.src, 0, 0, cast(Node)(r[e.src].length) - 1); } return r; } } } Dinic!Graph dinic(Graph, Node)(Graph g, Node s, Node t) { return Dinic!Graph(g, s, t); } auto io = IO!()(); import std.stdio; struct IO(string floatFormat = "%.10f", string delimiter = " ", alias IN = stdin, alias OUT = stdout) { import std.conv, std.format, std.meta, std.traits; alias assignable = hasAssignableElements; auto getV(T...)(ref T v) { foreach (ref w; v) get(w); } auto getA(T)(size_t n, ref T v) if (assignable!T) { v = new T(n); foreach (ref w; v) get(w); } auto getC(T...)(size_t n, ref T v) if (allSatisfy!(assignable, T)) { foreach (ref w; v) w = new typeof(w)(n); foreach (i; 0..n) foreach (ref w; v) get(w[i]); } auto getM(T)(size_t r, size_t c, ref T v) if (assignable!T && assignable!(ElementType!T)) { v = new T(r); foreach (ref w; v) getA(c, w); } template getS(E...) { auto getS(T)(size_t n, ref T v) { v = new T(n); foreach (ref w; v) foreach (e; E) mixin("get(w."~e~");"); } } auto put(bool flush = false, T...)(T v) { foreach (i, w; v) { putA(w); if (i < v.length-1) OUT.write(delimiter); } OUT.writeln; static if (flush) OUT.flush(); } auto putB(S, T)(bool c, S t, T f) { if (c) put(t); else put(f); } auto putRaw(T...)(T v) { OUT.write(v); OUT.writeln; } private { dchar[] buf; auto sp = (new dchar[](0)).splitter; void nextLine() { IN.readln(buf); sp = buf.splitter; } auto get(T)(ref T v) { if (sp.empty) nextLine(); v = sp.front.to!T; sp.popFront(); } auto putR(T)(T v) { auto w = v; while (!w.empty) { putA(w.front); w.popFront(); if (!w.empty) OUT.write(delimiter); } } auto putA(T)(T v) { static if (isInputRange!T && !isSomeString!T) putR(v); else if (isFloatingPoint!T) OUT.write(format(floatFormat, v)); else OUT.write(v); } } }