zoukankan      html  css  js  c++  java
  • zoj1232Adventure of Super Mario(图上dp)

    题目连接:

    啊哈哈。点我点我

    思路:

    这个题目是一个图上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 ",dp[A+B][K]); } int main() { scanf("%d",&t); while(t--) { read_Graph(); floyd(); solve(); } return 0; }



  • 相关阅读:
    在centos7上安装ClamAV杀毒,并杀毒(centos随机英文10字母)成功
    在centos7上安装Jenkins
    Spring cache简单使用guava cache
    Spring resource bundle多语言,单引号format异常
    如何优化coding
    IIS配置中出现HRESULT:0X80070020错误
    如何解决:对应的服务器 tls 为 tls 1.0,小程序要求的TLS版本必须大于等于1.2问题
    微信小程序--后台交互/wx.request({})方法/渲染页面方法 解析
    微信小程序页面带参数跳转及接收参数内容navigator
    微信小程序阿里云服务器https搭建
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7193985.html
Copyright © 2011-2022 走看看