Zoj 1232 adventure of Super Mario (Floyd + dp)

Source: Internet
Author: User

Adventure of Super Mario Time Limit: 2 seconds memory limit: 65536 KB After rescuing the beautiful princess, Super Mario needs to find a way home -- with the Princess of course :-) he's very familiar with the 'Super Mario world ', so he doesn't need a map, he only needs the best route in order to save time.

There are a ages and B castles in the world. AGEs are numbered 1 .. a, and castles are numbered a + 1 .. A + B. mario lives in village 1, and the castle he starts from is numbered A + B. also, there are two-way roads connecting them. two places are connected by at most one road and a place never has a road connecting to itself. mario has already measured the length of every road, but they don't want To walk all the time, since he walks one unit time for one unit distance (how slow !).

Luckily, in the castle where he saved the princess, Mario found a magic boot. if he wears it, he can super-run from one place to another in no time. (Don't worry about the princess, Mario has found a way to take her with him when super-running, but he wouldn't tell you:-P)

Since there are traps in the castles, Mario never super-runs through a castle. he always stops when there is a castle on the way. also, he starts/stops super-runnings only at ages or castles.

Unfortunately, the magic boot is too old, so he cannot use it to cover more than l kilometers at a time, and he cannot use more than K times in total. when he comes back home, he can have it retries red and make it usable again.

Input

The first line in the input contains a single integer T, indicating the number of test cases. (1 <= T <= 20) each test case begins with five integers a, B, M, L and K -- the number of ages, the number of Castles (1 <= A, B <= 50), the number of roads, the maximal distance that can be covered at a time (1 <= L <= 500), and the number of times the boot can be used. (0 <= k <= 10) The next M lines each contains three integers Xi, Yi, Li. that means there is a road connecting place XI and Yi. the distance is Li, so the walk time is also Li. (1 <= LI <= 100)

Output

For each test case in the input print a line containing a single integer indicating the minimal time needed to go home with the beautiful princess. It's guaranteed that Super Mario can always go home.

Sample Input

1
4 2 6 9 1
4 6 1
5 6 10
4 5 5
3 5 4
2 3 4
1 2 3

Sample output

9

Given N points, calculate the shortest path of 1-n. The additional condition is that a point before the N points is the village, and B is the castle. Mario uses a pair of boots to make a shuttle within a certain distance (L) and has only K chances. However, when using his shoes, he cannot walk through the castle. In this case, the shortest path is used to solve the problem.

Idea: first use Floyd to obtain the distance between any two points, and then enumerate each point. For details, see the code...

# Include "stdio. H "# include" string. H "# include" iostream "# include" algorithm "using namespace STD; # define N 105 const int INF = 1000000; int can [N] [N], E [N] [N], DP [N] ; int min (int A, int B) {return a <B? A: B;} int main () {int t, a, B, L, M, N, K; int I, J, K, U, V, D; scanf ("% d", & T); While (t --) {scanf ("% d", & A, & B, & M, & L, & K); n = a + B; for (I = 1; I <= N; I ++) // array initialization, {for (j = 1; j <= N; j ++) {e [I] [J] = (I = J? 0: INF); Can [I] [J] = 0 ;}} for (I = 0; I <m; I ++) {scanf ("% d", & U, & V, & D); e [u] [v] = E [v] [u] = D; if (d <= L) can [u] [v] = Can [v] [u] = 1 ;}for (k = 1; k <= N; k ++) // The Floyd algorithm is used to calculate the shortest circuit between any two points {for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {If (E [I] [J]> E [I] [k] + E [k] [J]) {e [I] [J] = E [I] [k] + E [k] [J]; if (E [I] [J] <= L & K <= A) can [I] [J] = 1 ;}}} for (I = 1; I <= N; I ++) DP [I]  = E  [I]; for (I = 2; I <= N; I ++) {for (j = 1; j <= K; j ++) {// DP [I] [J] indicates that the first point of I-1 goes to the I point, use the minimum time of the J-shoes; int TMP = inf; // enumerate the first I point in the [K, I] section to use the J-shoes, use the minimum time for (k = 1; k <I; k ++) // TMP to record this value {If (can [k] [I]) {TMP = min (TMP, min (DP [k] [J-1], DP [k] [J] + E [k] [I]);} // use the J-th shoe in the [K, I] section. The minimum value else TMP = min (TMP, DP [k] [J] + E [k] [I]);} DP [I] [J] = TMP;} printf ("% d \ n ", DP [N] [k]);} return 0 ;}

Zoj 1232 adventure of Super Mario (Floyd + dp)

Related Keywords:
Related Article E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD