1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public static void solve() { int n = io.nextInt(), m = io.nextInt(); List<int[]>[] g = new List[n]; Arrays.setAll(g, k -> new ArrayList<>()); for (int i = 0; i < m; i++) { int a = io.nextInt() - 1, b = io.nextInt() - 1, x = io.nextInt(), y = io.nextInt(); g[a].add(new int[]{b, x, y}); g[b].add(new int[]{a, -x, -y}); } long[] X = new long[n]; long[] Y = new long[n]; Arrays.fill(X, Long.MAX_VALUE); Arrays.fill(Y, Long.MAX_VALUE); boolean[] vis = new boolean[n]; dfs(0, vis, g, 0, 0, X, Y); for (int i = 0; i < n; i++) { if (X[i] != Long.MAX_VALUE && Y[i] != Long.MAX_VALUE) { io.println(X[i] + " " + Y[i]); } else { io.println("undecidable"); } } }
private static void dfs(int i, boolean[] vis, List<int[]>[] g, long x, long y, long[] X, long[] Y) { X[i] = x; Y[i] = y; vis[i] = true; for (int[] t : g[i]) { int j = t[0]; if (vis[j]) continue; long nx = t[1] + x, ny = t[2] + y; dfs(j, vis, g, nx, ny, X, Y); } }
|