for (int i = 0; i < n - 1; i ++ ) { int t = -1; // 在还未确定最短路的点中,寻找距离最小的点 for (int j = 1; j <= n; j ++ ) if (!st[j] && (t == -1 || dist[t] > dist[j])) t = j;
publicclassDijkstra{ staticint[][] g = newint[507][507]; staticint[] dist = newint[507]; staticboolean[] st = newboolean[507]; staticint n, m; static { for (int i = 0; i < 507; i++) { Arrays.fill(g[i], 0x3f3f3f3f); } } publicstaticintdijkstra(){ Arrays.fill(dist, 0x3f3f3f3f); dist[1] = 0; for (int i = 0; i < n - 1; i++) { int t = -1; for (int j = 1; j <= n; j++) { if (!st[j] && (t == -1 || dist[t] > dist[j])) { t = j; } }
publicclassHeapDijkstra{ privatestaticfinalint N = 1000010; staticint n; staticint m; staticint[] dist = newint[N]; staticboolean[] st = newboolean[N]; staticint[] h = newint[N]; staticint[] e = newint[N]; staticint[] ne = newint[N]; staticint[] we = newint[N]; staticint idx = 0; static { Arrays.fill(h, -1); } staticvoidadd(int a, int b, int z){ we[idx] = z; e[idx] = b; ne[idx] = h[a]; h[a] = idx++; }
staticintHeapDijkstra(){ Arrays.fill(dist, 0x3f3f3f3f); dist[1] = 0; PriorityQueue<int[]> heap = new PriorityQueue<>((a,b)->{return a[1]-b[1];}); heap.offer(newint[] {1, 0}); while (!heap.isEmpty()) { int[] cur = heap.poll(); int ver = cur[0], distance = cur[1]; if (st[ver]) { continue; } st[ver] = true; for (int i = h[ver]; i != -1; i=ne[i]) { int j = e[i]; if (dist[j] > distance + we[i]) { dist[j] = distance + we[i]; heap.offer(newint[] {j, dist[j]}); } } } if (dist[n] == 0x3f3f3f3f) { return -1; } return dist[n]; }
floyd算法
时间复杂度是 O(n3), n 表示点数
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//初始化: for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) if (i == j) d[i][j] = 0; else d[i][j] = INF;
// 算法结束后,d[a][b]表示a到b的最短距离 voidfloyd() { for (int k = 1; k <= n; k ++ ) for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); }
publicclassFloyd{ privatestaticfinalint N = 211; staticint n, m, k; staticint[][] d = newint[N][N];
publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); k = in.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) { d[i][j] = 0; } else { d[i][j] = 0x3f3f3f3f; } } } while (m-- > 0) { int x = in.nextInt(), y = in.nextInt(), z = in.nextInt(); d[x][y] = Math.min(d[x][y], z); } floyd(); while (k-- > 0) { int x = in.nextInt(), y = in.nextInt(); if (d[x][y] > 0x3f3f3f3f >> 1) { System.out.println("impossible"); } else { System.out.println(d[x][y]); } } }
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]); } } } } }
#include<iostream> #include<cstring> #include<algorithm> usingnamespace std; constint N = 1e3 + 3; int ans = 0x3f3f3f3f; int n, m; int g[N][N]; int rec[N][N]; int d[N][N]; int idx = -1; vector<int> path;
voidget_path(int i, int j){ if (g[i][j] == d[i][j]) { path.push_back(i); return; } get_path(i, rec[i][j]); get_path(rec[i][j], j); }
intmain(){ cin >> n >> m; memset(g, 0x3f, sizeof g); memset(d, 0x3f, sizeof d); for (int i = 0; i < m; i++) { int u, v, l; cin >> u >> v >> l; g[u][v] = g[v][u] = min(g[u][v], l); d[u][v] = d[v][u] = min(d[u][v], l); } for (int k = 1; k <= n; k++) { for (int i = 1; i < k; i++) { for (int j =1; j < i; j++) { if (ans > (longlong)g[i][k] + g[k][j] + d[i][j]) { ans = g[i][k] + g[k][j] + d[i][j]; idx = k; path.clear(); get_path(i, j); path.push_back(j); path.push_back(k); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (d[i][j] > d[i][k] + d[k][j]) { d[i][j] = d[i][k] + d[k][j]; rec[i][j] = k; } } } } if (idx == -1) { printf("No solution.\n"); } else { for (int x : path) { printf("%d ", x); } } return0; }
// 如果第n次迭代仍然会松弛三角不等式,就说明存在一条长度是n+1的最短路径,由抽屉原理,路径中至少存在两个相同的点,说明图中存在负权回路。 for (int i = 0; i < n; i ++ ) { for (int j = 0; j < m; j ++ ) { int a = edges[j].a, b = edges[j].b, w = edges[j].w; if (dist[b] > dist[a] + w) dist[b] = dist[a] + w; } }
if (dist[n] > 0x3f3f3f3f / 2) return-1; return dist[n];
publicclassBellmanFord{ staticclassedge{ int a, b, w; publicedge(int a, int b, int w){ this.a = a; this.b = b; this.w = w; } } staticint n; staticint m; staticint k; privatestaticfinalint N = 507; staticint[] dist = newint[N]; publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); k = in.nextInt(); edge[] edges = new edge[m]; for (int i = 0; i < m; i++) { int x = in.nextInt(), y = in.nextInt(), z = in.nextInt(); edges[i] = new edge(x, y, z); } int t = bellmanFord(edges); if (t > 0x3f3f3f3f / 2) { System.out.println("impossible"); } else { System.out.println(t); } }
publicstaticintbellmanFord(edge[] edges){ Arrays.fill(dist, 0x3f3f3f3f); dist[1] = 0; for (int i = 0; i < k; i++) { int[] copy = Arrays.copyOf(dist,N); for (int j = 0; j < m; j++) { int a = edges[j].a, b = edges[j].b, w = edges[j].w; dist[b] = Math.min(dist[b], copy[a] + w); } } return dist[n]; } }
publicclassSpfa{ staticint n; staticint m; privatestaticfinalint N = 100010; staticint[] h = newint[N]; staticint[] e = newint[N]; staticint[] ne = newint[N]; staticint[] we = newint[N]; staticint idx = 0; staticint[] dist = newint[N]; staticboolean[] st = newboolean[N]; static { Arrays.fill(h, -1); } staticvoidadd(int a, int b, int z){ we[idx] = z; e[idx] = b; ne[idx] = h[a]; h[a] = idx++; }
publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); while (m-- > 0) { int x = in.nextInt(), y = in.nextInt(), z = in.nextInt(); add(x, y, z); } int t = spfa(); if (t > 0x3f3f3f3f / 2) { System.out.println("impossible"); } else { System.out.println(t); } }
publicstaticintspfa(){ Arrays.fill(dist, 0x3f3f3f3f); dist[1] = 0; Queue<Integer> que = new LinkedList<>(); que.offer(1); while (!que.isEmpty()) { int t = que.poll(); st[t] = false; for (int i = h[t]; i != -1; i = ne[i]) { int b = e[i]; if (dist[b] > dist[t] + we[i]) { dist[b] = dist[t] + we[i]; if (!st[b]) { st[b] = true; que.offer(b); } } } } return dist[n]; } }
publicclassSpfaMinWay{ staticint n; staticint m; privatestaticfinalint N = 100010; staticint[] h = newint[N]; staticint[] e = newint[N]; staticint[] ne = newint[N]; staticint[] we = newint[N]; staticint idx = 0; staticint[] dist = newint[N]; staticboolean[] st = newboolean[N]; staticint[] count = newint[N]; static { Arrays.fill(h, -1); } staticvoidadd(int a, int b, int z){ we[idx] = z; e[idx] = b; ne[idx] = h[a]; h[a] = idx++; }
publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); while (m-- > 0) { int x = in.nextInt(), y = in.nextInt(), z = in.nextInt(); add(x, y, z); } if (spfa()) { System.out.println("Yes"); } else { System.out.println("No"); } }
publicstaticbooleanspfa(){ Arrays.fill(dist, 0x3f3f3f3f); Queue<Integer> que = new LinkedList<>(); for (int i = 1; i <= n; i++) { que.offer(i); st[i] = true; } while (!que.isEmpty()) { int t = que.poll(); st[t] = false; for (int i = h[t]; i != -1; i = ne[i]) { int b = e[i]; if (dist[b] > dist[t] + we[i]) { dist[b] = dist[t] + we[i]; count[b] = count[t]+1; if (count[b] >= n) { returntrue; } if (!st[b]) { st[b] = true; que.offer(b); } } } } returnfalse; } }
publicclassPrim{ staticint n,m; privatestaticfinalint N = 510; staticint[][] g = newint[N][N]; staticint res; staticboolean[] st = newboolean[N]; staticint[] dist = newint[N];
publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); for (int i = 1; i <= n; i++) { Arrays.fill(g[i], 0x3f3f3f3f); } while (m-- > 0) { int x = in.nextInt(), y = in.nextInt(), z = in.nextInt(); g[x][y] = g[y][x] = Math.min(g[x][y], z); } int t = prim(); if (t == 0x3f3f3f3f) { System.out.println("impossible"); } else { System.out.println(t); } }
publicstaticintprim(){ Arrays.fill(dist, 0x3f3f3f3f); dist[1] = 0; for (int i = 0; i < n - 1; i++) { int t = -1; for (int j = 1; j <= n; j++) { if (!st[j] && (t == -1 || dist[t] > dist[j])) { t = j; } } st[t] = true; if (dist[t] == 0x3f3f3f3f) { return dist[t]; } res += dist[t]; for (int j = 1; j <= n; j++) { dist[j] = Math.min(dist[j], g[t][j]); } } return res; } }
publicclassMain{ staticint n, m; staticint N = 100010; privatestaticfinalint M = 200010; staticint[] p = newint[N]; staticint[][] edge = newint[M][3]; staticint cnt = 0; staticint res = 0;
publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); for (int i = 0; i < m; i++) { int x = in.nextInt(), y = in.nextInt(), w = in.nextInt(); edge[i] = newint[] {x, y, w}; } int[] ans = kruskal(); if (ans[0] < n - 1) { System.out.println("impossible"); } else { System.out.println(ans[1]); } }
publicstaticint[] kruskal() { Arrays.sort(edge, 0, m, (a, b) -> a[2] - b[2]); for (int i = 1; i <= n; i++) { p[i] = i; } for (int i = 0; i < m; i++) { int a = edge[i][0], b = edge[i][1], w = edge[i][2]; a = find(a); b = find(b); if (a != b) { p[a] = b; cnt++; res += w; } } returnnewint[] {cnt, res}; } }
int n; // n表示点数 int h[N], e[M], ne[M], idx; // 邻接表存储图 int color[N]; // 表示每个点的颜色,-1表示未染色,0表示白色,1表示黑色
// 参数:u表示当前节点,c表示当前点的颜色 booldfs(int u, int c) { color[u] = c; for (int i = h[u]; i != -1; i = ne[i]) { int j = e[i]; if (color[j] == -1) { if (!dfs(j, !c)) returnfalse; } elseif (color[j] == c) returnfalse; }
returntrue; }
boolcheck() { memset(color, -1, sizeof color); bool flag = true; for (int i = 1; i <= n; i ++ ) if (color[i] == -1) if (!dfs(i, 0)) { flag = false; break; } return flag; }
publicclasserfentuRanSe{ staticint n, m; privatestaticfinalint N = 200010; staticint[] e = newint[N]; staticint[] h = newint[N]; staticint[] ne = newint[N]; staticint idx = 0; staticint[] color = newint[N]; static { Arrays.fill(h, -1); Arrays.fill(color, -1); }
staticvoidadd(int a, int b){ e[idx] = b; ne[idx] = h[a]; h[a] = idx++; }
publicstaticbooleandfs(int a, int col){ color[a] = col; for (int i = h[a]; i != -1; i = ne[i]) { int j = e[i]; if (color[j] == -1) { if (!dfs(j, col == 0 ? 1 : 0)) { returnfalse; } } elseif (color[j] == col){ returnfalse; } } returntrue; }
publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); for (int i = 0; i < m; i++) { int x = in.nextInt(), y = in.nextInt(); add(x, y); add(y, x); } boolean flag = true; for (int i = 1; i <= n; i++) { if (color[i]==-1 && !dfs(i, 0)) { flag = false; break; } } if (flag) { System.out.println("Yes"); } else { System.out.println("No"); } } }
publicclassHungarian{ staticint N = 510; staticint M = 100010; staticint n1; staticint n2; staticint m; staticint idx = 0; staticint[] h = newint[N]; staticint[] e = newint[M]; staticint[] ne = newint[M]; staticint[] match = newint[N]; staticboolean[] st = newboolean[N]; static { Arrays.fill(h, -1); }
staticvoidadd(int a, int b){ e[idx] = b; ne[idx] = h[a]; h[a] = idx++; }
staticbooleanfind(int x){ for (int i = h[x]; i != -1; i = ne[i]) { int j = e[i]; if (!st[j]) { st[j] = true; if (match[j] == 0 || find(match[j])) { match[j] = x; returntrue; } } } returnfalse; }
publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); n1 = in.nextInt(); n2 = in.nextInt(); m = in.nextInt(); while (m-- > 0) { int a = in.nextInt(), b = in.nextInt(); add(a, b); } int res = 0; for (int i = 1; i <= n1; i++) { Arrays.fill(st, false); if (find(i)) { res++; } } System.out.println(res); } }