挑战程序竞赛系列(93):3.6凸包(4)

挑战程序竞赛系列(93):3.6凸包(4)

传送门:POJ 3608: Bridge Across Islands

题意:

跨岛大桥:在两个凸包小岛之间造桥,求最小距离?

思路: 旋转卡壳法,两个凸包分别记作P和Q,对于P中的每一条边,计算和Q中的每个点对应的最小面积,在最小面积的情况下,计算两条边之间的最短距离,或者点到直线的最短距离。

一开始以为给定凸包的点是无序,实际上已经按照凸包排好序了,所以拿过来直接旋转卡壳求解即可。

代码如下:

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.StringTokenizer;

public class Main{

    String INPUT = "./data/judge/201710/P3608.txt";

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

    static final int INF = 0x3f3f3f3f;
    static final double EPS = 1e-8;

    class P implements Comparable<P>{

        double x;
        double y;

        P(double x, double y){
            this.x = x;
            this.y = y;
        }

        P sub(P a) {
            return new P(x - a.x, y - a.y);
        }

        double det(P a) {
            return x * a.y - y * a.x;
        }

        @Override
        public int compareTo(P o) {
            return Double.compare(x, o.x) == 0 ? Double.compare(y, o.y) : Double.compare(x, o.x);
        }

        @Override
        public String toString() {
            return x + "," + y;
        }
    }

    int N, M;
    P[] p;
    P[] q;

    double dist(P a, P b) {
        double dx = a.x - b.x;
        double dy = a.y - b.y;
        return Math.sqrt(dx * dx + dy * dy);
    }

    // ab 垂直 ac
    double verticle(P a, P b, P c) {
        return (b.x - a.x) * (c.x - a.x) + (b.y - a.y) * (c.y - a.y);
    }

    double cross(P a, P b, P c) {
        return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
    }

    // 点c到直线ab的最短距离
    double min(P a, P b, P c) {
        if (dist(a, b) < EPS) return dist(b, c);
        if (verticle(a, b, c) < EPS) return dist(a, c);
        if (verticle(b, a, c) < EPS) return dist(b, c);
        return Math.abs(cross(a, b, c) / dist(a, b));
    }

    double min(P a, P b, P c, P d) {
        return Math.min(Math.min(min(a, b, c), min(a, b, d)), Math.min(min(c, d, a), min(c, d, b)));
    }

    double solve(P[] a, P[] b, int n, int m) {

        P[] ps = new P[n + 1];
        P[] qs = new P[m + 1];
        System.arraycopy(a, 0, ps, 0, n);
        System.arraycopy(b, 0, qs, 0, m);

        ps[n] = ps[0];
        qs[m] = qs[0];

        int ymin = 0;
        int ymax = 0;

        for (int i = 0; i < n; ++i) if (ps[i].y < ps[ymin].y) ymin = i;
        for (int i = 0; i < m; ++i) if (qs[i].y > qs[ymax].y) ymax = i;

        double ans = INF;
        double tmp = 0.0;
        for (int i = 0; i < n; ++i) {

            while ((tmp = cross(ps[ymin + 1], qs[ymax + 1], ps[ymin]) - cross(ps[ymin + 1], qs[ymax], ps[ymin])) > EPS) {
                ymax = (ymax + 1) % m;
            }
            if (tmp + EPS < 0) ans = Math.min(ans, min(ps[ymin], ps[ymin + 1], qs[ymax]));
            else ans = Math.min(ans, min(ps[ymin], ps[ymin + 1], qs[ymax], qs[ymax + 1]));
            ymin = (ymin + 1) % n;
        }

        return ans;
    }

    void read() {
        while (true) {
            N = ni();
            M = ni();

            if (N + M == 0) break;

            p = new P[N];
            q = new P[M];

            for (int i = 0; i < N; ++i) {
                p[i] = new P(nd(), nd());
            }

            for (int i = 0; i < M; ++i) {
                q[i] = new P(nd(), nd());
            }

            out.printf("%.5f\n", Math.min(solve(p, q, N, M), solve(q, p, M, N)));

        }
    }

    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);
        }
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏机器学习入门

挑战程序竞赛系列(88):3.6平面扫描(2)

挑战程序竞赛系列(88):3.6平面扫描(2) 传送门:POJ 3168: Barn Expansion 题意: 求不重叠的矩形个数。 当然,题意是说求能够被...

1775
来自专栏机器学习入门

挑战程序竞赛系列(95):3.6数值积分(1)

挑战程序竞赛系列(95):3.6数值积分(1) 传送门:AOJ 1313: Intersection of Two Prisms 题意: 有一个侧棱与Z轴平行...

19210
来自专栏机器学习入门

挑战程序竞赛系列(91):3.6凸包(2)

挑战程序竞赛系列(91):3.6凸包(2) 传送门:POJ 1113: Wall 题意参考hankcs: http://www.hankcs.com/pro...

1916
来自专栏机器学习入门

POJ 刷题系列:2109. Power of Cryptography

题意: 给定n,p,求k,使得kn=pk^n = p 思路: 这不应该放在贪心里啊!!!刷新了我对double的认识,实际上double的表示范围是巨大的...

1765
来自专栏机器学习入门

挑战程序竞赛系列(75):4.3强连通分量分解(2)

挑战程序竞赛系列(75):4.3强连通分量分解(2) 传送门:POJ 3180: The Cow Prom 题意: 求结点大于等于2的强连通分量个数。 直接上...

1886
来自专栏机器学习入门

POJ 刷题系列:2632. Crashing Robots

题意: 在一张地图(A x B)中给出N个robot的位置和初始移动方向,以及M条指令,任意两个robot不能同时移动,问是否存在非法的crash状态(撞墙,...

1958
来自专栏机器学习入门

挑战程序竞赛系列(84):3.6极限情况(1)

挑战程序竞赛系列(84):3.6极限情况(1) 传送门:POJ 1981: Circle and Points 题意: 平面上有N个点,给定半径为1的圆,最多...

1898
来自专栏机器学习入门

POJ 刷题系列:3299. Humidex

POJ 刷题系列:3299. Humidex 传送门:POJ 3299. Humidex 题意: 给定T, D, H中的任意两个数,求另外一个。 思路: ...

21210
来自专栏Python小屋

Python多态原理与示例演示

所谓多态(polymorphism),是指基类的同一个方法在不同派生类对象中具有不同的表现和行为。派生类继承了基类行为和属性之后,还会增加某些特定的行为和属性,...

3248
来自专栏机器学习入门

POJ 刷题系列:2262. Goldbach's Conjecture

POJ 刷题系列:2262. Goldbach’s Conjecture 传送门:POJ 2262. Goldbach’s Conjecture 题意: 给定...

1526

扫码关注云+社区