#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <cstring>
using namespace std;
const int inf=1000000000;
int G[510][510];
bool vis[510];
int hands[510],d[510];
vector<int> pre[510];
int N,M,st,ed;
void Dij(int st)
{
memset(vis,false,sizeof(vis));
fill(d,d+510,inf);
d[st]=0;
for(int i=0;i<N;i++)
{
int u=-1,MIN=inf;
for(int j=0;j<N;j++)
{
if(vis[j]==false&&d[j]<MIN)
{
u=j;
MIN=d[j];
}
}
if(u==-1)return;
vis[u]=true;
for(int v=0;v<N;v++)
{
if(G[u][v]<inf&&vis[v]==false)
{
if(d[u]+G[u][v]<d[v])
{
d[v]=d[u]+G[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if(d[u]+G[u][v]==d[v])
{
pre[v].push_back(u);
}
}
}
}
}
vector<int> temppath,path;
int maxhands=-1,pathnum=0;
void DFS(int now)
{
if(now==st)
{
pathnum++;
temppath.push_back(now);
int temphands=0;
for(int i=0;i<temppath.size();i++)
{
temphands+=hands[temppath[i]];
}
if(temphands>maxhands)
{
path=temppath;
maxhands=temphands;
}
temppath.pop_back();
return;
}
temppath.push_back(now);
for(int i=0;i<pre[now].size();i++)
{
DFS(pre[now][i]);
}
temppath.pop_back();
}
int main() {
scanf("%d%d%d%d",&N,&M,&st,&ed);
fill(G[0],G[0]+510*510,inf);
for(int i=0;i<N;i++)
{
scanf("%d",&hands[i]);
}
for(int i=0;i<M;i++)
{
int c1,c2,l;
scanf("%d%d%d",&c1,&c2,&l);
G[c1][c2]=l;
G[c2][c1]=l;
}
Dij(st);
DFS(ed);
printf("%d %d",pathnum,maxhands);
return 0;
}
PAT甲1003 Emergency (25)(25 分)
最新推荐文章于 2022-04-13 09:17:15 发布