UVA10269 Adventure of Super Mario(Floyd+DP)

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个顶点的一张简单图,其中A个顶点标示为村庄,B个顶点标示为城堡,求顶点1到顶点A+B的一条最短路径,但是过程中允许使用K次魔法鞋,每次使用可以在0的时间内移动L个单位,中途遇到城堡将停止魔法,使用魔法鞋的起点和终点都只能是城堡或者村庄。

解题报告

这道题目一开始想到最短路,可是不知道怎么处理使用魔法鞋的情况。在网上看了神犇的博客才知道解法,其实是Floyd+dp 首先,用Floyd处理任意两点间距离,开一个数组,在处理时判断是否能用魔法鞋走,即g[i][j]<=L&&k<=A,那么can[i][j]=1 。然后,再用dp处理。用数组dis[i][k]
表示走到地i个点,用了k次魔法的最小值。状态转移方程为 dis[j][k-1] (i与j之间可用魔法鞋)

$dis[i][k]=min\{\sum\limits_{j=1}^{i-1}dis[j][h]+g[j][i]\}$

初始状态
dis[i][0]=g[s][i] (i=1 to a+b)  
dis[1][i]=0 i=0 to K

最后dis[A+B][K] 即为结果。

#include<queue>
#include <algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define Pair pair<int,int>
#define MAXN 101
#define MAX 99999999
using namespace std;
int t,a,b,m,l,k,g[MAXN][MAXN],can[MAXN][MAXN];
int dis[MAXN][MAXN];
void init()
{
memset(can,,sizeof(can));
memset(g,,sizeof(g));
memset(dis,,sizeof(dis));
scanf("%d%d%d%d%d",&a,&b,&m,&l,&k);
for(int i=;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
g[x][y]=g[y][x]=z;
if(z<=l) can[x][y]=can[y][x]=;
}
}
void floyed()
{
for(int i=;i<=a+b;i++) g[i][i]=;
for(int k=;k<=a+b;k++)
for(int i=;i<=a+b;i++)
for(int j=;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) can[i][j]=can[j][i]=;
} }
void dp(int s)
{
for(int i=;i<=a+b;i++) dis[i][]=g[s][i];
for(int i=;i<=k;i++) dis[s][i]=;
for(int i=;i<=a+b;i++)
for(int h=;h<=k;h++)
{
int minn=MAX;
for(int j=;j<i;j++)
{
if(can[i][j]) minn=min(minn,dis[j][h-]);
minn=min(minn,dis[j][h]+g[j][i]);
}
dis[i][h]=minn;
}
} int main()
{ scanf("%d",&t);
while(t--)
{
init();
floyed();
dp();
printf("%d\n",dis[a+b][k]);
}
return ;
}

(对于我来说算是个难题了==)

UVA10269 Adventure of Super Mario(Floyd+DP)的更多相关文章

  1. UVa 10269 Adventure of Super Mario (Floyd + DP + BFS)

    题意:有A个村庄,B个城市,m条边,从起点到终点,找一条最短路径.但是,有一种工具可以使人不费力的移动L个长度,但始末点必须是城市或村庄.这种工具有k个,每个只能使用一次,并且在城市内部不可使用,但在 ...

  2. ZOJ 1232 Adventure of Super Mario (Floyd + DP)

    题意:有a个村庄,编号为1到a,有b个城堡,编号为a+1到a+b.现在超级玛丽在a+b处,他的家在1处.每条路是双向的,两端地点的编号以及路的长度都已给出.路的长度和通过所需时间相等.他有一双鞋子,可 ...

  3. ZOJ1232 Adventure of Super Mario(DP+SPFA)

    dp[u][t]表示从起点出发,到达i点且用了t次magic boot时的最短时间, 方程如下: dp[v][t]=min(dp[v][t],dp[u][t]+dis[u][v]); dp[v][t] ...

  4. [题解]UVA10269 Adventure of Super Mario

    链接:http://vjudge.net/problem/viewProblem.action?id=24902 描述:由城镇.村子和双向边组成的图,从A+B走到1,要求最短路.有K次瞬移的机会,距离 ...

  5. UVA-10269 Adventure of Super Mario (dijkstra)

    题目大意:有A个村庄,B个城市,m条边,从起点到终点,找一条最短路径.但是,有一种工具可以使人不费力的移动L个长度,但始末点必须是城市或村庄.这种工具有k个,每个只能使用一次,并且在城市内部不可使用, ...

  6. ZOJ1232 Adventure of Super Mario spfa上的dp

    很早之前听说有一种dp是在图上的dp,然后是在跑SPFA的时候进行dp,所以特地找了一题关于在SPFA的时候dp的. 题意:1~a是村庄 a+1~a+b是城堡,存在m条无向边.求由a+b->1的 ...

  7. UVA 10269 Adventure of Super Mario

    看了这里 http://blog.csdn.net/acm_cxlove/article/details/8679230的分析之后自己又按照自己的模板写了一遍,算是对spfa又加深了一步认识(以前真是 ...

  8. zoj1232Adventure of Super Mario(图上dp)

    题目连接: 啊哈哈.点我点我 思路: 这个题目是一个图上dp问题.先floyd预处理出图上全部点的最短路,可是在floyd的时候,把可以用神器的地方预处理出来,也就是转折点地方不能为城堡..预处理完成 ...

  9. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

随机推荐

  1. MyEclipse 启动之 java.lang.RuntimeException: No application id has been

    found. 今天公司刚买来一台服务器,配置安装 java 开发环境的时候,MyEclipse 无法启动,查看日志文件之后,具体错误信息 如下: [java] view plaincopyprint? ...

  2. SpringCloud学习笔记(5)----Spring Cloud Netflix之Eureka的服务认证和集群

    1. Eureka服务认证 1. 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...

  3. Dapper基础知识三

    在下刚毕业工作,之前实习有用到Dapper?这几天新项目想用上Dapper,在下比较菜鸟,这块只是个人对Dapper的一种总结. Dapper,当项目在开发的时候,在没有必要使用依赖注入的时候,如何做 ...

  4. Vue-cli 3.0 构建项目

    Vue-cli是vue的一个脚手架,我们可以通过它来构建我们的前端项目 vue-cli3环境配置 //1. 安装nodeJS(已经集成npm) 首先需要安装node环境,可以直接到中文官网http:/ ...

  5. vue生命周期-学习心得

    每个Vue实例在被创建之前都要经过一系列的初始化过程,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染→更新→渲染.销毁等一系列过程,这个过程就是vue的生命周期. 1 vue生命周期图 {: ...

  6. 【CS-4476-project 6】Deep Learning

    AlexNet / VGG-F network visualized by mNeuron. Project 6: Deep LearningIntroduction to Computer Visi ...

  7. LAMP环境搭建备忘 -- Apache、pHp 安装 (二)

    上一篇 Linux 已经安装好了,我们选择了 CentOS 7 的最小化安装,即没有图形界面,并且我们在安装时设置了网络连接即能够连上外部网络,还设置了 root 密码.下面我们要在此基础上继续安装 ...

  8. makefile--回顾基础篇

    前阵子让写makefile,纠结了下,基本忘记差不多了. 1.gcc的编译选项 -c 只是编译不链接,生成目标文件“.o” -S 只是编译不汇编,生成汇编代码 -E 只进行预编译,不做其他处理 -g ...

  9. 紫书 例题 10-3 UVa 10375 (唯一分解定理)

    这道题感觉非常的秀 因为结果会很大,所以就质因数分解分开来算 非常的巧妙! #include<cstdio> #include<vector> #include<cstr ...

  10. Linux文件属性(属主属组权限)

    Linux文件属性 Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规定. ...