ZOJ-1232 Adventure of Super Mario

26 篇文章 0 订阅
12 篇文章 0 订阅
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 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

————————————————————集训13.2的分割线————————————————————

思路:这道题做起来简单,想起来难。反过来从村子1开始,在图的每个顶点上,我有可能使用鞋子,也可能不使用鞋子。因此预处理出每个点的最短路。在松弛的时候,判断k点是不是城堡、判断能不能 i 到 j 的距离是否不超过飞行长度。这样就可以在任意的两点之间进行状态转移。

状态有两个,一个是当前点,一个是剩余的使用靴子的次数。

先跑Floyd,然后初始化DP[ ][ ],另开一个数组表示 i 到 j 能不能飞。

P.S. 鞋子的飞行长度是每次飞行的长度。

P.S. 假如村子不是前A个点的话,就不能够像这样写Floyd。假设1是村子。2是城堡,3是村子,4是村子/城堡。当我选取3进行松弛的时候,3不是城堡而且飞行长度足够的话,就认为1可以飞到4了。但是2是城堡。本题前面的都是村子,只要还没有枚举到后B个点,从 i 到 j 之间的最短路上就不会有城堡(只枚举了前A个)。

P.S. 假如如上所述编号是混乱的,那么可以映射、重新编号。还可以用SPFA进行预处理,城堡只能作为起点入队,并不能作为松弛点入队。而DP的过程,可以直接递推,也可以再扔进SPFA里面。只是松弛条件从路径长变成了更小的DP值了。

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int NA = 55, NB = 55, N = 110;
int A, B, m, L, K, n;
int G[N][N], dp[N][15];//表示从起点走到某一点鞋子用了k次的最短路径
bool fly[N][N];

void Floyd()
{
    //由于该题前A个都是村子,才可以直接从前向后Floyd
    for(int k = 1; k <= n; k++) {//选一个k
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) if(k != i && k != 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) 
                        fly[i][j] = fly[j][i] = true; 
                }
            }
        }
    }
}

void run_DP()
{
    memset(dp, 0x3f, sizeof(dp));
    for(int i = 1; i <= n; i++) {
        dp[i][0] = G[1][i];
    }
    for(int k = 0; k <= K; k++) {
        dp[1][k] = 0;
    }
    for(int i = 2; i <= n; i++) {
        for(int k = 1; k <= K; k++) {
            for(int j = 1; j < i; j++) {
                if(fly[j][i]) {
                    dp[i][k] = min(dp[i][k], dp[j][k-1]);//飞过来
                }
                dp[i][k] = min(dp[i][k], dp[j][k] + G[j][i]);//不能飞过来,或者选择走过来
            }
        }
    }
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
//    freopen(".out", "w", stdout);
#endif
    int T;
    scanf("%d", &T);
    while(T--) {
        memset(fly, 0, sizeof(fly));
        memset(G, 0x3f, sizeof(G));
        scanf("%d%d%d%d%d", &A, &B, &m, &L, &K);
        n = A + B;
        int u, v, w;
        for(int i = 0; i < m; i++) {
            scanf("%d%d%d", &u, &v, &w);
            G[u][v] = G[v][u] = w;
            if(w <= L) fly[u][v] = fly[v][u] = true;
        }
        Floyd();
        run_DP();
        printf("%d\n", dp[A+B][K]);
    }
    return 0;
}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值