(beginer) 最短路 UVA 10269 Adventure of Super Mario

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.

(beginer) 最短路 UVA 10269 Adventure of Super Mario - 恶魔仁 - 恶魔仁

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
__________________________________________________________________________________________
Rujia Liu


题意:起点是A+B,终点是1,有K次加速机会,加速的时候,到达城堡必须停下来,经过村庄可以继续,但是在一段路加速的时候貌似不可以一半加速,一半普通速度。不过也很好搞吧。

思路:一开始我们先用floyd算法,算出点和点直接只经过村庄的最短路。然后再根据这个矩阵构图。最后spfa就行了。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 105;
int M , A , B , L , K;
int g[maxn][maxn];
int d[maxn][15];
bool inq[maxn][15];
vector<int> G[maxn];

struct State
{
        State(int xx,int kk) : x(xx) , k(kk) { }
        int x;
        int k;
};

void input()
{
        memset(g,0,sizeof(g));
        while (M--)
        {
                int u , v , w;
                scanf("%d%d%d",&u,&v,&w);
                g[u][v] = g[v][u] = w;
        }
        for (int k = 1 ; k <= A ; ++k)
        {
                for (int i = 1 ; i <= A+B ; ++i) if (g[i][k])
                        for (int j = 1 ; j <= A+B ; ++j) if (g[k][j])
                                if (g[i][j]==0 || g[i][j] > g[i][k]+g[k][j]) g[i][j] = g[i][k]+g[k][j];
        }
        for (int i = 1 ; i <= A+B ; ++i) {
                G[i].clear();
                for (int j = 1 ; j <= A+B ; ++j) if (g[i][j])
                        G[i].push_back(j);
        }
}

void solve()
{
        queue<State> q;
        memset(d,0x3f,sizeof(d));
        memset(inq,false,sizeof(inq));
        inq[A+B][K] = true;
        d[A+B][K] = 0;
        q.push(State(A+B,K));
        while (q.size())
        {
                State tmp = q.front(); q.pop();
                int x = tmp.x , k = tmp.k;
                inq[x][k] = false;
                for (int i = 0 ; i < G[x].size() ; ++i)
                {
                        int y = G[x][i];
                        if (d[y][k] > d[x][k]+g[x][y]) {
                                d[y][k] = d[x][k]+g[x][y];
                                if (!inq[y][k])
                                {
                                        inq[y][k] = true;
                                        q.push(State(y,k));
                                }
                        }
                        if (k > 0 && g[x][y] <= L && d[y][k-1] > d[x][k]) 
                        {
                                d[y][k-1] = d[x][k]+max(0,g[x][y]-L);
                                if (!inq[y][k-1]) {
                                        inq[y][k-1] = true;
                                        q.push(State(y,k-1));
                                }
                        }
                }
        }
        int ans = 1e9;
        for (int i = 0 ; i <= K ; ++i) if (ans > d[1][i])
                ans = d[1][i];
        cout << ans << endl;
}

int main()
{
        int T; cin>>T;
        while (T--)
        {
                scanf("%d%d%d%d%d",&A,&B,&M,&L,&K);
                input();
                solve();
        }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值