1. 程式人生 > 實用技巧 >HDU 6797 Tokitsukaze and Rescue

HDU 6797 Tokitsukaze and Rescue

Princess CJB has lived almost her entire life in the isolated town of Ertona, where CJB uses her unique ability to recognize where crystals of materials are buried. By way of a fateful encounter, CJB learns of the Alchemy Exam and decides to take her first step into the outside world, setting off on a grand journey to become a certified alchemist and discover the mysteries that life has to offer!


In order to take part in the Alchemy Exam, CJB goes to the Reisenberg town without any partners. But the kingdom Adalet is unbelievably enormous so that there are many hidden risks. Claris, a powerful evil magician, wants to monopolize CJB for the extraordinary beauty of her. Due to the power limitation of CJB, she can't escape from Claris without any assistance. The alchemist Tokitsukaze has heard this savage act and wants to rescue the princess CJB.

There arencities numbered from1tonin the kingdom Adalet. Because of the excellent transportation, there is exactly a two-way road between any two cites. Tokitsukaze lives in city1. The Magician Claris lives in cityn. Since the exam will be held soon, Tokitsukaze wants to rescue CJB as fast as possible, so she will choose the shortest path to reach cityn
.

Claris has also heard this news and is afraid of being punished, so he decides to slow Tokitsukaze down by making an explosion onkroads he chose and causing these roads to lose their capability of two-way transportation, since it can pave the way for having enough time to prepare his powerful magic against Tokitsukaze.

Tokitsukaze knows some roads will be destroyed and can immediately recognize where they are, but she has no approach to prevent this explosion, so she chooses just to move along the shortest path after Claris completes his explosion.

Now Claris wants to know, after finishing his explosion, what the longest possible length is of the shortest path from city1to cityn.


Input There are several test cases.

The first line contains an integerT(1T100), denoting the number of test cases. Then follow all the test cases.

For each test case, the first line contains two integersnandk(3n50,1kmin(n2,5)), denoting the number of cities and the number of roads being exploded, respectively.

The nextn(n1)2lines describe all the roads, where each line contains three integersu,vandw(1u,vn,uv,1w104), representing a two-way road of lengthwbetween cityuand cityv. It is guaranteed that for every two cities, there exists exactly one road whose length is randomly distributed between1and104.


Output For each case, output in one line an integer, denoting the longest possible length of the shortest path after the explosion.


Sample Input 3 5 1 1 2 2990 1 3 2414 1 4 4018 1 5 6216 2 3 9140 2 4 4169 2 5 550 3 4 6618 3 5 3206 4 5 105 5 2 1 2 2990 1 3 2414 1 4 4018 1 5 6216 2 3 9140 2 4 4169 2 5 550 3 4 6618 3 5 3206 4 5 105 5 3 1 2 2990 1 3 2414 1 4 4018 1 5 6216 2 3 9140 2 4 4169 2 5 550 3 4 6618 3 5 3206 4 5 105


Sample Output 4123 5620 6216


Source 2020 Multi-University Training Contest 3


Recommend liuyiding|We have carefully selected several similar problems for you:68016800679967986797 題意:在n個點的無向完全圖中,刪除k條邊,使得最短路最長。 比賽想著如果刪除1條邊的話,是刪除最短路徑上的一條邊,然後求最短路中最大的那個,以為這個刪除k條邊後,這樣繼續的話正確性欠佳,就沒做了,誰知題解就是這麼做,暴力DFS刪除最短路徑上的一條邊,化為刪除k-1條邊的子問題。
//#include <bits/stdc++.h>
 #include <iostream>
 #include <cstring>
 #include <string>
 #include <algorithm>
 #include <cmath>
 #include <cstdio>
 #include <queue>
 #include <stack>
 #include <map>
 #include <bitset>
 #include <set>
 #include <vector>
 #include <iomanip>
#define ll long long
#define ull unsigned long long
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define bep(i, a, b) for(int i = a; i >= b; --i)
#define lowbit(x) (x&(-x))
#define MID (l + r) / 2
#define ls pos*2
#define rs pos*2+1
#define pb push_back
#define ios() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

using namespace std;

const int maxn = 100010;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const double eps = 1e-4;
const double PI = acos(-1);
int n, k,vis[55];
ll a[55][55];
ll dis[55];
int pre[10][55];
ll SPFA(int id) {
    rep(i, 1, n)dis[i] = inf,vis[i] = 0,pre[id][i] = -1;
    dis[1] = 0;
    queue<int>que;
    que.push(1);
    while (!que.empty()) {
        int u = que.front();
        que.pop();
        vis[u] = 0;
        for (int v = 1; v <= n; v++) {
            if (u != v && dis[v] > dis[u] + a[u][v]) {
                pre[id][v] = u;
                dis[v] = dis[u] + a[u][v];
                if (vis[v])continue;
                vis[v] = 1;
                que.push(v);
            }
        }
    }
    return dis[n];
}
ll ans;
void DFS(int x) {
    if (x == 0) {
        ll now = SPFA(x);
        ans = max(ans, now);
        return;
    }
    SPFA(x);
    int v = n;
    while (pre[x][v] != -1) {
        int u = pre[x][v];
        ll temp = a[u][v];
        a[u][v] = a[v][u] = inf;
        DFS(x - 1);
        a[u][v] = a[v][u] = temp;
        v = u;
    }
}
int main() {
    ios();
    int T;
    cin >> T;
    while (T--) {
        ans = 0;
        cin >> n >> k;
        int all = n * (n - 1) / 2;
        rep(i, 1, all) {
            int u, v, w;
            cin >> u >> v >> w;
            a[u][v] = a[v][u] = w;
        }
        DFS(k);
        cout << ans << endl;
    }
    return 0;
}