文章
454
粉丝
0
获赞
1
访问
10.4k
n 位维修工在数轴上各有一个初始位置,m 次故障按顺序发生,每次派一名维修工前往故障点,代价为移动距离,求最小总代价(n, m ≤ 600)。
把每次故障看成一个点,同一维修工连续处理两个故障的代价是两点距离,问题变成用 n 条从各自起点出发的路径覆盖全部故障点。
普通完全建边是 O(m^2),按故障坐标排序后用分治(线段树式)插入辅助节点,把边数降到 O((n + m) log)。
每次沿最短路增广,用 SPFA 实现;队列用数组模拟环形双端队列并加 SLF 优化。
坐标可达 1e9,代价与距离都用 64 位整数。
O(增广次数 × 建图规模),n, m ≤ 600。
C:
#include <stdio.h>
#include <stdlib.h>
typedef long long ll;
#define MAXV 40000
#define MAXA 400000
static int head[MAXV], nxt[MAXA], to[MAXA], cap[MAXA], ec = 2;
static ll wcost[MAXA];
static void adde(int u, int v, int c, ll w) {
to[ec] = v; cap[ec] = c; wcost[ec] = w; nxt[ec] = head[u]; head[u] = ec++;
to[ec] = u; cap[ec] = 0; wcost[ec] = -w; nxt[ec] = head[v]; head[v] = ec++;
}
static ll dist[MAXV];
static int inq[MAXV], pre[MAXV], pree[MAXV];
static int V;
static int qb[MAXV + 1], qh, qsz;
static void spfa(int s) {
int i;
for (i = 0; i < V; i++) { dist[i] = (ll)1e18; inq[i] = 0; pre[i] = -1; pree[i] = -1; }
dist[s] = 0; inq[s]...
登录后发布评论
暂无评论,来抢沙发