UVA 10269 - Adventure of Super Mario(最短路 + dp)

来源:互联网 发布:laravel 批量删除数据 编辑:程序博客网 时间:2024/04/29 03:58

Problem A
Adventure of Super Mario
Input:
 Standard Input
Output: Standard Output

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 Villages and B Castles in the world. Villages 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 Villages 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 repaired 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 Villages, 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

题意:A个村庄,B个城堡,有M条线路,魔法鞋最长距离L,和可以使用次数K。现在马里奥在最后一个城堡要到第一个村庄,中间可以用魔法鞋直接穿越一些村庄如果魔法鞋可以穿越的距离大于村庄距离,但是如果中间有城堡的话魔法鞋是穿不过去的。问现在除去魔法鞋最少要走的路程。

思路:利用folyd把点点间最短路找出来,用一个数组标记如果中间有城堡或距离大于L则不能用,然后利用dijkstra去找最短路,保留起点到目标点距离的权值的数组要多开一维来保存次数。

代码:

#include <stdio.h>#include <string.h>#define min(a, b) (a)<(b)?(a):(b)#define INF 0x3f3f3f3fconst int MAXN = 110;int t, A, B, M, L, K, G[MAXN][MAXN], floyd[MAXN][MAXN], d[MAXN][15];int a, b, v;void init() {    memset(floyd, 0, sizeof(floyd));    memset(G, INF, sizeof(G));    memset(d, 0, sizeof(d));    scanf("%d%d%d%d%d", &A, &B, &M, &L, &K);    for (int i = 0; i < M; i ++) {scanf("%d%d%d", &a, &b, &v);G[a][b] = G[b][a] = v;if (v <= L) floyd[a][b] = floyd[b][a] = 1;    }    for (int i = 1; i <= A + B; i ++)G[i][i] = 0;    for (int k = 1; k <= A + B; k ++)for (int i = 1; i <= A + B; i ++)    for (int j = 1; j <= A + B; j ++) {if (G[i][j] > G[i][k] + G[k][j]) {    G[i][j] = G[i][k] + G[k][j];    if (k <= A && G[i][j] <= L) floyd[i][j] = floyd[j][i] = 1;}    }}void Dijkstra(int s) {    for (int i = 1; i <= A + B; i ++) d[i][0] = G[s][i];    for (int i = 0; i <= K; i ++) d[s][i] = 0;    for (int i = 2; i <= A + B; i ++) {for (int k = 1; k <= K; k ++) {    int Min = INF;    for (int j = 1; j < i; j ++) {if (floyd[j][i]) {    Min = min(Min, d[j][k - 1]);}Min = min(Min, d[j][k] + G[j][i]);    }    d[i][k] = Min;}    }}void solve() {    init();    Dijkstra(1);    printf("%d\n", d[A + B][K]);}int main() {    scanf("%d", &t);    while (t --) {solve();    }    return 0;}


原创粉丝点击