你来到一个迷宫前。该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数。还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定了你的起点和终点房间,你首要目标是从起点尽快到达终点,在满足首要目标的前提下,使得你的得分总和尽可能大。现在问题来了,给定房间、道路、分数、起点和终点等全部信息,你能计算在尽快离开迷宫的前提下,你的最大得分是多少么?
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