作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。
输入格式:
输入第一行给出 4 个正整数 n、m、s、d,其中 n(2≤n≤500)是城市的个数,顺便假设城市的编号为 0 ~ (n−1);m 是快速道路的条数;s 是出发地的城市编号;d是目的地的城市编号。
第二行给出 n 个正整数,其中第 i 个数是第 i 个城市的救援队的数目,数字间以空格分隔。随后的 m 行中,每行给出一条快速道路的信息,分别是:城市 1、城市 2、快速道路的长度,中间用空格分开,数字均为整数且不超过 500。输入保证救援可行且最优解唯一。
输出格式:
第一行输出最短路径的条数和能够召集的最多的救援队数量。第二行输出从 s 到 d 的路径中经过的城市编号。数字间以空格分隔,输出结尾不能有多余空格。
输入样例:
4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2
输出样例:
2 60
0 1 3
#include<bits/stdc++.h>
using namespace std;
int n,m,s,d;
const int maxn=500;
int rescue[maxn];
int paths[maxn]={0};
int max_rescue[maxn]={0};
int pre[maxn]={-1};
int dist[maxn]={INT_MAX};
bool vis[maxn]={false};
vector<vector<pair<int,int>>>graph(maxn);
void dijkstra(){
fill(dist,dist+maxn,INT_MAX);
fill(paths,paths+maxn,0);
fill(max_rescue,max_rescue+maxn,0);
fill(pre,pre+maxn,-1);
fill(vis,vis+maxn,false);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
dist[s]=0;
pq.push({dist[s],s});
paths[s]=1;
max_rescue[s]=rescue[s];
while(!pq.empty()){
int u=pq.top().second;
pq.pop();
if(u==d){
break;
}
if(vis[u])continue;
vis[u]=true;
for(auto edge:graph[u]){
int v=edge.second;
int w=edge.first;
if(dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
pq.push({dist[v],v});
paths[v]=paths[u];
max_rescue[v]=max_rescue[u]+rescue[v];
pre[v]=u;
}else if(dist[v]==dist[u]+w){
paths[v]+=paths[u];
if(max_rescue[v]<max_rescue[u]+rescue[v]){
max_rescue[v]=max_rescue[u]+rescue[v];
pre[v]=u;
}
}
}
}
}
void printpath(int v){
if(v==s){
cout<<v;
return;
}
printpath(pre[v]);
cout<<" "<<v;
}
void solve(){
cin>>n>>m>>s>>d;
for(int i=0;i<n;i++){
cin>>rescue[i];
}
for(int i=0;i<m;i++){
int u,v,l;
cin>>u>>v>>l;
graph[u].push_back({l,v});
graph[v].push_back({l,u});
}
dijkstra();
cout<<paths[d]<<" "<<max_rescue[d]<<endl;
printpath(d);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
solve();
return 0;
}