Description
你来到一个迷宫前。该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数。还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定了你的起点和终点房间,你首要目标是从起点尽快到达终点,在满足首要目标的前提下,使得你的得分总和尽可能大。现在问题来了,给定房间、道路、分数、起点和终点等全部信息,你能计算在尽快离开迷宫的前提下,你的最大得分是多少么?
Input
第一行4个整数n (<=500), m, start, end。n表示房间的个数,房间编号从0到(n - 1),m表示道路数,任意两个房间之间最多只有一条道路,start和end表示起点和终点房间的编号。
第二行包含n个空格分隔的正整数(不超过600),表示进入每个房间你的得分。
再接下来m行,每行3个空格分隔的整数x, y, z (0
Output
一行,两个空格分隔的整数,第一个表示你最少需要的时间,第二个表示你在最少时间前提下可以获得的最大得分。
Input示例
3 2 0 2
1 2 3
0 1 10
1 2 11
Output示例
21 6
解题思路
spfa求最短路,并在路径长度相等时更新得分最大值.
代码实现
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn =507;
const int maxm=150000;
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
struct node
{
int u;
int v;
int value;
int ne;
} edge[maxm*2];
int score[maxn];
int head[maxn],tot;
int dis[maxn],point[maxn];
bool vis[maxn];
int ans=0;
void init()
{
memset(head,-1,sizeof(head));
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
tot=0;
}
void addedge(int u,int v,int value)
{
edge[tot].u=u;
edge[tot].v=v;
edge[tot].value=value;
edge[tot].ne=head[u];
head[u]=tot++;
}
void spfa(int s,int e)
{
queue<int>qu;
qu.push(s);
vis[s]=1;
dis[s]=0;
score[s]=point[s];
while(!qu.empty())
{
int tmp=qu.front();
qu.pop();
vis[tmp]=0;
for(int i=head[tmp]; i!=-1; i=edge[i].ne)
{
int to=edge[i].v;
if(dis[to]>dis[tmp]+edge[i].value||
((dis[to]==dis[tmp]+edge[i].value)&&(score[to]<score[tmp]+point[to])))
{
dis[to]=dis[tmp]+edge[i].value;
score[to]=score[tmp]+point[to];
if(!vis[to])
{
qu.push(to);
vis[to]=1;
}
}
}
}
}
int main()
{
IO;
init();
int n,m,s,e;
int u,v,value;
cin>>n>>m>>s>>e;
for(int i=0; i<n; i++)
cin>>point[i];
for(int i=0; i<m; i++)
{
cin>>u>>v>>value;
addedge(u,v,value);
addedge(v,u,value);
}
spfa(s,e);
cout<<dis[e]<<" "<<score[e]<<endl;
return 0;
}