HDU 6797 Tokitsukaze and Rescue
阿新 • • 發佈:2020-07-28
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.
The first line contains an integerT(1≤T≤100), denoting the number of test cases. Then follow all the test cases.
For each test case, the first line contains two integersnandk(3≤n≤50,1≤k≤min(n−2,5)), denoting the number of cities and the number of roads being exploded, respectively.
The nextn(n−1)2lines describe all the roads, where each line contains three integersu,vandw(1≤u,v≤n,u≠v,1≤w≤104), 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.
//#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; }