import java.io.*; import java.util.*; /** * Use a min-cost max flow algorithm. * First, use the Floyd-Warshall algorithm to get all pairs shortest paths between * the warehouse locations. * Then, compute the cost to move all of each product i to each warehouse j. * Then, set up a bipartite graph with vertices corresponding to the products in one * partite set and vertices corrsponding to the warehouses in the other. Give capacity * 1 (and 0 cost) to edges between a source vertex and the product vertices and * capacity 1 (and 0 cost) to edges between warehouse vertices and the sink vertex. * Add a capacity 1 edge from each product i to each warehouse j with a cost equal to * the cost to move all of product i to warehouse j. * Run a min-cost max-flow algorithm on the resulting graph. * * @author Finn Lidbetter */ public class WhichWarehouse_Finn { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] s = br.readLine().split(" "); int w = Integer.parseInt(s[0]); int p = Integer.parseInt(s[1]); long[][] products = new long[w][p]; for (int i=0; i val) { dist[k] = val; dad[k] = s; } } if (flow[s][k] < cap[s][k]) { long val = dist[s] + pi[s] - pi[k] + cost[s][k]; if (dist[k] > val) { dist[k] = val; dad[k] = s; } } if (dist[k] < dist[best]) { best = k; } } s = best; } for (int k = 0; k < N; k++) { pi[k] = Math.min(pi[k] + dist[k], INF); } return found[t]; } }