Submission #2558360


Source Code Expand

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    int h, w;
    char[][] css;

    public static void main(String args[]) {
        new Main().run();
    }

    void run() {
        FastReader sc = new FastReader();
        h = sc.nextInt();
        w = sc.nextInt();
        css = new char[h][w];
        int sx = -1;
        int sy = -1;
        int tx = -2;
        int ty = -2;

        Dinic d = new Dinic(2 + h + w);
        for (int i = 0; i < h; i++) {
            css[i] = sc.next().toCharArray();
            for (int j = 0; j < w; j++) {
                if (css[i][j] == 'o') {
                    d.addUndirectedEdge(2 + i, 2 + h + j, 1);
                } else if (css[i][j] == 'S') {
                    d.addEdge(0, 2 + i, Integer.MAX_VALUE);
                    d.addEdge(0, 2 + h + j, Integer.MAX_VALUE);
                    sx = j;
                    sy = i;
                } else if (css[i][j] == 'T') {
                    d.addEdge(2 + i, 1, Integer.MAX_VALUE);
                    d.addEdge(2 + h + j, 1, Integer.MAX_VALUE);
                    tx = j;
                    ty = i;
                }
            }
        }
        if (sx == tx || sy == ty) {
            System.out.println(-1);
            return;
        }
        System.out.println(d.maximumFlow(0, 1));
    }

    public class Dinic {
        ArrayList<ArrayList<DinicEdge>> graph;
        // distances from s
        int[] levels;
        int[] iters;

        Dinic(int n) {
            graph = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                graph.add(new ArrayList<>());
            }
            levels = new int[n];
            iters = new int[n];
            // manually add edges by using addEdge
        }

        void bfs(int s) {
            Arrays.fill(levels, -1);
            Queue<Integer> queue = new LinkedList<>();
            levels[s] = 0;
            queue.offer(s);
            while (!queue.isEmpty()) {
                int v = queue.poll();
                for (DinicEdge e : graph.get(v)) {
                    if (e.capacity > 0 && levels[e.to] < 0) {
                        levels[e.to] = levels[v] + 1;
                        queue.offer(e.to);
                    }
                }
            }
        }

        long dfs(int v, int t, long f) {
            if (v == t) {
                return f;
            }
            for (int i = iters[v]; i < graph.get(v).size(); i++) {
                DinicEdge e = graph.get(v).get(i);
                if (e.capacity > 0 && levels[v] < levels[e.to]) {
                    long d = dfs(e.to, t, Math.min(f, e.capacity));
                    if (d > 0) {
                        e.capacity -= d;
                        graph.get(e.to).get(e.reverse).capacity += d;
                        return d;
                    }
                }
            }
            return 0;
        }

        long maximumFlow(int s, int t) {
            long flow = 0;
            while (true) {
                bfs(s);
                if (levels[t] < 0) {
                    return flow;
                }
                Arrays.fill(iters, 0);
                long f;
                while ((f = dfs(s, t, Long.MAX_VALUE)) > 0) {
                    flow += f;
                }
            }
        }

        void addUndirectedEdge(int from, int to, int capacity) {
            graph.get(from).add(new DinicEdge(to, capacity, graph.get(to).size()));
            graph.get(to).add(new DinicEdge(from, capacity, graph.get(from).size() - 1));
        }

        void addEdge(int from, int to, int capacity) {
            graph.get(from).add(new DinicEdge(to, capacity, graph.get(to).size()));
            graph.get(to).add(new DinicEdge(from, 0, graph.get(from).size() - 1));
        }
    }

    class Pair {
        int x;
        int y;

        Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int hashCode() {
            return x << 16 + y;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof Pair) {
                Pair p = (Pair)obj;
                return this.x == p.x && this.y == p.y;
            } else {
                return false;
            }
        }
    }

    class DinicEdge {
        int to;
        int capacity;
        int reverse;

        DinicEdge(int to, int capacity, int reverse) {
            this.to = to;
            this.capacity = capacity;
            this.reverse = reverse;
        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}

Submission Info

Submission Time
Task F - Lotus Leaves
User ynish
Language Java8 (OpenJDK 1.8.0)
Score 800
Code Size 5999 Byte
Status AC
Exec Time 134 ms
Memory 24764 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 4
AC × 54
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt
Case Name Status Exec Time Memory
0_00.txt AC 72 ms 18644 KB
0_01.txt AC 71 ms 20436 KB
0_02.txt AC 71 ms 21332 KB
0_03.txt AC 73 ms 19412 KB
1_00.txt AC 82 ms 20436 KB
1_01.txt AC 71 ms 19284 KB
1_02.txt AC 72 ms 19412 KB
1_03.txt AC 73 ms 18388 KB
1_04.txt AC 76 ms 19540 KB
1_05.txt AC 81 ms 19156 KB
1_06.txt AC 108 ms 21028 KB
1_07.txt AC 75 ms 19924 KB
1_08.txt AC 80 ms 19156 KB
1_09.txt AC 85 ms 19156 KB
1_10.txt AC 103 ms 18628 KB
1_11.txt AC 105 ms 22420 KB
1_12.txt AC 87 ms 20820 KB
1_13.txt AC 123 ms 22776 KB
1_14.txt AC 113 ms 20372 KB
1_15.txt AC 103 ms 19536 KB
1_16.txt AC 102 ms 19832 KB
1_17.txt AC 79 ms 18132 KB
1_18.txt AC 100 ms 22312 KB
1_19.txt AC 122 ms 19688 KB
1_20.txt AC 100 ms 19028 KB
1_21.txt AC 85 ms 21588 KB
1_22.txt AC 103 ms 21060 KB
1_23.txt AC 117 ms 21252 KB
1_24.txt AC 89 ms 18772 KB
1_25.txt AC 80 ms 21204 KB
1_26.txt AC 82 ms 19284 KB
1_27.txt AC 94 ms 24764 KB
1_28.txt AC 98 ms 18808 KB
1_29.txt AC 112 ms 22936 KB
1_30.txt AC 78 ms 20180 KB
1_31.txt AC 107 ms 20552 KB
1_32.txt AC 134 ms 21272 KB
1_33.txt AC 78 ms 20820 KB
1_34.txt AC 103 ms 21308 KB
1_35.txt AC 132 ms 20700 KB
1_36.txt AC 116 ms 19596 KB
1_37.txt AC 115 ms 20888 KB
1_38.txt AC 82 ms 18260 KB
1_39.txt AC 119 ms 21528 KB
1_40.txt AC 78 ms 21844 KB
1_41.txt AC 90 ms 19796 KB
1_42.txt AC 74 ms 18900 KB
1_43.txt AC 88 ms 22100 KB
1_44.txt AC 72 ms 19412 KB
1_45.txt AC 79 ms 21460 KB
1_46.txt AC 76 ms 18260 KB
1_47.txt AC 75 ms 21204 KB
1_48.txt AC 88 ms 21588 KB
1_49.txt AC 79 ms 18900 KB