結果
問題 | No.957 植林 |
ユーザー | uwi |
提出日時 | 2019-12-20 01:54:18 |
言語 | Java21 (openjdk 21) |
結果 |
RE
|
実行時間 | - |
コード長 | 7,523 bytes |
コンパイル時間 | 3,924 ms |
コンパイル使用メモリ | 92,688 KB |
実行使用メモリ | 114,416 KB |
最終ジャッジ日時 | 2024-07-07 02:48:23 |
合計ジャッジ時間 | 48,528 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 50 ms
50,056 KB |
testcase_01 | AC | 51 ms
50,056 KB |
testcase_02 | AC | 52 ms
50,312 KB |
testcase_03 | AC | 648 ms
89,976 KB |
testcase_04 | AC | 727 ms
88,712 KB |
testcase_05 | AC | 611 ms
91,280 KB |
testcase_06 | AC | 585 ms
89,348 KB |
testcase_07 | AC | 677 ms
89,308 KB |
testcase_08 | AC | 457 ms
89,364 KB |
testcase_09 | AC | 462 ms
89,236 KB |
testcase_10 | AC | 543 ms
89,076 KB |
testcase_11 | AC | 510 ms
89,460 KB |
testcase_12 | AC | 542 ms
89,048 KB |
testcase_13 | AC | 460 ms
82,468 KB |
testcase_14 | AC | 417 ms
89,208 KB |
testcase_15 | AC | 507 ms
89,980 KB |
testcase_16 | AC | 371 ms
88,728 KB |
testcase_17 | AC | 497 ms
88,812 KB |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | RE | - |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | RE | - |
testcase_37 | RE | - |
testcase_38 | RE | - |
testcase_39 | RE | - |
testcase_40 | RE | - |
testcase_41 | AC | 277 ms
87,564 KB |
testcase_42 | AC | 275 ms
87,368 KB |
testcase_43 | AC | 351 ms
88,344 KB |
testcase_44 | AC | 423 ms
88,884 KB |
testcase_45 | AC | 52 ms
50,444 KB |
testcase_46 | AC | 51 ms
50,256 KB |
testcase_47 | AC | 53 ms
50,152 KB |
ソースコード
package adv2019; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; import java.util.Random; public class D20_2 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), m = ni(); int[][] g = new int[n][m]; for(int i = 0;i < n;i++) { g[i] = na(m); } int[] R = na(n); int[] C = na(m); List<Edge> es = new ArrayList<>(); int src = n*m+n+m, sink = src + 1; for(int i = 0;i < n;i++) { for(int j = 0;j < m;j++) { // es.add(new Edge(src, i*m+j, 0L)); es.add(new Edge(i*m+j, sink, g[i][j])); } } for(int i = 0;i < n;i++) { es.add(new Edge(src, n*m+i, R[i])); // es.add(new Edge(n*m+i, sink, 0)); for(int j = 0;j < m;j++) { es.add(new Edge(n*m+i, i*m+j, R[i])); } } for(int i = 0;i < m;i++) { es.add(new Edge(src, n*m+n+i, C[i])); // es.add(new Edge(n*m+n+i, sink, 0)); for(int j = 0;j < n;j++) { es.add(new Edge(n*m+n+i, j*m+i, C[i])); } } long ans = 0; for(int v : R)ans += v; for(int v : C)ans += v; // ans -= maximumFlowDinicNoRec(compileWD(sink+1, es), src, sink); Edge[][] G = compileWD(sink+1, es); ans -= maximumFlowDinic(G, src, sink); out.println(ans); } public static class Edge { public int from, to; public long capacity; public long flow; public Edge complement; public boolean cloned; public Edge(int from, int to, long capacity) { this.from = from; this.to = to; this.capacity = capacity; } } public static void reset(Edge[][] g) { for(Edge[] row : g){ for(Edge e : row){ e.flow = e.cloned ? e.capacity : 0; } } } public static Edge[][] compileWD(int n, List<Edge> edges) { Edge[][] g = new Edge[n][]; // cloning for(int i = edges.size()-1;i >= 0;i--){ Edge origin = edges.get(i); Edge clone = new Edge(origin.to, origin.from, origin.capacity); clone.flow = origin.capacity; clone.complement = origin; clone.cloned = true; origin.complement = clone; edges.add(clone); } int[] p = new int[n]; for(Edge e : edges)p[e.from]++; for(int i = 0;i < n;i++)g[i] = new Edge[p[i]]; for(Edge e : edges)g[e.from][--p[e.from]] = e; return g; } public static Edge[][] compileWU(int n, List<Edge> edges) { Edge[][] g = new Edge[n][]; // cloning for(int i = edges.size()-1;i >= 0;i--){ Edge origin = edges.get(i); Edge clone = new Edge(origin.to, origin.from, origin.capacity*2); origin.flow = origin.capacity; clone.flow = origin.capacity; clone.complement = origin; clone.cloned = true; origin.complement = clone; origin.capacity *= 2; edges.add(clone); } int[] p = new int[n]; for(Edge e : edges)p[e.from]++; for(int i = 0;i < n;i++)g[i] = new Edge[p[i]]; for(Edge e : edges)g[e.from][--p[e.from]] = e; return g; } public static <T> T[] shuffle(T[] a, Random gen){ for(int i = 0, n = a.length;i < n;i++){ int ind = gen.nextInt(n-i)+i; T d = a[i]; a[i] = a[ind]; a[ind] = d; } return a; } public static long maximumFlowDinic(Edge[][] g, int source, int sink) { int n = g.length; int[] d = new int[n]; int[] q = new int[n]; long ret = 0; while(true){ Arrays.fill(d, 999999999); int r = 0; q[r++] = sink; d[sink] = 0; for(int v = 0;v < r;v++){ int cur = q[v]; for(Edge ne : g[cur]){ if(d[ne.to] > d[cur] + 1 && ne.flow > 0) { q[r++] = ne.to; d[ne.to] = d[cur] + 1; } } } if(d[source] == 999999999)break; // tr(d); int[] sp = new int[n]; for(int i = 0;i < n;i++)sp[i] = g[i].length - 1; ret += dfsDinic(d, g, sp, source, sink, Long.MAX_VALUE); } return ret; } static int step = 0; private static long dfsDinic(int[] d, Edge[][] g, int[] sp, int cur, int sink, long min) { if(++step == 10000000)throw new RuntimeException(); if(cur == sink)return min; long left = min; for(int i = sp[cur];i >= 0;i--){ Edge ne = g[cur][i]; if(d[ne.to] == d[cur]-1 && ne.capacity - ne.flow > 0){ long fl = dfsDinic(d, g, sp, ne.to, sink, Math.min(left, ne.capacity - ne.flow)); if(fl > 0){ left -= fl; ne.flow += fl; ne.complement.flow -= fl; if(left == 0){ sp[cur] = i; return min; } } } } sp[cur] = -1; return min-left; } void run() throws Exception { // int n = 400, m = 400; // Random gen = new Random(1114); // StringBuilder sb = new StringBuilder(); // sb.append(n + " "); // sb.append(m + " "); // for (int i = 0; i < n*m+n+m; i++) { // if(i < n*m) { // sb.append(gen.nextInt(5000000) + " "); // }else { // sb.append(gen.nextInt(1000000000) + " "); // } // } // INPUT = sb.toString(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){ // @Override // public void run() { // long s = System.currentTimeMillis(); // solve(); // out.flush(); // if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // } // }; // t.start(); // t.join(); } public static void main(String[] args) throws Exception { new D20_2().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }