题目大意:无向图,求st到de的最短路条数,以及最短路下节点和的最大值。
算法:dijistra最短路(堆优化),最短路计数,sum记录最短路数量,ans记录最短路下的节点最大值
代码如下
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#define N 505
#define ll long long
using namespace std;
int army[N];
struct node
{
int next;
int to;
int val;
}edg[N<<1];
int head[N],vis[N],ans[N],sum[N],dis[N];
int cnt=1;
void init()
{
cnt=1;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edg[cnt].next=head[u];
edg[cnt].to=v;
edg[cnt].val=w;
head[u]=cnt++;
}
struct no
{
int po,d;
};
bool operator < (no a,no b)
{
return a.d > b.d;
}
void dijistra(int rt)
{
memset(dis,0x3f3f3f3f,sizeof(dis));
priority_queue<no>Q;
no point;
point.po=rt;
point.d=0;
dis[rt]=0;
Q.push(point);
while(!Q.empty())
{
no p=Q.top();
Q.pop();
int u=p.po;
if(vis[u]) continue;
vis[u]=1;
for(int i = head[u];i != -1;i=edg[i].next)
{
int to=edg[i].to;
if(dis[to]>edg[i].val+dis[u])
{
sum[to]=sum[u];
ans[to]=ans[u]+army[to];
dis[to]=edg[i].val+dis[u];
no temp;
temp.d=dis[to];
temp.po=to;
Q.push(temp);
}else if(dis[to]==dis[u]+edg[i].val)
{
sum[to]+=sum[u];
if(army[to]+ans[u]>ans[to])
{
ans[to]=ans[u]+army[to];
}
}
//vis[u]=0;
}
}
}
int main()
{
int n,m,st,de;
cin >> n >> m >> st >> de;
init();
for(int i = 0;i < n;i++)
{
cin >> army[i];
}
for(int i = 1;i <= m;i++)
{
int a,b,c;
cin >> a >> b >> c;
add(a,b,c);
add(b,a,c);
}
sum[st]=1;
ans[st]=army[st];
dijistra(st);
cout << sum[de] <<" "<<ans[de];
return 0 ;
}