这道题不妨借用一下动态规划的思路,把一个点拆成k+1(max是20)个点来做,对于所有的点ui分别和vi与vi+1相连。
详见代码:
#include <iostream>
const int maxn = 10000;
const int maxm = 100000;
const int INF = maxn * 100000;
int n, m, k, s;
int p[maxn+1], q[maxn+1], d[maxn+1], old[maxn+1], first[maxn+1];
int next[maxm+1], tail[maxm+1], t[maxm+1];
void swap(int i, int j) {
int u = q[i];
int v = q[j];
p[u] = j; p[v] = i;
q[i] = v; q[j] = u;
}
void up(int i) {
int f = i / 2;
if (f > 0 && d[q[f]] > d[q[i]]) {
swap(f, i);
up(f);
}
}
void down(int i) {
int c = i + i;
if (c + 1 <= s && d[q[c+1]] < d[q[c]])
c++;
if (c <= s && d[q[c]] < d[q[i]]) {
swap(i, c);
down(c);
}
}
int pop(void) {
swap(1, s);
s--;
down(1);
return q[s&#