题目连接:

啊哈哈。点我点我

思路:

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

。dp[][],两维分别表示到达的地点和使用神器的次数。。

这样这个问题就得到了解决。。

题目:

ZOJ Problem Set - 1232
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


Source: OIBH Reminiscence Programming Contest

Submit    Status

代码:

#include<cstdio>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std; const int maxn=100+10;
int gra[maxn][maxn],dp[maxn][10+10];
bool is_true[maxn][maxn];
int A,B,M,L,K;
int t,u,v,w; void floyd()
{
for(int k=1;k<=A+B;k++)
for(int i=1;i<=A+B;i++)
for(int j=1;j<=A+B;j++)
{
if(gra[i][j]>gra[i][k]+gra[k][j])
gra[i][j]=gra[i][k]+gra[k][j];
if(k<=A&&gra[i][j]<=L)//这里仅仅有3个点。所以仅仅须要注意中间的点是不是城堡就可以。。 is_true[i][j]=is_true[j][i]=true;
}
} void read_Graph()
{
memset(is_true,false,sizeof(is_true));
scanf("%d%d%d%d%d",&A,&B,&M,&L,&K);
for(int i=1;i<=A+B;i++)
for(int j=1;j<=A+B;j++)
{
if(i==j) gra[i][j]=0;
else gra[i][j]=INF;
}
for(int i=1;i<=M;i++)
{
scanf("%d%d%d",&u,&v,&w);
gra[u][v]=gra[v][u]=w;
if(w<=L) is_true[u][v]=is_true[v][u]=true;
}
} void solve()
{
memset(dp,0x3f,sizeof(dp));
for(int i=1;i<=A+B;i++)
dp[i][0]=gra[1][i];
for(int k=0;k<=K;k++)
dp[1][k]=0;
for(int i=1;i<=A+B;i++)
for(int k=1;k<=K;k++)
for(int j=1;j<i;j++)
{
if(is_true[j][i])
dp[i][k]=min(dp[i][k],dp[j][k-1]);
dp[i][k]=min(dp[i][k],dp[j][k]+gra[j][i]);
}
printf("%d\n",dp[A+B][K]);
} int main()
{
scanf("%d",&t);
while(t--)
{
read_Graph();
floyd();
solve();
}
return 0;
}

zoj1232Adventure of Super Mario(图上dp)的更多相关文章

  1. ZOJ1232 Adventure of Super Mario spfa上的dp

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

  2. 洛谷 P2656 (缩点 + DAG图上DP)

    ### 洛谷 P2656 题目链接 ### 题目大意: 小胖和ZYR要去ESQMS森林采蘑菇. ESQMS森林间有N个小树丛,M条小径,每条小径都是单向的,连接两个小树丛,上面都有一定数量的蘑菇.小胖 ...

  3. Codeforces 918D MADMAX 图上dp 组合游戏

    题目链接 题意 给定一个 \(DAG\),每个边的权值为一个字母.两人初始各占据一个顶点(可以重合),轮流移动(沿着一条边从一个顶点移动到另一个顶点),要求每次边上的权值 \(\geq\) 上一次的权 ...

  4. poj 3635 Full Tank? ( 图上dp )

    题意: 已知每一个点的加油站的油价单位价格(即点权).每条路的长度(边权). 有q个询问.每一个询问包含起点s.终点e和油箱容量. 问从起点走到终点的最小花费.假设不可达输出impossible,否则 ...

  5. [Luogu P3119] [USACO15JAN]草鉴定Grass Cownoisseur (缩点+图上DP)

    题面 传送门:https://www.luogu.org/problemnew/show/P3119 Solution 这题显然要先把缩点做了. 然后我们就可以考虑如何处理走反向边的问题. 像我这样的 ...

  6. [正经分析] DAG上dp两种做法的区别——拓扑序与SPFA

    在下最近刷了几道DAG图上dp的题目. 要提到的第一道是NOIP原题<最优贸易>.这是一个缩点后带点权的DAG上dp,它同时规定了起点和终点. 第二道是洛谷上的NOI导刊题目<最长路 ...

  7. DAG上dp思想

    DAG上DP的思想 在下最近刷了几道DAG图上dp的题目.要提到的第一道是NOIP原题<最优贸易>.这是一个缩点后带点权的DAG上dp,它同时规定了起点和终点.第二道是洛谷上的NOI导刊题 ...

  8. HDU 3249 Test for job (有向无环图上的最长路,DP)

     解题思路: 求有向无环图上的最长路.简单的动态规划 #include <iostream> #include <cstring> #include <cstdlib ...

  9. hdu 5001 概率DP 图上的DP

    http://acm.hdu.edu.cn/showproblem.php?pid=5001 当时一看是图上的就跪了 不敢写,也没退出来DP方程 感觉区域赛的题  一则有一个点难以想到 二则就是编码有 ...

随机推荐

  1. joyoi1864 守卫者的挑战

    #include <algorithm> #include <iostream> #include <cstdio> using namespace std; in ...

  2. POJ-3261 Milk Patterns,后缀数组+二分。。

                                                        Milk Patterns 题意:求可重叠的至少重复出现k次的最长的字串长. 这题的做法和上一题 ...

  3. phpstorm 修改头部注释

    点击“setting”->"File  Templates"  ->"PHP File Header"    

  4. iOS学习笔记21-推送证书与秘钥申请

    ---恢复内容开始--- 一.iOS证书 iOS常用的证书包括开发证书和发布证书,无论是真机调试还是最终发布应用到App Store这两个证书都是必须的,它是iOS开发的基本证书.Xcode7以后真机 ...

  5. 【bzoj3696】化合物 树形dp

    题目描述 首长NOI惨跪,于是去念文化课了.现在,他面对一道化学题.这题的来源是因为在一个奇怪的学校两个化竞党在玩一个奇怪的博弈论游戏.这个游戏很蛋疼,我相信你们也没有兴趣听.由于这个游戏涉及博弈论, ...

  6. 【Luogu】P1383高级打字机

    可持久化线段树模板题之一. 权当温习主席树模板 #include<cstdio> #include<cstdlib> #include<cctype> #defin ...

  7. NOJ——1628Alex’s Game(III)(DFS+回溯)

    [1628] Alex’s Game(III) 时间限制: 1000 ms 内存限制: 65535 K 问题描述 Alex likes to play with one and zero as you ...

  8. HDU 1565 方格取数(1) ——插头DP

    [题目分析] 其实直接状压就可以了. 但是有点闲,又写了一个可读性极差,智商低下,很(gou)好(pi)的代码 [代码] #include <cstdio> #include <cs ...

  9. 源码分析 脱壳神器ZjDroid工作原理

    0. 神器ZjDroid Xposed框架的另外一个功能就是实现应用的简单脱壳,其实说是Xposed的作用其实也不是,主要是模块编写的好就可以了,主要是利用Xposed的牛逼Hook技术实现的,下面就 ...

  10. SpringBoot + Spring Security 基本使用及个性化登录配置详解

    Spring Security 基本介绍 这里就不对Spring Security进行过多的介绍了,具体的可以参考官方文档 我就只说下SpringSecurity核心功能: 认证(你是谁) 授权(你能 ...