/** * Solution to the ride hailing problem using the Ford Fulkerson * algorithm for maximum bipartite matching. * The idea is that you want to re-use the drivers as much as possible. * We have two vertices for each request--one set of starting requests, * one set of next requests. We add an edge from starting request i to * next request j if it is possible for a single driver to serve request * j after serving request i. Choosing an edge to be in the matching * corresponds to re-using a driver for consecutive requests. * The answer is then the number of requests minus the number of edges * in the matching. * * @author Finn Lidbetter */ import java.util.*; import java.io.*; public class FordFulkersonRideHailing { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); String[] s = br.readLine().split(" "); int n = Integer.parseInt(s[0]); int m = Integer.parseInt(s[1]); int k = Integer.parseInt(s[2]); Integer[][] apsp = new Integer[n][n]; for (int i=0; i[] graph = createGraph(2*k+2); for (int i=0; i[] createGraph(int nodes) { List[] graph = new List[nodes]; for (int i = 0; i < nodes; i++) graph[i] = new ArrayList<>(); return graph; } static void addEdge(List[] graph, int u, int v, int cap) { graph[u].add(new Edge(v, graph[v].size(), cap)); graph[v].add(new Edge(u, graph[u].size() - 1, 0)); } static int maxFlow(List[] graph, int s, int t) { boolean[] mincut = new boolean[graph.length]; for (int flow = 0;;) { int df = findPath(graph, mincut, s, t, Integer.MAX_VALUE); if (df == 0) return flow; Arrays.fill(mincut, false); flow += df; } } static int findPath(List[] gr, boolean[] vis, int u, int t, int f) { if (u == t) return f; vis[u] = true; for (Edge e : gr[u]) if (!vis[e.v] && e.f < e.cap) { int df = findPath(gr, vis, e.v, t, Math.min(f, e.cap - e.f)); if (df > 0) { e.f += df; gr[e.v].get(e.rev).f -= df; return df; } } return 0; } static class Edge { int v, rev; int cap, f; public Edge(int v, int rev, int cap) { this.v = v; this.rev = rev; this.cap = cap; } } }