挑战程序竞赛系列(93):3.6凸包(4)
2017-10-16 15:06
441 查看
挑战程序竞赛系列(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 = 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 ; 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); } } }
相关文章推荐
- 挑战程序竞赛系列(94):3.6凸包(5)
- 挑战程序竞赛系列(90):3.6凸包(1)
- 挑战程序竞赛系列(91):3.6凸包(2)
- 挑战程序竞赛系列(92):3.6凸包(3)
- 挑战程序竞赛系列(33):POJ 2991 Crane
- 挑战程序竞赛系列(62):4.6平面上的分治法(2)
- 挑战程序竞赛系列(87):3.6平面扫描(1)
- 挑战程序竞赛系列(45):4.1Polya 计数定理(1)
- 挑战程序竞赛系列(58):4.6树上的分治法(1)
- 挑战程序竞赛系列(24):3.5最大流与最小割
- 挑战程序竞赛系列(78):4.3 2-SAT(2)
- 挑战程序竞赛系列(44):4.1计数 欧拉函数
- 挑战程序竞赛系列(10):2.4并查集
- 挑战程序竞赛系列(54):4.4 双端队列(1)
- 挑战程序竞赛系列(65):4.7字符串上的动态规划(3)
- 挑战程序竞赛系列(82):4.3 LCA(2)
- 挑战程序竞赛系列(88):3.6平面扫描(2)
- 挑战程序竞赛系列(49):4.2 推理与动态规划算法(2)
- 挑战程序竞赛系列(56):4.4 双端队列(3)
- 挑战程序竞赛系列(7):2.1一往直前!贪心法(区间)