题意
给出一个有 n 个顶点的完全图,现在要删除 k 条边,使得删完边之后,从顶点 1 到达顶点 n 的最短路最长,输出这个距离。
(3≤n≤50,1≤k≤min(n−2,5))
题解
比赛的时候把 k 看成50了。。。。
代码
#include <bits/stdc++.h>
#define fuck system("pause")
#define emplace_back push_back
#define pb push_back
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int N = 55;
int g[N][N], dis[N], vis[N], back[N], ans, n, k;
void dfs(int cnt)
{
int tmp[N];
for (int i = 1; i <= n; i++) {
tmp[i] = vis[i] = 0, dis[i] = inf;
}
dis[1] = 0;
for (int i = 1; i <= n; i++) {
int u, minn = inf;
for (int j = 1; j <= n; j++) {
if (!vis[j] && dis[j] < minn) {
minn = dis[j];
u = j;
}
}
vis[u] = 1;
for (int j = 1; j <= n; j++) {
if (!vis[j] && dis[u] + g[u][j] < dis[j]) {
tmp[j] = u;
dis[j] = dis[u] + g[u][j];
}
}
}
if (!cnt) {
if (dis[n] != inf)
ans = max(ans, dis[n]);
return;
}
int now = n;
while (tmp[now]) {//tmp数组一定要在函数里开,否则进行dfs回溯的时候数组发生变化
int temp = g[tmp[now]][now];
g[tmp[now]][now] = g[now][tmp[now]] = inf;
dfs(cnt - 1);
g[tmp[now]][now] = g[now][tmp[now]] = temp;
now = tmp[now];
}
}
int main()
{
int T;
scanf("%d", &T);
while (T--) {
ans = 0;
memset(g, inf, sizeof(g));
scanf("%d%d", &n, &k);
for (int i = 1; i <= (n - 1) * n / 2; i++) {
int x, y, v;
scanf("%d%d%d", &x, &y, &v);
g[x][y] = g[y][x] = v;
}
dfs(k);
printf("%d
", ans);
}
// fuck;
return 0;
}
/*
*/