uva10269 - Adventure of Super Mario floyd+dp+spfa

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

 

  有村庄1到A,城堡A+1到A+B,马里奥要从城堡A+B到村庄A。他可以瞬移不超过K次,每次瞬移可以从一个地点移动到另一个地点(瞬移的起点和终点只能是村庄或城堡),并且瞬移路径路径中间不能穿过城堡,距离不能超过L。

  这个题条件真是多。。思路搞清楚了其实也不算难。模型可以转化成建一个图,求起点到终点的最短路。这个图上的点就是d[u][k],表示第从起点经过k次瞬移到u点的最短路。

那么若k小于K,和u不经过城堡的最短距离小于等于L的点v,d[u][k]就可以和d[v][k+1]连一条边,权值为0。再考虑这一步不瞬移,那么就和一般的最短路一样做,d[u][k]可以和本来题目给的连接的点v连一条边,权值为w[u][v]。用这个图做起点为d[A+B][0],终点为d[1][K]的最短路就行了。

  现在问题变成要找出每两个点之间不经过城堡的最短路。用一次floyd就行了(k从1循环到A)。这时有些w[u][v]已经被更新到比原来小(也不一定是最小),直接那这个w作为要建的图的边也行(比光用题目给的边重新算最短路还快一些,因为已经完成一部分)。

  最后用spfa就行了,又好写又省事,具体过程也不用管,只要能更新的点重新加到队列就行了。。

  总之感觉这道题很有特点,图上的点是二维表示,把最短路和DP结合起来,以前一些DP都是一层一层递推,这个不好推,但是只要建好图用求最短路的方法就行了。这又有点像记忆化搜索,感觉最短路也可以用记忆化搜索做啊,但是貌似有环就麻烦了。。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define INF 0x3f3f3f3f
#define MAXN 10010
#define MAXM 5010
#define eps 1e-9
#define pii pair<int,int>
using namespace std;
int T,N,M,A,B,L,K;
int d[110][15],inq[110][15],w[110][110];
void floyd(){
    int i,j,k;
    for(k=1;k<=A;k++)
        for(i=1;i<=N;i++)
            for(j=1;j<=N;j++) w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
}
int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d%d%d",&A,&B,&M,&L,&K);
        N=A+B;
        memset(w,INF,sizeof(w));
        for(int i=1;i<=N;i++) w[i][i]=0;
        while(M--){
            int u,v,t;
            scanf("%d%d%d",&u,&v,&t);
            w[u][v]=w[v][u]=t;
        }
        floyd();
        memset(d,INF,sizeof(d));
        memset(inq,0,sizeof(inq));
        d[N][0]=0;
        queue<pii> q;
        q.push(make_pair(N,0));
        inq[N][0]=1;
        while(!q.empty()){
            pii cur=q.front();
            q.pop();
            int u=cur.first,k=cur.second;
            inq[u][k]=0;
            for(int v=1;v<=N;v++){
                if(k<K&&w[u][v]<=L&&d[v][k+1]>d[u][k]){
                    d[v][k+1]=d[u][k];
                    if(!inq[v][k+1]){
                        q.push(make_pair(v,k+1));
                        inq[v][k+1]=1;
                    }
                }
                if(d[v][k]>d[u][k]+w[u][v]){
                    d[v][k]=d[u][k]+w[u][v];
                    if(!inq[v][k]){
                        q.push(make_pair(v,k));
                        inq[v][k]=1;
                    }
                }
            }
        }
        printf("%d\n",d[1][K]);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值