|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + static Solver practice2_e = new practice2_e(); |
| 6 | + static Solver GRL_6_B = new GRL_6_B(); |
| 7 | + |
| 8 | + public static void main(String[] args) { |
| 9 | + // practice2_e.run(); |
| 10 | + GRL_6_B.run(); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +abstract class Solver implements Runnable { public abstract void run(); } |
| 15 | + |
| 16 | +/** |
| 17 | + * @problem |
| 18 | + * AtCoder Library Practice Contest E - MinCostFlow |
| 19 | + * {@see https://atcoder.jp/contests/practice2/tasks/practice2_e} |
| 20 | + * @submission |
| 21 | + * {@see https://atcoder.jp/contests/practice2/submissions/20811599} |
| 22 | + */ |
| 23 | +class practice2_e extends Solver { |
| 24 | + static class Answer { |
| 25 | + final long ans; |
| 26 | + final char[][] g; |
| 27 | + Answer(long ans, char[][] g) { |
| 28 | + this.ans = ans; |
| 29 | + this.g = g; |
| 30 | + } |
| 31 | + @Override |
| 32 | + public String toString() { |
| 33 | + StringBuilder sb = new StringBuilder(); |
| 34 | + sb.append(ans); |
| 35 | + for (char[] r : g) { |
| 36 | + sb.append('\n').append(r); |
| 37 | + } |
| 38 | + return sb.toString(); |
| 39 | + } |
| 40 | + } |
| 41 | + public void run() { |
| 42 | + Scanner sc = new Scanner(System.in); |
| 43 | + int n = Integer.parseInt(sc.next()); |
| 44 | + int k = Integer.parseInt(sc.next()); |
| 45 | + long[][] a = new long[n][n]; |
| 46 | + for (long[] r : a) Arrays.setAll(r, j -> Long.parseLong(sc.next())); |
| 47 | + sc.close(); |
| 48 | + System.out.println(solve(n, k, a)); |
| 49 | + } |
| 50 | + |
| 51 | + static final long INF = 1l << 50; |
| 52 | + |
| 53 | + public Answer solve(int n, int k, long[][] a) { |
| 54 | + MinCostFlow mcf = new MinCostFlow(n + n + 2); |
| 55 | + int s = n + n , t = s + 1; |
| 56 | + mcf.addEdge(s, t, n * k, INF); |
| 57 | + |
| 58 | + for(int i = 0; i < n; i++) { |
| 59 | + mcf.addEdge(s, i, k, 0); |
| 60 | + mcf.addEdge(i + n, t, k, 0); |
| 61 | + } |
| 62 | + |
| 63 | + for (int i = 0; i < n; i++) { |
| 64 | + for (int j = 0; j < n; j++) { |
| 65 | + mcf.addEdge(i, j + n, 1, INF - a[i][j]); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + MinCostFlow.FlowAndCost res = mcf.minCostFlow(s, t, n * k); |
| 70 | + long ans = INF * res.flow - res.cost; |
| 71 | + MinCostFlow.WeightedCapEdge[] es = mcf.getEdges(); |
| 72 | + char[][] g = new char[n][n]; |
| 73 | + for (char[] r : g) Arrays.fill(r, '.'); |
| 74 | + for (MinCostFlow.WeightedCapEdge e : es) { |
| 75 | + if (e.flow == 1 && e.from != s && e.to != t) { |
| 76 | + g[e.from][e.to - n] = 'X'; |
| 77 | + } |
| 78 | + } |
| 79 | + return new Answer(ans, g); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * @problem |
| 85 | + * GRL_6 B - Minimum Cost Flow |
| 86 | + * {@see https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B} |
| 87 | + * @submission |
| 88 | + * {@see https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=5281698#1} |
| 89 | + */ |
| 90 | +class GRL_6_B extends Solver { |
| 91 | + public int solve(int n, int m, int f, int[][] edges) { |
| 92 | + MinCostFlow mcf = new MinCostFlow(n); |
| 93 | + for (int[] edge : edges) { |
| 94 | + int u = edge[0]; |
| 95 | + int v = edge[1]; |
| 96 | + int c = edge[2]; |
| 97 | + int d = edge[3]; |
| 98 | + mcf.addEdge(u, v, c, d); |
| 99 | + } |
| 100 | + MinCostFlow.FlowAndCost fc = mcf.minCostFlow(0, n - 1, f); |
| 101 | + return (int) (fc.flow == f ? fc.cost : -1); |
| 102 | + } |
| 103 | + |
| 104 | + public void run() { |
| 105 | + Scanner sc = new Scanner(System.in); |
| 106 | + int v = Integer.parseInt(sc.next()); |
| 107 | + int e = Integer.parseInt(sc.next()); |
| 108 | + int f = Integer.parseInt(sc.next()); |
| 109 | + int[][] edges = new int[e][4]; |
| 110 | + for (int[] edge : edges) { |
| 111 | + Arrays.setAll(edge, i -> Integer.parseInt(sc.next())); |
| 112 | + } |
| 113 | + sc.close(); |
| 114 | + System.out.println(solve(v, e, f, edges)); |
| 115 | + } |
| 116 | +} |
0 commit comments