法一:Dijkstra
#include<iostream>
#include<cstring>
#include<algorithm>
#define mm(a,x) memset(a,x,sizeof(a))
#define inf 0x3f3f3f3f
const int maxn =510;
using namespace std;
int n,m,s,d;
int map[maxn][maxn];
int dist[maxn];
int cost[maxn][maxn];
int dicost[maxn];
int path[maxn];
int vis[maxn];
void dijkstra(){
mm(dist,inf);mm(dicost,inf);
for(int i=0;i<n;i++) path[i]=i;
dist[s]=0;
dicost[s]=0;
for(int i=0;i<n;i++){
int u=-1,minn=inf;
for(int j=0;j<n;j++){
if(!vis[j]&&dist[j]<minn){
u=j;minn=dist[j];
}
}
if(u==-1) return ;
vis[u]=1;
for(int v=0;v<n;v++){
if(!vis[v]&&map[u][v]!=inf){
if(dist[u]+map[u][v]<dist[v]){
dist[v]=dist[u]+map[u][v];
dicost[v]=dicost[u]+cost[u][v];
path[v]=u;
}else if(dist[u]+map[u][v]==dist[v]){
if(dicost[u]+cost[u][v]<dicost[v])
{
dicost[v]=dicost[u]+cost[u][v];
path[v]=u;
}
}
}
}
}
}
void dfs(int v){
if(v==s){
cout<<v<<" ";
return ;
}
dfs(path[v]);
cout<<v<<" ";
}
int main(){
cin>>n>>m>>s>>d;
mm(map,inf);
for(int i=0;i<m;i++){
int t1,t2,t3,t4;cin>>t1>>t2>>t3>>t4;
map[t1][t2]=map[t2][t1]=t3;
cost[t1][t2]=cost[t2][t1]=t4;
}
dijkstra();
dfs(d);
cout<<dist[d]<<" "<<dicost[d];
return 0;
}
法二:Dijkstra+DFS
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#define mm(a,x) memset(a,x,sizeof(a))
#define inf 0x3f3f3f3f
const int maxn =510;
using namespace std;
int n,m,s,d;
int map[maxn][maxn],cost[maxn][maxn];
int dist[maxn],minCost=inf;
int vis[maxn];
vector<int > pre[maxn];
vector<int > tempPath,path;
void dijkstra(){
mm(dist,inf);
dist[s]=0;
for(int i=0;i<n;i++){
int u=-1,minn=inf;
for(int j=0;j<n;j++){
if(!vis[j]&&dist[j]<minn){
u=j;minn=dist[j];
}
}
if(u==-1) return ;
vis[u]=1;
for(int v=0;v<n;v++){
if(!vis[v]&&map[u][v]!=inf){
if(dist[u]+map[u][v]<dist[v]){
dist[v]=dist[u]+map[u][v];
pre[v].clear();
pre[v].push_back(u);
}else if(dist[u]+map[u][v]==dist[v]){
pre[v].push_back(u);
}
}
}
}
}
void dfs(int v){
if(v==s){
tempPath.push_back(v);
int tempCost=0;
for(int i=tempPath.size()-1;i>0;i--){
int id=tempPath[i],idNext=tempPath[i-1];
tempCost+=cost[id][idNext];
}
if(tempCost<minCost){
minCost=tempCost;
path=tempPath;
}
tempPath.pop_back();
return ;
}
tempPath.push_back(v);
for(int i=0;i<pre[v].size();i++){
dfs(pre[v][i]);
}
tempPath.pop_back();
}
int main(){
cin>>n>>m>>s>>d;
mm(map,inf);
for(int i=0;i<m;i++){
int t1,t2,t3,t4;cin>>t1>>t2>>t3>>t4;
map[t1][t2]=map[t2][t1]=t3;
cost[t1][t2]=cost[t2][t1]=t4;
}
dijkstra();
dfs(d);
for(int i=path.size()-1;i>=0;i--){
cout<<path[i]<<" ";
}
cout<<dist[d]<<" "<<minCost;
return 0;
}