Data update

This commit is contained in:
Ingy döt Net 2025-06-11 20:16:52 -04:00
parent 72eb4943cb
commit 4d5544505c
2347 changed files with 62432 additions and 16731 deletions

View file

@ -1,9 +1,7 @@
proc floydwarshall w[][] n . .
proc floydwarshall w[][] n .
for i to n
con[][] &= [ ]
for j to n
con[i][] &= 1 / 0
.
for j to n : con[i][] &= 1 / 0
.
for i to len w[][]
con[w[i][1]][w[i][2]] = w[i][3]

View file

@ -0,0 +1,49 @@
include xpllib; \for Print
proc FloydWarshall(Weights, Len, Verts);
int Weights, Len, Verts;
int Dist, Next, I, J, K, W;
proc PrintResult;
int U, V;
[Print("Pair Dist Path\n");
for I:= 0 to Verts-1 do
for J:= 0 to Verts-1 do
if I # J then
[U:= I+1; V:= J+1;
Print("%d -> %d %2d %d", U, V, Dist(I,J), U);
repeat U:= Next(U-1, V-1);
Print(" -> %d", U);
until U = V;
Print("\n");
];
];
[Dist:= Reserve(Verts*IntSize);
for I:= 0 to Verts-1 do
[Dist(I):= Reserve(Verts*IntSize);
for J:= 0 to Verts-1 do
Dist(I,J):= \Inf\10000;
];
for W:= 0 to Len-1 do
Dist(Weights(W,0)-1, Weights(W,1)-1):= Weights(W,2);
Next:= Reserve(Verts*IntSize);
for I:= 0 to Verts-1 do
[Next(I):= Reserve(Verts*IntSize);
for J:= 0 to Verts-1 do
Next(I,J):= if I=J then 0 else J+1;
];
for K:= 0 to Verts-1 do
for I:= 0 to Verts-1 do
for J:= 0 to Verts-1 do
if Dist(I,J) > Dist(I,K) + Dist(K,J) then
[Dist(I,J):= Dist(I,K) + Dist(K,J);
Next(I,J):= Next(I,K);
];
PrintResult;
];
int Weights;
[Weights:= [ [1, 3, -2], [2, 1, 4], [2, 3, 3], [3, 4, 2], [4, 2, -1] ];
FloydWarshall(Weights, 5, 4);
]

View file

@ -0,0 +1,99 @@
// floyd_warshall.zig
const std = @import("std");
const F64_INF = std.math.inf(f64);
fn idx(i: usize, j: usize, n: usize) usize {
return i * n + j;
}
fn printResult(dist: []const f64, next: []const usize, n: usize) void {
std.debug.print("(pair, dist, path)\n", .{});
for (0..n) |i| {
for (0..n) |j| {
if (i == j) continue;
const @"u0" = i + 1;
const v0 = j + 1;
std.debug.print("({d} -> {d}, {d}, ", .{
@"u0", v0, dist[idx(i, j, n)],
});
var u = @"u0";
std.debug.print("{d}", .{u});
while (u != v0) {
u = next[idx(u - 1, v0 - 1, n)];
std.debug.print(" -> {d}", .{u});
}
std.debug.print(")\n", .{});
}
}
}
fn solve(
allocator: std.mem.Allocator,
edges: []const [3]i64, // (u,v,w) 1based
n: usize,
) !void {
// Allocate matrices
var dist = try allocator.alloc(f64, n * n);
defer allocator.free(dist);
var next_v = try allocator.alloc(usize, n * n);
defer allocator.free(next_v);
// Initialise
for (dist)|*d| d.* = F64_INF;
for (next_v)|*x| x.* = 0;
// Edge weights
for (edges) |e| {
const u = @as(usize, @intCast(e[0] - 1));
const v = @as(usize, @intCast(e[1] - 1));
dist[idx(u, v, n)] = @as(f64, @floatFromInt(e[2]));
}
// Successor matrix
for (0..n) |i| {
for (0..n) |j| {
if (i != j) next_v[idx(i, j, n)] = j + 1; // 1based
}
}
// FloydWarshall
for (0..n) |k| {
for (0..n) |i| {
for (0..n) |j| {
const dik = dist[idx(i, k, n)];
const dkj = dist[idx(k, j, n)];
if (std.math.isFinite(dik) and std.math.isFinite(dkj)) {
const alt = dik + dkj;
const pos = idx(i, j, n);
if (alt < dist[pos]) {
dist[pos] = alt;
next_v[pos] = next_v[idx(i, k, n)];
}
}
}
}
}
printResult(dist, next_v, n);
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
const edges = [_][3]i64{
.{ 1, 3, -2 },
.{ 2, 1, 4 },
.{ 2, 3, 3 },
.{ 3, 4, 2 },
.{ 4, 2, -1 },
};
try solve(alloc, edges[0..], 4);
}