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 theprincess of course :-) He's very familiar with the 'Super Mario World', so he doesn't needa map, he only needs the best route in order to save time.
![](http://uva.onlinejudge.org/external/102/p10269.jpg)
There are A Villages and B Castles in the world. Villages are numbered 1..A, and Castles are numberedA+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 acastle 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 covermore 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 ofroads, 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
14 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
代码:
#include<iostream>
#include<stdio.h>
using namespace std;
const int oo = 2147483647;
const int maxn = 110;
int map[maxn][maxn],d[maxn][maxn];
int f[15][maxn];
bool vis[15][maxn];
int A, B, M, L, K;
void floyed()
{
for (int k = 1; k <= A; k ++)
{
for (int i = 1; i <= A + B; i ++)
{
for (int j = 1; j <= A + B; j ++)
{
if (i != j && d[i][k] != oo && d[k][j] != oo)
{
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
}
void dijkstra()
{
int u, v, Min, id;
for (int k = 0; k <= K; k ++)
{
for (int u = 1; u <= A + B; u ++)
{
f[k][u] = oo;
vis[k][u] = false;
}
}
for(int k=0;k<=K;k++)
{
u = A + B;
f[k][u] = 0;
vis[k][u] = true;
for (int v = 1; v <= A + B; v ++)
{
if (!vis[k][v] && map[u][v] != oo)
f[k][v] = min(f[k][v], map[u][v]);
if (d[u][v] <= L) f[k + 1][v] = f[k][u];
}
for(int t=1;t<A+B;t++)
{
Min=oo;
for (int v = 1; v <= A + B; v ++)
{
if (!vis[k][v] && f[k][v] < Min)
{
Min = f[k][v];
id = v;
}
}
vis[k][id] = true;
for (int v = 1; v <= A + B; v ++)
{
if (!vis[k][v] && map[id][v] != oo)
f[k][v] = min(f[k][v], f[k][id] + map[id][v]);
if (d[id][v] <= L)
f[k + 1][v] = min(f[k + 1][v], f[k][id]);
}
}
}
}
int main()
{
int T;
int Xi, Yi, Li;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d",&A,&B,&M,&L,&K);
for (int i = 1; i <= A + B; i ++)
{
for (int j = i; j <= A + B; j ++)
{
map[i][j] = map[j][i] = oo;
d[i][j] = d[j][i] = oo;
}
}
for (int i = 1; i <= M; i ++)
{
scanf("%d%d%d", &Xi, &Yi, &Li);
map[Xi][Yi] = map[Yi][Xi] = Li;
d[Xi][Yi] = d[Yi][Xi] = Li;
}
floyed();
dijkstra();
printf("%d\n",f[K][1]);
}
return 0;
}