题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376
- 要看清楚到底是有向图还是无向图(建图的时候要用)
- 用哪种方法求最短路
- 仔细考虑附带问题(像本题中的最短路的个数,和在最短路条件下的最大营救人数)
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct edge{
int u,v,w,next;
}e[1100];
int team[505], head[505], cnt, sum[505]; //c1-i最短路径下的team数
int n,m,c1,c2, dis[505];
int path_num[505];
bool vis[505];
void add(int a, int b, int c){//邻接表存图
e[++cnt].u = a;
e[cnt].v = b;
e[cnt].w = c;
e[cnt].next = head[a];
head[a] = cnt;
}
void dijkstra(){ //优先队列优化的dijkstra算法
for (int i = 0; i < n; i++){
dis[i] = 1e9; vis[i] = false;
}
dis[c1] = 0; sum[c1] = team[c1]; path_num[c1] = 1;
priority_queue<pair<int,int> > que;
que.push(make_pair(0, c1));
while (!que.empty()){
int cur = que.top().second;
//cout<<cur<<endl;
que.pop();
if (vis[cur]) continue;
vis[cur] = true;
for (int i = head[cur]; i; i = e[i].next){
int now = e[i].v;
if (vis[now]) continue;
if (dis[cur]+e[i].w < dis[now]){
dis[now] = dis[cur] + e[i].w;
que.push(make_pair(-dis[now], now));
path_num[now] = path_num[cur];//如果最短路更新,那么path_num和sum都要重新赋值
sum[now] = sum[cur]+team[now];
}else if (dis[cur]+e[i].w == dis[now]){
path_num[now] += path_num[cur];
sum[now] = max(sum[now], sum[cur]+team[now]);
}
}
}
}
int main()
{
cin>>n>>m>>c1>>c2;
for (int i = 0; i < n; i++) cin >> team[i];
for (int i = 0; i < m; i++){
int a,b,c;
cin>>a>>b>>c;
add(a, b, c);
add(b, a, c); //这里要进行双边存储、又因为是双向建边,因为edge要开点的两倍多一点
}
dijkstra();
cout<<path_num[c2]<<" "<<sum[c2];
return 0;
}