#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1001;
const int INF=10000000;
int n,m,st,ed;
int d[maxn],G[maxn][maxn],cost[maxn][maxn],w[maxn],pre[maxn];
bool vis[maxn]={false};
void Dijkstra(int st){
fill(d,d+maxn,INF);
fill(w,w+maxn,INF);
for(int i=0;i<n;i++)
pre[i]=i;
d[st]=0;
w[st]=0;
for(int i=0;i<n;i++){
int u=-1,MIN=INF;
for(int j=0;j<n;j++){
if(vis[j]==false&&d[j]<MIN){
u=j;
MIN=d[j];
}
}
if(u==-1)
return;
vis[u]=true;
for(int v=0;v<n;v++){
if(vis[v]==false&&G[u][v]!=INF){
if(d[u]+G[u][v]<d[v]){
d[v]=d[u]+G[u][v];
w[v]=w[u]+cost[u][v];
pre[v]=u;
}
else if(d[v]==d[u]+G[u][v]){
if(w[u]+cost[u][v]<w[v]){
w[v]=w[u]+cost[u][v];
pre[v]=u;
}
}
}
}
}
}
void DFS(int v){
if(v==st){
printf("%d ",v);
return;
}
DFS(pre[v]);
printf("%d ",v);
}
int main(){
int u,v,p,q;
scanf("%d %d %d %d",&n,&m,&st,&ed);
fill(G[0],G[0]+maxn*maxn,INF);
for(int i=0;i<m;i++){
scanf("%d %d %d %d",&u,&v,&p,&q);
G[u][v]=p;
G[v][u]=p;
cost[u][v]=q;
cost[v][u]=q;
}
Dijkstra(st);
DFS(ed);
printf("%d %d",d[ed],w[ed]);
return 0;
}
甲级pat-1030
最新推荐文章于 2022-10-20 15:02:01 发布