码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 6797 Tokitsukaze and Rescue

时间:2020-07-28 22:14:42      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:ios   offer   tran   pow   rescue   就是   port   fas   sim   

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 are n cities numbered from 1 to n in the kingdom Adalet. Because of the excellent transportation, there is exactly a two-way road between any two cites. Tokitsukaze lives in city 1. The Magician Claris lives in city n. 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 city n.

Claris has also heard this news and is afraid of being punished, so he decides to slow Tokitsukaze down by making an explosion on k roads 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 city 1 to city n.

 


Input
There are several test cases.

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

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

The next n(n1)2 lines describe all the roads, where each line contains three integers uv and w (1u,vn,uv,1w104), representing a two-way road of length w between city u and city v. It is guaranteed that for every two cities, there exists exactly one road whose length is randomly distributed between 1 and 104.
 


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
 


Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6801 6800 6799 6798 6797 
题意:在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;
}

  

HDU 6797 Tokitsukaze and Rescue

标签:ios   offer   tran   pow   rescue   就是   port   fas   sim   

原文地址:https://www.cnblogs.com/cherish-lin/p/13393495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!