ZOJ 1232 Adventure of Super Mario

最短路+DP(个人用的SPFA+完全背包)

做了一上午……开始想用SPFA+BFS。但是写了半天越写越乱,放弃了。

就想到了是不是可以当作背包问题(背出病了……)把鞋子可以使用的次数当作背包容量。做完全背包。

先N次SPFA把 各点的最短距离算出来,其实比较适合Floyd。(个人用vector实现伪邻接表,然后SPFA)

然后SPFA更新路径的时候,当鞋子使用次数不为0的时候,做完全背包。

还有个忧伤的地方就是我把INF=0x7fffffff。结果加上一个数 变成负数了。一直不对。

找了半天,最后加上了 !=INF 才对。

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long
using namespace std;
int a,b,m,l,k,n;
struct lx
{
    int v,len;
};
vector<lx>g[101];
int dis[101][101];
void SPFA(int start)
{
    queue<int>q;
    bool vis[101];
    memset(vis,0,sizeof(vis));

    q.push(start);
    vis[start]=1;
    dis[start][start]=0;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;

        for(int j=0;j<g[u].size();j++)
        {
            int v=g[u][j].v;
            int len=g[u][j].len;
            if(dis[start][v]>dis[start][u]+len)
            {
                dis[start][v]=dis[start][u]+len;
                if(v<=a&&!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
void spfa()
{
    bool vis[101];
    int dp[101][11];
    for(int i=0;i<101;i++)
    {
        vis[i]=0;
        for(int j=0;j<11;j++)
        dp[i][j]=INF;
    }
    queue<int>q;
    q.push(n);
    vis[n]=1;
    dp[n][k]=0;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;

        for(int i=0;i<=k;i++)
        {
            for(int j=0;j<g[u].size();j++)
            {
                int v=g[u][j].v;
                int len=g[u][j].len;
                if(dp[u][i]!=INF&&dp[v][i]>dp[u][i]+len)
                {
                    dp[v][i]=dp[u][i]+len;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        q.push(v);
                    }
//                    printf("%d > %d+%d\n",dp[v][i],dp[u][i],len);
//                    dp[u][i]!=INF;注意
                }
                if(i==0)continue;
                for(v=1;v<=n;v++)
                {
                    if(v!=u&&dis[u][v]<=l)
                    {
                        if(dp[v][i-1]>dp[u][i])
                        {
                            dp[v][i-1]=dp[u][i];
                            if(!vis[v])
                            {
                                vis[v]=1;
                                q.push(v);
                            }
                        }
                    }
                }
            }
        }
    }
    int ans=INF;
    for(int i=0;i<=k;i++)
        ans=min(ans,dp[1][i]);
    printf("%d\n",ans);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d%d",&a,&b,&m,&l,&k);
        for(int i=0;i<101;i++)
            g[i].clear();
        n=a+b;
        int u,v,len;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&len);
            lx now;
            now.len=len;
            now.v=v,g[u].push_back(now);
            now.v=u,g[v].push_back(now);
        }
        for(int i=0;i<101;i++)
            for(int j=0;j<101;j++)
            dis[i][j]=INF;

        for(int i=1;i<=n;i++)
        SPFA(i);

        spfa();
    }
}

ZOJ 1232 Adventure of Super Mario

时间: 2024-08-03 04:25:45

ZOJ 1232 Adventure of Super Mario的相关文章

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

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

UVA10269 Adventure of Super Mario(Floyd+DP)

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 be

uva 10269 Adventure of Super Mario (floyd + dijkstra)

uva 10269 Adventure of Super Mario 题目大意:有A个村庄,B座城堡,村庄编号从1-A, 城堡编号从A + 1 - A + B.马里奥住在1号村庄,公主被关在A + B号城堡.马里奥有一件宝物,可以让他瞬间跑过L的距离,但是这件宝物是有限制的.发动这件宝物的起点或终点必须是村庄或者城堡,并且不能穿过城堡.这样的宝物当然不能随便用,所以它的耐久度只有K,也就是最多只能有K次,就要拿到铁匠铺去修理了.现在,马里奥已经在A + B号城堡救到公主了,问马里奥最快返回1号村

UVA 10269 Adventure of Super Mario

主要时floyd判断出利用飞鞋生成的DIS .其他SPFA或DIJKSTRA都可以 #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include &

(floyd+DP) zoj 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 n

zoj1232Adventure of Super Mario(图上dp)

题目连接: 啊哈哈,点我点我 思路: 这个题目是一个图上dp问题,先floyd预处理出图上所有点的最短路,但是在floyd的时候,把能够用神器的地方预处理出来,也就是转折点地方不能为城堡..预处理完毕后,就是一个dp问题了...dp[][],两维分别表示到达的地点和使用神器的次数..这样这个问题就得到了解决.. 题目: Adventure of Super Mario Time Limit: 2 Seconds      Memory Limit: 65536 KB After rescuing

hdu-4417 Super Mario(树状数组 + 划分树)

题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is

HDU 4417 Super Mario (树状数组/线段树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble agai