• 迷宫游戏(单源最短路)


    个人心得:对于复杂抽象的算法还是比较模糊,希望以后有待加强。这题就是用dijskrual算法,在算出最短时间的时候进行适当的更改,还是比较模糊。

    基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
     收藏
     关注
    你来到一个迷宫前。该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数。还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定了你的起点和终点房间,你首要目标是从起点尽快到达终点,在满足首要目标的前提下,使得你的得分总和尽可能大。现在问题来了,给定房间、道路、分数、起点和终点等全部信息,你能计算在尽快离开迷宫的前提下,你的最大得分是多少么?
    Input
    第一行4个整数n (<=500), m, start, end。n表示房间的个数,房间编号从0到(n - 1),m表示道路数,任意两个房间之间最多只有一条道路,start和end表示起点和终点房间的编号。
    第二行包含n个空格分隔的正整数(不超过600),表示进入每个房间你的得分。
    再接下来m行,每行3个空格分隔的整数x, y, z (0<z<=200)表示道路,表示从房间x到房间y(双向)的道路,注意,最多只有一条道路连结两个房间, 你需要的时间为z。
    输入保证从start到end至少有一条路径。
    Output
    一行,两个空格分隔的整数,第一个表示你最少需要的时间,第二个表示你在最少时间前提下可以获得的最大得分。
    Input示例
    3 2 0 2
    1 2 3
    0 1 10
    1 2 11
    Output示例
    21 6
     1 #include<iostream>
     2 #include<cstring>
     3 #include<vector>
     4 #include<cstdio>
     5 #include<queue>
     6 #include<algorithm>
     7 using namespace std;
     8 const int inf = 0x3f3f3f3f;
     9   int n,m,start,send;
    10   int a[505];
    11   int ans[505];
    12   int d[505],book[505];
    13   struct node
    14   {
    15       int v,s;
    16       node(int x,int y):v(x),s(y){}
    17       bool operator <(const node&r)const{
    18         return r.s<s;
    19       }
    20   };
    21   struct edge
    22   {
    23       int v,w;
    24       edge(int x,int y):v(x),w(y){}
    25   };
    26   vector <edge>E[505];
    27   void getedge(int x,int y,int z){
    28      E[x].push_back(edge(y,z));
    29   }
    30   void dij()
    31   {
    32       priority_queue<node>q;
    33       for(int i=0;i<n;i++){
    34         d[i]=inf;
    35         book[i]=0;
    36         ans[i]=0;
    37       }
    38       d[start]=0;
    39       ans[start]=a[start];
    40       q.push(node(start,0));
    41       while(!q.empty())
    42       {
    43           node t=q.top();q.pop();
    44           int u=t.v;
    45           if(book[u])
    46             continue;
    47           book[u]=1;
    48           for(int i=0;i<E[u].size();i++)
    49           {
    50               int v=E[u][i].v;
    51               int w=E[u][i].w;
    52               if(!book[v]&&d[u]+w<d[v]){
    53                 d[v]=d[u]+w;
    54                 ans[v]=ans[u]+a[v];
    55                 q.push(node(v,d[v]));
    56               }
    57               else if(!book[v]&&d[u]+w==d[v])
    58                 ans[v]=max(ans[v],ans[u]+a[v]);
    59           }
    60       }
    61 
    62   }
    63   int main()
    64   {
    65       scanf("%d%d%d%d",&n,&m,&start,&send);
    66       for(int i=0;i<n;i++)
    67         scanf("%d",&a[i]);
    68       while(m--){
    69 
    70         int x,y,z;
    71         scanf("%d%d%d",&x,&y,&z);
    72         getedge(x,y,z);
    73         getedge(y,x,z);
    74       }
    75       dij();
    76       printf("%d %d
    ",d[send],ans[send]);
    77       return 0;
    78 
    79   }


  • 相关阅读:
    ASP.NET 跨域请求之jQuery的ajax jsonp的使用解惑 (转载)
    调用WebService报错404问题 (转载)
    使你的ActiveX控件执行时不弹出安全性提示(转载)
    FFmpeg for Android compiled with x264, libass, fontconfig, freetype and fribidi
    ffmpeg: ‘UINT64_C’ was not declared in this scope (转)
    vs中ffmpeg release版本崩溃问题(转)
    #pragma execution_character_set("utf-8")
    上半年
    C获取当前时间
    linux 信号量之SIGNAL 0<转>
  • 原文地址:https://www.cnblogs.com/blvt/p/7532514.html
Copyright © 2020-2023  润新知