publicvoidquickSort(int[] nums, int left, int right){ if (left >= right) return; int i = left - 1, j = right + 1, x = nums[left]; while (i < j) { do i++; while (nums[i] < x); do j--; while (nums[j] > x); if (i < j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } quickSort(nums, left, j); quickSort(nums, j + 1, right); }
publicintbinarySearchLeft(int[] nums, int target){ int left = 0, right = nums.length - 1; while (left < right) { int mid = left + (right - left) / 2; if (check(mid)) right = mid; else left = mid + 1; } return nums[left]; }
publicintbinarySearchRight(int[] nums, int target){ int left = 0, right = nums.length - 1; while (left < right) { int mid = left + (right - left) / 2 + 1; if (check(mid)) left = mid; else right = mid - 1; } return nums[left]; }
publicintkmp(String text, String pattern){ int m = pattern.length(); if (m == 0) return0; int n = text.length(); int[] next = newint[m + 1]; for (int i = 2, j = 0; i <= m; i++) { while (j > 0 && pattern.charAt(i - 1) != pattern.charAt(j)) j = next[j]; if (pattern.charAt(i - 1) == pattern.charAt(j)) j++; next[i] = j; } for (int i = 1, j = 0; i <= n; i++) { while (j > 0 && text.charAt(i - 1) != pattern.charAt(j)) j = next[j]; if (text.charAt(i - 1) == pattern.charAt(j)) j++; if (j == m) return i - m; } return -1; }
publicstaticvoidmain(String[] args){ Scanner scan = new Scanner(System.in); n = scan.nextInt(); int m = scan.nextInt(); g = newint[n + 1][n + 1]; dist = newint[n + 1]; visit = newboolean[n + 1]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (i == j) g[i][j] = 0; else g[i][j] = INF; for (int i = 0; i < m; i++) { int x = scan.nextInt(), y = scan.nextInt(), z = scan.nextInt(); g[a][b] = Math.min(g[a][b], c); } int res = dijkstra(); System.out.println(res); }
publicstaticclassNode{ publicint node; publicint length; publicNode(int node, int length){ this.node = node; this.length = length; } } publicstaticvoidmain(String[] args){ Scanner scan = new Scanner(System.in); n = scan.nextInt(); dist = newint[n + 1]; visit = newboolean[n + 1]; int m = scan.nextInt(); for (int i = 1; i <= n; i++) map.put(i, new ArrayList<>()); for (int i = 0; i < m; i++) { int x = scan.nextInt(), y = scan.nextInt(), z = scan.nextInt(); map.get(x).add(new Node(y, z)); } int res = dijkstra(); System.out.println(res); }
}
Dijkstra 边权不能是负数! 1.dist[1] = 0, dist[i] = +∞ 2.for i 1 ~ n t <- 不在s中的,距离最近的点 – n2n2 / n s <- t – n 用t更新其他点的距离 – m / mlogn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicstaticintdijkstra(){ for (int i = 1; i <= n; i++) dist[i] = INF; dist[1] = 0; for (int i = 0; i < n - 1; i++) { int t = -1; for (int j = 1; j <= n; j++) if (!visit[j] && (t == -1 || dist[t] > dist[j])) t = j; if (t == n) break; for (int j = 1; j <= n; j++) dist[j] = Math.min(dist[j], dist[t] + g[t][j]); visit[t] = true; } if (dist[n] == INF) return -1; return dist[n]; }
publicstaticintdijkstra(){ for (int i = 1; i <= n; i++) dist[i] = INF; dist[1] = 0; PriorityQueue<Node> heap = new PriorityQueue<>((node1, node2) -> node1.length - node2.length); heap.add(new Node(1, 0)); while (!heap.isEmpty()) { Node top = heap.poll(); int length = top.length, cur = top.node; if (visit[cur]) continue; visit[cur] = true; for (Node next: map.get(cur)) { int node = next.node, cost = next.length; if (dist[node] > length + cost) { dist[node] = length + cost; heap.add(new Node(node, dist[node])); } } } if (dist[n] == INF) return -1; return dist[n]; }
例题:AcWing 850
Bellman-ford O(nm)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicstaticintbellman_ford(){ for (int i = 1; i <= n; i++) dist[i] = INF; dist[1] = 0; for (int i = 0; i < k; i++) { for (int j = 1; j <= n; j++) backup[j] = dist[j]; // deep copy for (int k = 0; k < m; k++) { int x = edges[k].x; int y = edges[k].y; int z = edges[k].z; dist[y] = Math.min(dist[y], backup[x] + z); } } if (dist[n] > INF / 2) return -1; else return dist[n]; }
publicstaticbooleanspfa(){ Queue<Integer> queue = new LinkedList<>(); for (int i = 1; i <= n; i++) { queue.add(i); visit[i] = true; } while (!queue.isEmpty()) { int t = queue.poll(); visit[t] = false; for (Node cur: map.get(t)) { int node = cur.node, length = cur.length; if (dist[node] > dist[t] + length) { dist[node] = dist[t] + length; count[node] = count[t] + 1; if (count[node] >= n) returntrue; if (!visit[node]) { queue.add(node); visit[node] = true; } } } } returnfalse; }
例题:AcWing 852
Floyd O(n3)O(n3)
1 2 3 4 5 6 7
publicstaticvoidfloyd(){ for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]); }