最短路,注意最短路相同时取最小花费的路径。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 505
int map_d[MAX][MAX],map_c[MAX][MAX];//分别记录一条路得距离与花费
int vis[MAX],dis[MAX],cost[MAX],path[MAX];
int n,m,s,d;
void Dijkstra(int s){
for(int i=0;i<n;i++){
dis[i]=map_d[s][i];
cost[i]=map_c[s][i];
vis[i]=0;
if(dis[i]!=INF)
path[i]=s;
else
path[i]=-1;
}
//path[s]=-1;
vis[s]=1;//初始化
dis[s]=0;
cost[s]=0;
for(int i=1;i<n;i++){
int index,tmp=INF;
for(int j=0;j<n;j++)//找直接相邻最短的那个点
if(!vis[j]&&dis[j]<tmp){
tmp=dis[j];
index=j;
}
vis[index]=1;
for(int j=0;j<n;j++){
if(!vis[j]&&map_d[index][j]!=INF){
if(dis[j]>dis[index]+map_d[index][j]){
dis[j]=dis[index]+map_d[index][j];
cost[j]=cost[index]+map_c[index][j];
path[j]=index;
}
else if(dis[j]==dis[index]+map_d[index][j]){
if(cost[j]>cost[index]+map_c[index][j]){
cost[j]=cost[index]+map_c[index][j];
path[j]=index;
}
}
}
}
}
}
void Print2(int s,int d){
int t=d;
stack<int> res;
while(t!=s){
res.push(t);
t=path[t];
}
printf("%d ",s);
while(!res.empty()){
printf("%d ",res.top());
res.pop();
}
}
int main(){
//freopen("input.txt","r",stdin);
scanf("%d %d %d %d",&n,&m,&s,&d);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
map_d[i][j]=map_c[i][j]=INF;
}
for(int i=0;i<m;i++){
int t1,t2,t3,t4;
scanf("%d %d %d %d",&t1,&t2,&t3,&t4);
if(map_d[t1][t2]>t3){
map_d[t1][t2]=map_d[t2][t1]=t3;
map_c[t1][t2]=map_c[t2][t1]=t4;
}
else if(map_d[t1][t2]==t3){
if(map_c[t1][t2]>t4)
map_c[t1][t2]=map_c[t2][t1]=t4;
}
}
Dijkstra(s);
Print2(s,d);
printf("%d %d\n",dis[d],cost[d]);
return 0;
}