makeGraph = (cost, min, max) => {
const sources = [];
const targets = [];
const costs = [];
for (let x = 0; x < W; ++x) {
for (let y = 0; y < W; ++y) {
for (let z = 0; z <= 1; ++z) {
for (let dir = -1; dir <= 1; dir += 2) {
let c = 0;
let x0 = x;
let y0 = y;
for (let i = 1; i <= max; ++i) {
if (z) {
y0 += dir;
if (y0 < 0 || y0 >= W) break;
} else {
x0 += dir;
if (x0 < 0 || x0 >= W) break;
}
c += cost[x0 + y0 * W];
if (i >= min) {
sources.push(((x + W * y) << 1) + z);
targets.push(((x0 + W * y0) << 1) + (1 - z));
costs.push(c);
}
}
}
}
}
}
return { sources, targets, costs };
}