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 <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 110
const int INF = 0x3f3f3f3f ;
int dis[MAXN][MAXN];
int A,B,M,L,K;
int dp[MAXN][MAXN];
bool inq[MAXN][MAXN];
struct node
{
    int idx;
    int cnt;
};
void read()
{
    scanf("%d%d%d%d%d",&A,&B,&M,&L,&K);
    memset(dis,0x3f,sizeof(dp));
    for (int i = 0; i <= A + B; i++) dis[i][i] = 0;
    while (M--)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        dis[u][v] = dis[v][u] = w;
    }
    for (int k = 1; k <= A; k++)
        for (int i = 1; i <= A + B; i++)
          for (int j = 1; j <= A + B; j++)
          dis[i][j] = min(dis[i][j],dis[i][k] + dis[k][j]);
}
void SPFA()
{
    memset(inq,false,sizeof(inq));
    memset(dp,0x3f,sizeof(dp));
    queue<node>q; while (!q.empty()) q.pop();
    node tmp;
    tmp.cnt = K; tmp.idx = A + B;
    inq[A + B][K] = true;
    dp[A + B][K] = 0;
    q.push(tmp);
    while (!q.empty())
    {
        node tmp = q.front(); q.pop();
        int u = tmp.idx , num = tmp.cnt;
        inq[u][num] = false;
        //printf("%d %d \n",u,num);
        for (int i = 1; i <= A + B; i++)
        {
            if (i == u) continue;
            if (dis[u][i] != INF)
            {
          //      printf("dis[%d][%d] = %d\n",u,i,dis[u][i]);
          //      printf("%d\n",dp[i][num - 1]);
                if (num > 0 && dis[u][i] <= L && dp[i][num - 1] > dp[u][num])
                {
                    dp[i][num - 1] = dp[u][num];
                    if (!inq[i][num - 1])
                    {
                        inq[i][num - 1] = true;
                        node temp;
                        temp.idx = i;
                        temp.cnt = num - 1;
                        q.push(temp);
                    }
                }
                if (dp[i][num] > dp[u][num] + dis[u][i])
                {
                    dp[i][num] = dp[u][num] + dis[u][i];
                    if (!inq[i][num])
                    {
                        inq[i][num] = true;
                        node temp;
                        temp.idx = i;
                        temp.cnt = num;
                        q.push(temp);
                    }
                }
            }
        }
    }
    int ans = INT_MAX;
    for (int i = 0; i <= K; i++)
        ans = min(ans,dp[1][i]);
    printf("%d\n",ans);
}
int main()
{
   // freopen("sample.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while (T--)
    {
        read();
        SPFA();
    }
    return 0;
}
时间: 2024-10-28 11:32:53

UVA 10269 Adventure of Super Mario的相关文章

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号村

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

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

ZOJ 1232 Adventure of Super Mario

最短路+DP(个人用的SPFA+完全背包) 做了一上午--开始想用SPFA+BFS.但是写了半天越写越乱,放弃了. 就想到了是不是可以当作背包问题(背出病了--)把鞋子可以使用的次数当作背包容量.做完全背包. 先N次SPFA把 各点的最短距离算出来,其实比较适合Floyd.(个人用vector实现伪邻接表,然后SPFA) 然后SPFA更新路径的时候,当鞋子使用次数不为0的时候,做完全背包. 还有个忧伤的地方就是我把INF=0x7fffffff.结果加上一个数 变成负数了.一直不对. 找了半天,最

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

hdu4417 Super Mario 树状数组离线/划分树

http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2720    Accepted Submission(s): 1322 Problem Description Mario is world-famous plumber