結果
問題 | No.2376 障害物競プロ |
ユーザー |
![]() |
提出日時 | 2023-07-07 23:05:42 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,013 ms / 4,000 ms |
コード長 | 3,079 bytes |
コンパイル時間 | 3,298 ms |
コンパイル使用メモリ | 77,284 KB |
実行使用メモリ | 52,484 KB |
最終ジャッジ日時 | 2024-07-21 19:30:38 |
合計ジャッジ時間 | 74,464 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;public class Main {public static void main(String[] args) throws Exception {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String[] sa = br.readLine().split(" ");int n = Integer.parseInt(sa[0]);int m = Integer.parseInt(sa[1]);int n2 = n * 2;Point[] arr = new Point[n2];for (int i = 0; i < n; i++) {sa = br.readLine().split(" ");Point p1 = new Point();p1.x = Integer.parseInt(sa[0]);p1.y = Integer.parseInt(sa[1]);Point p2 = new Point();p2.x = Integer.parseInt(sa[2]);p2.y = Integer.parseInt(sa[3]);arr[i] = p1;arr[i + n] = p2;}int[] a = new int[m];int[] b = new int[m];int[] c = new int[m];int[] d = new int[m];for (int i = 0; i < m; i++) {sa = br.readLine().split(" ");a[i] = Integer.parseInt(sa[0]) - 1;b[i] = Integer.parseInt(sa[1]);c[i] = Integer.parseInt(sa[2]) - 1;d[i] = Integer.parseInt(sa[3]);}br.close();double inf = 1e18;double[][] dist = new double[n2][n2];for (int i = 0; i < n2; i++) {for (int j = i + 1; j < n2; j++) {boolean flg = true;for (int k = 0; k < n; k++) {if (i % n != k && j % n != k) {if (cross(arr[i], arr[j], arr[k], arr[k + n])) {flg = false;break;}}}if (flg) {dist[i][j] = Math.hypot(arr[i].x - arr[j].x, arr[i].y - arr[j].y);} else {dist[i][j] = inf;}dist[j][i] = dist[i][j];}}for (int k = 0; k < n2; k++) {for (int i = 0; i < n2; i++) {for (int j = 0; j < n2; j++) {if (dist[i][k] != inf && dist[k][j] != inf) {dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);}}}}PrintWriter pw = new PrintWriter(System.out);for (int i = 0; i < m; i++) {int p1 = a[i];if (b[i] == 2) {p1 += n;}int p2 = c[i];if (d[i] == 2) {p2 += n;}pw.println(dist[p1][p2]);}pw.flush();}static boolean cross(Point a, Point b, Point c, Point d) {// AB-Clong s1 = (long) (a.x - b.x) * (c.y - a.y) - (long) (a.y - b.y) * (c.x - a.x);// AB-Dlong t1 = (long) (a.x - b.x) * (d.y - a.y) - (long) (a.y - b.y) * (d.x - a.x);if (s1 > 0 && t1 > 0 || s1 < 0 && t1 < 0) {return false;}// CD-Along s2 = (long) (c.x - d.x) * (a.y - c.y) - (long) (c.y - d.y) * (a.x - c.x);// CD-Blong t2 = (long) (c.x - d.x) * (b.y - c.y) - (long) (c.y - d.y) * (b.x - c.x);if (s2 > 0 && t2 > 0 || s2 < 0 && t2 < 0) {return false;}if (s1 == 0 && t1 == 0 && s2 == 0 && t2 == 0) {int ax = Math.min(a.x, b.x);int bx = Math.max(a.x, b.x);int ay = Math.min(a.y, b.y);int by = Math.max(a.y, b.y);int cx = Math.min(c.x, d.x);int dx = Math.max(c.x, d.x);int cy = Math.min(c.y, d.y);int dy = Math.max(c.y, d.y);// D < A || B < Cif (dx < ax || dy < ay || bx < cx || by < cy) {return false;}return true;} else {return true;}}static class Point {int x, y;}}