题目连接:

啊哈哈。点我点我

思路:

这个题目是一个图上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. Knockout v3.4.0 中文版教程-14-控制文本内容和外观-style绑定

    5. style绑定 目的 style绑定用来给关联的DOM元素添加或移除一个或多个样式值.在如下情况很有用,比如,当某些值为负时,高亮显示,或者设置容器元素的宽度来匹配数值的改变. (注意:如果你不 ...

  2. luogu1963 [NOI2009]变换序列

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...

  3. js 格式化 时间插件

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  4. 第五部分 linux系统管理员 开机流程 模组管理 与loader

    第五部分   linux系统管理员  开机流程  模组管理  与loader   开机流程分析 cmos保存电脑硬件的参数 bios 基本的输入输出系统  读取硬件的软件 MBR  master bo ...

  5. NYOJ 293 Sticks

    Sticks 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 George took sticks of the same length and cut them r ...

  6. redis2.3.7安装时出现undefined reference to `clock_gettime'

    (转自:http://blog.csdn.net/qq_28779503/article/details/54844988) undefined reference to `clock_gettime ...

  7. Scala基础知识[一]

    摘要:在Scala 是 Scalable Language 的简写,是一门多范式(multi-paradigm)的编程语言.设计初衷是要集成面向对象编程和函数式编程的各种特性.Scala 运行在Jav ...

  8. 雅礼培训4.3 Problem A 【点分治】

    题目简述 一个\(N\)个节点的树,有\(M\)个炸弹分布在一些节点上,有各自的威力,随着其他点距离增大对其他点的伤害呈等差减小,直至为0 问每个点受到的伤害 题解 QAQ考场代码没处理好有些炸弹威力 ...

  9. 小Z的袜子(bzoj 2038)

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...

  10. docker-nginx-标记一下

    拉取nginx镜像 然后启动容器: docker run -p : --name mynginx -d -v $PWD/nginx.conf:/etc/nginx/conf.d/default.con ...