結果
問題 |
No.1320 Two Type Min Cost Cycle
|
ユーザー |
![]() |
提出日時 | 2020-12-17 20:58:59 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,566 bytes |
コンパイル時間 | 3,976 ms |
コンパイル使用メモリ | 83,788 KB |
実行使用メモリ | 69,828 KB |
最終ジャッジ日時 | 2024-09-21 08:15:32 |
合計ジャッジ時間 | 7,480 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 6 TLE * 1 -- * 50 |
ソースコード
import java.util.*; public class Main { static boolean[] used; static long min = Long.MAX_VALUE; static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean isArrow = (sc.nextInt() == 1); int n = sc.nextInt(); int m = sc.nextInt(); for (int i = 0; i < n; i++) { graph.add(new HashMap<>()); } for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; int c = sc.nextInt(); graph.get(a).put(b, c); if (!isArrow) { graph.get(b).put(a, c); } } used = new boolean[n]; long[] costs = new long[n]; for (int i = 0; i < n; i++) { if (!used[i]) { Arrays.fill(costs, 0); next(i, 0, 1, costs); } } if (min == Long.MAX_VALUE) { System.out.println(-1); } else { System.out.println(min); } } static void next(int idx, int from, long x, long[] costs) { if (costs[idx] != 0) { min = Math.min(min, x - costs[idx]); return; } costs[idx] = x; used[idx] = true; for (int y : graph.get(idx).keySet()) { if (y == from) { continue; } next(y, idx, x + graph.get(idx).get(y), costs); } costs[idx] = 0; } }