作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。
输入格式:
输入第一行给出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 <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
int N,M,S,D;
const int MAX_V = 501;
int map[MAX_V][MAX_V];
int dis[MAX_V];
int cnt[MAX_V];
int sum[MAX_V];
int val[MAX_V];
bool vis[MAX_V];
int pre[MAX_V];
void dijkstra(int s){
while(true){
int v = -1;
for(int u=0;u<N;u++){
if(!vis[u] && (v==-1||dis[u]<dis[v])){
v = u;
}
}
if(v==-1) break;
vis[v] = true;
for(int u=0;u<N;u++){
if(!vis[u]){
if(dis[u]>map[v][u]+dis[v]){
dis[u] = map[v][u] + dis[v];
cnt[u] = cnt[v];
pre[u] = v;
sum[u] = val[u] + sum[v];
}
else if(dis[u]==map[v][u]+dis[v]){
cnt[u] = cnt[u] + cnt[v];
if(sum[u]<sum[v]+val[u]){
sum[u] = sum[v] + val[u];
pre[u] = v;
}
}
}
}
}
}
bool flag = false;
void get_path(int d){
if(d!=-1){
get_path(pre[d]);
if(flag){
cout<<" ";
}
else{
flag = true;
}
cout<<d;
}
}
int main(){
cin>>N>>M>>S>>D;
for(int i=0;i<N;i++){
cin>>val[i];
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(i==j)
map[i][j] = 0;
else
map[i][j] = INF;
}
}
fill(cnt,cnt+N,0);
fill(vis,vis+N,false);
fill(dis,dis+N,INF);
fill(sum,sum+N,0);
fill(pre,pre+N,-1);
for(int i=0;i<M;i++){
int x,y,z;
cin>>x>>y>>z;
map[x][y] = map[y][x] = z;
}
cnt[S] = 1;
sum[S] = val[S];
dis[S] = 0;
dijkstra(S);
cout<<cnt[D]<<" "<<sum[D]<<endl;
get_path(D);
return 0;
}