#include<iostream>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=1010;
const int INF=1000000000;
int G[maxn][maxn],cost[maxn][maxn];
int d[maxn],c[maxn],vis[maxn],pre[maxn];
int n,m,st,ed;
void dijkstra(int s)
{
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
c[i]=d[i]=INF;
pre[i]=i;
}
c[s]=0;
d[s]=0;
int MIN,u;
for(int i=0;i<n;i++)
{
MIN=INF;
u=-1;
for(int j=0;j<n;j++)
{
if(vis[j]==0&&d[j]<MIN)
{
MIN=d[j];
u=j;
}
}
if(u==-1)
return;
vis[u]=1;
for(int v=0;v<n;v++)
{
if(vis[v]==0&&G[u][v]!=INF)
{
if(d[v]>d[u]+G[u][v])
{
d[v]=d[u]+G[u][v];
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
else if(d[v]==d[u]+G[u][v])
{
if(c[v]>c[u]+cost[u][v])
{
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
}
}
}
}
}
void DFS(int s)
{
if(s==st)
{
printf("%d ",s);
return;
}
DFS(pre[s]);
printf("%d ",s);
}
int main()
{
scanf("%d%d%d%d",&n,&m,&st,&ed);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
G[i][j]=INF;
cost[i][j]=INF;
}
}
for(int i=0;i<m;i++)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
G[a][b]=G[b][a]=c;
cost[a][b]=cost[b][a]=d;
}
dijkstra(st);
DFS(ed);//从末尾开始
printf("%d %d\n",d[ed],c[ed]);
return 0;
}
PAT——A1030 Travel Plan(dijkstra算法)
猜你喜欢
转载自blog.csdn.net/qq_42232118/article/details/82912922
今日推荐
周排行