結果
問題 | No.2955 Pizza Delivery Plan |
ユーザー | tenten |
提出日時 | 2024-11-11 10:55:08 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 162 ms / 2,000 ms |
コード長 | 3,328 bytes |
コンパイル時間 | 4,842 ms |
コンパイル使用メモリ | 79,224 KB |
実行使用メモリ | 41,848 KB |
最終ジャッジ日時 | 2024-11-11 10:55:18 |
合計ジャッジ時間 | 8,106 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
37,704 KB |
testcase_01 | AC | 49 ms
37,492 KB |
testcase_02 | AC | 49 ms
37,348 KB |
testcase_03 | AC | 46 ms
37,656 KB |
testcase_04 | AC | 47 ms
37,528 KB |
testcase_05 | AC | 47 ms
37,444 KB |
testcase_06 | AC | 47 ms
37,536 KB |
testcase_07 | AC | 46 ms
37,344 KB |
testcase_08 | AC | 114 ms
40,924 KB |
testcase_09 | AC | 106 ms
40,812 KB |
testcase_10 | AC | 104 ms
40,808 KB |
testcase_11 | AC | 104 ms
41,036 KB |
testcase_12 | AC | 107 ms
40,932 KB |
testcase_13 | AC | 147 ms
41,516 KB |
testcase_14 | AC | 153 ms
41,580 KB |
testcase_15 | AC | 162 ms
41,680 KB |
testcase_16 | AC | 146 ms
41,432 KB |
testcase_17 | AC | 111 ms
41,416 KB |
testcase_18 | AC | 102 ms
41,504 KB |
testcase_19 | AC | 156 ms
41,736 KB |
testcase_20 | AC | 131 ms
41,848 KB |
testcase_21 | AC | 118 ms
41,688 KB |
testcase_22 | AC | 111 ms
41,728 KB |
testcase_23 | AC | 115 ms
41,624 KB |
testcase_24 | AC | 94 ms
41,500 KB |
testcase_25 | AC | 108 ms
40,728 KB |
testcase_26 | AC | 108 ms
41,024 KB |
testcase_27 | AC | 109 ms
40,812 KB |
testcase_28 | AC | 103 ms
41,548 KB |
testcase_29 | AC | 129 ms
41,572 KB |
testcase_30 | AC | 93 ms
41,512 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static double[] dp; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int k = sc.nextInt(); Point[] points = new Point[n]; for (int i = 0; i < n; i++) { points[i] = new Point(sc.nextInt(), sc.nextInt()); } double[][] matrix = new double[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i][j] = points[i].getDistance(points[j]); } } dp = new double[1 << n]; Arrays.fill(dp, Double.MAX_VALUE); dp[0] = 0; double[][] routes = new double[n][1 << n]; for (int i = 1; i < (1 << n); i++) { int count = Integer.bitCount(i); if (count > k) { continue; } for (int j = 0; j < n; j++) { if ((i & (1 << j)) == 0) { continue; } if (count == 1) { routes[j][i] = points[j].getFromBase(); } else { routes[j][i] = Double.MAX_VALUE; for (int a = 0; a < n; a++) { if (a == j || (i & (1 << a)) == 0) { continue; } routes[j][i] = Math.min(routes[j][i], routes[a][i ^ (1 << j)] + matrix[a][j]); } } dp[i] = Math.min(dp[i], routes[j][i] + points[j].getFromBase()); } } System.out.println(new java.math.BigDecimal(dfw((1 << n) - 1)).toPlainString()); } static double dfw(int mask) { if (dp[mask] == Double.MAX_VALUE) { for (int x = ((mask - 1) & mask); x > 0; x = ((x - 1) & mask)) { dp[mask] = Math.min(dp[mask], dfw(x) + dfw(mask ^ x)); } } return dp[mask]; } static class Point { double x; double y; public Point(double x, double y) { this.x = x; this.y = y; } public double getDistance(Point p) { return Math.sqrt(Math.pow(x - p.x, 2) + Math.pow(y - p.y, 2)); } public double getFromBase() { return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }