题目连接:

啊哈哈。点我点我

思路:

这个题目是一个图上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. python 跨域

    CORS跨域请求 CORS即Cross Origin Resource Sharing 跨域资源共享, 那么跨域请求还分为两种,一种叫简单请求,一种是复杂请求~~ 简单请求 HTTP方法是下列方法之一 ...

  2. PHP 页面跳转的三种方式

    第一种方式:header() header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. 语法: void header ( string $string [, bool $re ...

  3. Excel读取导入数据库碰到的问题

    1.未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序. 下载并安装驱动:http://download.microsoft.com/download/7/0/3/703 ...

  4. ubuntu14.04:php7+apache2+mysql

    apache2: sudo apt-get install apache2 apache2-dev service apache2 start mysql: sudo apt-get install ...

  5. lua-helloworld

    write script file, a.lua: #!/usr/bin/lua print("hello world!") add excutable prperty to th ...

  6. .NET重构(五):存储过程、触发器和函数的区别

    导读:在触发器的学习过程中,师傅讲了它的耦合性高,建议我能用存储过程,那到底什么是存储过程呢,自己也不是特别了解,还有就是,触发器也算是一种特殊的存储过程,为什么就不建议多用呢?接下来,就谈谈触发器. ...

  7. Python模块安装路径初探

    在调用MySQL第三方工具mysqlrplsync是报错 mysqlrplsync --master=checksum:checksum@master_ip:3306 --slave=checksum ...

  8. 【Luogu】P2827蚯蚓(堆转队列)

    按照国际惯例先发题目链接‍ woc从4月就开始做这sb题.最开始30分升到65分不管了,直到最近几天升到85分,再到今天AC.激动的心情自然是那些一遍就A或者一小时以内就A的神犇难以想象的. 下面说说 ...

  9. [luoguP3413] SAC#1 - 萌数(数位DP)

    传送门 gtm的数位dp! 看到好多题解,都是记忆化搜索,好像非常方便啊,但是我还是用递推好了,毕竟还是有些类似数位dp的题用递推的思路,记忆化做不了,现在多培养一下思路 首先这道题, 只看长度大于等 ...

  10. response.sendRedirect(location)与rd.forward()区别

    在Java Web开发中,经常会用到跳转页面的方法,一般有下面两种方法. HttpServletResponse response = new HttpServletResponse(); respo ...