Antenna Placement

2019-05-26 00:18:09 浏览数 (1)

POJ 刷题系列:3020. Antenna Placement

题意:

一个矩形中,有N个城市’*’,现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可以覆盖相邻的两个城市。问至少放置多少个基站才能使得所有的城市都覆盖无线?

思路: 看了discuss,咋有那么多人纠结无向图和有向图的区别。而且我也并没有理解所谓的答案: 顶点数 - 最大匹配数 / 2,说说我的思路吧。首先在二维矩阵中,对邻接城市进行建图时,它一定可以建成二分图,所以这里也没有所谓的无向图和有向图区分,个人认为无向图和有向图做匹配答案是一样的。

此处参考《挑战》P221页。我们实际要求一个图的最小边覆盖,最小边覆盖为: 顶点数-最大匹配数。简单证明下,首先最大匹配数的顶点集为 2 * match,由于是二分图,所以它们每个匹配顶点对彼此之间是不相邻的,那么剩余的顶点也需要有边连接,如果是孤立的顶点,也要建天线,所以是孤立点就 1。此外,从最大匹配数的顶点集出发,连一条边到其余顶点,而这个数刚好是:所有顶点数 - 2 * 最大匹配数 匹配数。

代码如下:

代码语言:javascript复制
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class Main{

    String INPUT = "./data/judge/201801/P3020.txt";

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

    boolean[][] map;
    int[] match;

    boolean dfs(int v, boolean[] vis, int num) {
        vis[v] = true;
        for (int to = 0; to < num;   to) {
            if (map[v][to]) {
                int w = match[to];
                if (w == -1 || !vis[w] && dfs(w, vis, num)) {
                    match[v] = to;
                    match[to] = v;
                    return true;
                }
            }
        }
        return false;
    }

    int bipatite_match(int V) {
        match = new int[V];
        Arrays.fill(match, -1);
        int res = 0;
        for (int i = 0; i < V;   i) {
            if (match[i] < 0) {
                if (dfs(i, new boolean[V], V))
                    res   ;
            }
        }
        return res;
    }

    void read() {
        int T = ni();
        while (T --> 0) {
            int n = ni();
            int m = ni();
            char[][] board = new char[n][m];
            List<int[]> star = new ArrayList<int[]>();
            for (int i = 0; i < n;   i) {
                String ns = ns();
                for (int j = 0; j < m;   j) {
                    board[i][j] = ns.charAt(j);
                    if (board[i][j] == '*') star.add(new int[] {i, j});
                }
            }

            int tot = star.size();

            // build map
            map = new boolean[n * m][n * m];

            boolean[][] vis = new boolean[n][m];
            int[][] dir = {{1, 0},{-1, 0},{0, 1},{0, -1}};
            for (int[] now : star) {
                int i = now[0];
                int j = now[1];
                if (!vis[i][j]) {
                    for (int[] d : dir) {
                        int ni = i   d[0];
                        int nj = j   d[1];
                        if (ni >= 0 && ni < n && nj >= 0 && nj < m && board[ni][nj] == '*' && !vis[ni][nj]) {
                            map[ni * m   nj][i * m   j] = true;
                            map[i * m   j][ni * m   nj] = true;
                            vis[i][j] = true;
                        }
                    }
                }
            }
            out.println(tot - (bipatite_match(n * m)));
        }
    }

    FastScanner in;
    PrintWriter out;

    void run() throws IOException {
        boolean oj;
        try {
            oj = ! System.getProperty("user.dir").equals("F:\oxygen_workspace\Algorithm");
        } catch (Exception e) {
            oj = System.getProperty("ONLINE_JUDGE") != null;
        }

        InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
        in = new FastScanner(is);
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        read();
        out.flush();
        if (!oj){
            System.out.println("["   (System.currentTimeMillis() - s)   "ms]");
        }
    }

    public boolean more(){
        return in.hasNext();
    }

    public int ni(){
        return in.nextInt();
    }

    public long nl(){
        return in.nextLong();
    }

    public double nd(){
        return in.nextDouble();
    }

    public String ns(){
        return in.nextString();
    }

    public char nc(){
        return in.nextChar();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        boolean hasNext;

        public FastScanner(InputStream is) throws IOException {
            br = new BufferedReader(new InputStreamReader(is));
            hasNext = true;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    hasNext = false;
                    return "##";
                }
            }
            return st.nextToken();
        }

        String next = null;
        public boolean hasNext(){
            next = nextToken();
            return hasNext;
        }

        public int nextInt() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Integer.parseInt(more);
        }

        public long nextLong() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Long.parseLong(more);
        }

        public double nextDouble() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Double.parseDouble(more);
        }

        public String nextString(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more;
        }

        public char nextChar(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more.charAt(0);
        }
    }

    static class D{

        public static void pp(int[][] board, int row, int col) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < row;   i) {
                for (int j = 0; j < col;   j) {
                    sb.append(board[i][j]   (j   1 == col ? "n" : " "));
                }
            }
            System.out.println(sb.toString());
        }

        public static void pp(char[][] board, int row, int col) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < row;   i) {
                for (int j = 0; j < col;   j) {
                    sb.append(board[i][j]   (j   1 == col ? "n" : " "));
                }
            }
            System.out.println(sb.toString());
        }
    }

    static class ArrayUtils {

        public static void fill(int[][] f, int value) {
            for (int i = 0; i < f.length;   i) {
                Arrays.fill(f[i], value);
            }
        }

        public static void fill(int[][][] f, int value) {
            for (int i = 0; i < f.length;   i) {
                fill(f[i], value);
            }
        }

        public static void fill(int[][][][] f, int value) {
            for (int i = 0; i < f.length;   i) {
                fill(f[i], value);
            }
        }
    }

    static class Num{
        public static <K> void inc(Map<K, Integer> mem, K k) {
            if (!mem.containsKey(k)) mem.put(k, 0);
            mem.put(k, mem.get(k)   1);
        }
    }
}

0 人点赞