zoj1232Adventure of Super Mario(图上dp)

题目连接:

啊哈哈,点我点我

思路:

这个题目是一个图上dp问题,先floyd预处理出图上所有点的最短路,但是在floyd的时候,把能够用神器的地方预处理出来,也就是转折点地方不能为城堡。。预处理完毕后,就是一个dp问题了。。。dp[][],两维分别表示到达的地点和使用神器的次数。。这样这个问题就得到了解决。。

题目:

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

代码:

#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)
                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)

时间: 2024-08-28 17:46:51

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

ZOJ 3644 Kitty&#39;s Game (图上DP 约数)

哎-这一场就做了三个题目,全队倒数第一,简直是太弱了. A Kitty's Game (ZOJ 3644) 题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3644 题意: 给出一个有向图,每个节点有一个权值pi, 有一个人从1节点出发(其权值为1节点的权值),前往n号节点,每经过一个节点,他的权值就变成了他经过这个节点前的权值和这个节点权值的最小公倍数,如果他经过这个节点后权值不发生变化则他就不能经过这个节点

poj 3635 Full Tank? ( 图上dp )

题意: 已知每个点的加油站的油价单价(即点权),每条路的长度(边权). 有q个询问,每个询问包括起点s.终点e和油箱容量. 问从起点走到终点的最小花费.如果不可达输出impossible,否则输出最小的旅途费用. 算法: 其实要分析状态= =感觉就像是dp. 最直接的想法是  每到一个点都加上要走到下一个点所需要的油量.但是走的路不同,到底怎么处理加多少的问题呢? 因此想到分解状态,即拆点.每到一个点都+1单位的油量,然后把这个状态加入队列.另外如果现在油箱内的油足够达到下一点, 则更新状态,把

poj 3249 Test for Job 图上dp(记忆化搜索)

题意: 给一个n个点的DAG,每个点有一个值p,现在要在图上找一个入度为0到出度为0的路径,使路径上的点的p值和最大. 分析: dp[v]记录以点v为起点能获得的最大值,搜一遍即可. 代码: //poj 3249 //sep9 #include <iostream> using namespace std; const int maxN=100024; const int maxM=1000024; int n,m,e; int p[maxN],head[maxN],dp[maxN],vis[

zoj 1232 Adventure of Super Mario (Floyd+dp)

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 n

Codeforces Round #455 (Div. 2) E. Coprocessor DAG图上dp

E. Coprocessor 题意:n 个任务,每个任务在 主 / 副 处理器上执行.每个任务可能依赖于其它的一些任务,副处理器每次可以处理多个任务.但如果一个任务要在副处理器上执行,那它所依赖的任务要么已执行完了,要么和它一起在这个副处理器上同时执行.问副处理器最少调用多少次. 直白一点讲,就是给出一个 DAG 图,n 个点, m 条边,每个点的权值为 0 或1 .操作:直接相互连通的权值为 1 的点可以一次处理掉. 问最少操作多少次. tags:因为是DAG 图,直接跑 dp dp[i] 表

Codeforces 918D MADMAX 图上dp 组合游戏

题目链接 题意 给定一个 \(DAG\),每个边的权值为一个字母.两人初始各占据一个顶点(可以重合),轮流移动(沿着一条边从一个顶点移动到另一个顶点),要求每次边上的权值 \(\geq\) 上一次的权值.无法移动者输. 要求:对所有可能的初始情况,给出一张胜负表. 思路 特殊情况 两人在同一个顶点上,那么必然是先手输: 如果有\(u\rightarrow v\)边,并且先手在 \(u\) 上,后手在 \(v\) 上,且先手此时可以移动(判断边的权值),那么必然是先手赢 一般情况 考虑用 \(dp

ZOJ 1232 Adventure of Super Mario SPFA+DP

第一次做类似的题目,卡了好几天,最后看了爱酱的blog(http://blog.csdn.net/acm_cxlove/article/details/8679230)才会的,sad 题意大概是这样,给你一个图,求起点1到N的最短时间,你有一双鞋子,可以加速,一次性花费0的时间行走M单位的路程,但是鞋子只能用K次,并且鞋子使用的时候起点和终点都必须在节点上,不能在半路停下.并且使用的鞋子的时候不能穿过编号大于A的节点,在不使用鞋子的情况下行走单位路程花费单位时间. dp[i][k]表示从起点到i

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

 解题思路: 求有向无环图上的最长路,简单的动态规划 #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <cmath> #define LL long long using namespace std; const int

hdu 5001 概率DP 图上的DP

http://acm.hdu.edu.cn/showproblem.php?pid=5001 当时一看是图上的就跪了 不敢写,也没退出来DP方程 感觉区域赛的题  一则有一个点难以想到 二则就是编码有点难度. 这个题: 我一直的思路就是1-能到达i的概率 就是不能到达i的概率,然后三维方程巴拉巴拉,,,,把自己搞迷糊 正确做法: dp[k][j]   经过j步到达k点 并且不经过i点的概率 这么设的原因是,就可以求不能到达i点的概率了.   不能到达i点的概率就是segma(dp[v][j-1]