[51NOD] 1459 迷宫游戏 [最短路][spfa]

你来到一个迷宫前。该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数。还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定了你的起点和终点房间,你首要目标是从起点尽快到达终点,在满足首要目标的前提下,使得你的得分总和尽可能大。现在问题来了,给定房间、道路、分数、起点和终点等全部信息,你能计算在尽快离开迷宫的前提下,你的最大得分是多少么?
Input
第一行4个整数n (<=500), m, start, end。n表示房间的个数,房间编号从0到(n - 1),m表示道路数,任意两个房间之间最多只有一条道路,start和end表示起点和终点房间的编号。
第二行包含n个空格分隔的正整数(不超过600),表示进入每个房间你的得分。
再接下来m行,每行3个空格分隔的整数x, y, z (0

解题报告

类spfa走一遍就ok了

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
#define MAX_N 505
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int,int> P;
P val[MAX_N];//first->time second->score
int n;

vector<int> score;
vector<pair<int,int>/*first->to second->cost*/ > edge[MAX_N];

P spfa(int start,int end){
    memset(val,0x3f,sizeof(val));
    queue<int> que;
    val[start]=make_pair(0,score[start]);
    que.push(start);
    while(!que.empty()){
        int from=que.front();que.pop();
        for(int i=0;i<edge[from].size();i++){
            int to=edge[from][i].first,cost=edge[from][i].second;
            if(val[to].first>val[from].first+cost
            ||val[to].first==val[from].first+cost&&val[to].second<val[from].second+score[to]){
                que.push(to);
                val[to]=make_pair(val[from].first+cost,val[from].second+score[to]);
            }
        }
    }
    return val[end];
}

int main()
{
    int m,start,end;
    scanf("%d%d%d%d",&n,&m,&start,&end);
    for(int i=0,g;i<n;i++){
        scanf("%d",&g);
        score.push_back(g);
    }
    for(int i=0,x,y,cost;i<m;i++){
        scanf("%d%d%d",&x,&y,&cost);
        edge[x].push_back(make_pair(y,cost));
        edge[y].push_back(make_pair(x,cost));
    }

    P ans=spfa(start,end);
    printf("%d %d\n",ans.first,ans.second);
    return 0;
}

原题链接
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值