import java.util.*; public class WhichWarehouse_John { public static final int MAX_NODES = 100; public static final int MAX_WEIGHT = 1000; public static final int INFINITY = MAX_NODES*MAX_WEIGHT + 1; public static void main(String [] args) { int w, p; Scanner in = new Scanner(System.in); int [][] dist; int [][] inventory; w = in.nextInt(); p = in.nextInt(); inventory = new int[w][p]; dist = new int[w][w]; for(int i=0; i " + j + ": " + dist[i][j]); } } */ // setup bipartite graph BiGraph g = new BiGraph(w, w); int [][] table = new int[p][w]; // table[p][w] = cost to move item p to warehouse w for(int i=0; i " + (j-w) + ": " + val+" "); } } for(int i=p; i " + j + ": " + val+" "); } } int [] matching = g.findMinCostMatching(); int cost = 0; for(int i=0; i" + (matching[i]-w)); cost += table[i][matching[i]-w]; } System.out.println(cost); } } class BiGraph { private static final int INF = 100000000; private ArrayList vertices = new ArrayList(); private int n, m; public BiGraph(int nn, int mm) { n=nn; m = mm; vertices = new ArrayList(); for(int i=0; i itr = vertices.iterator(); for(int i=0; i path = findMinCostPath(); if (path == null) break; addPath(path); } int [] matching = new int[n]; itr = vertices.iterator(); for(int i=0; i path) { for(Iterator itr = path.iterator() ; itr.hasNext() ; ) { Edge e = itr.next(); vertices.get(e.v).edges.remove(e); vertices.get(e.w).edges.add(new Edge(e.w, e.v, -e.cost)); } } private ArrayList findMinCostPath() { int nVert = n + m + 2; TableEntry [][] table = new TableEntry[nVert][nVert]; for(int i=0; i " + j + ": " + val+" "); } // System.out.println(); } int [] matching = g.findMinCostMatching(); boolean [] matched = new boolean[2*size]; for(int i=0; i" + matching[i]); if (matching[i] < size || matching[i] >= 2*size) System.out.println("ERROR: matching for " + i + " out of bounds"); else if (matched[matching[i]]) System.out.println("ERROR: matching for " + i + " already matched"); else matched[matching[i]] = true; } } } } class TableEntry { int cost; Edge prev; public TableEntry(int c, Edge p) { cost = c; prev = p; } } class Vertex { String name; int [] products; // used to store amount of each product in initial graph int num; ArrayList edges; public Vertex(String n, int nm) { name = n; num = nm; edges = new ArrayList(); } public void addEdge(Edge e) { edges.add(e); } } class Edge { int v, w; int cost; public Edge(int vv, int ww, int c) { v = vv; w = ww; cost = c; } }