1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include <cstdio> #include <algorithm> using namespace std; const int maxn=510; const int INF=1e9; int n,m,st,ed; int G[maxn][maxn],cost[maxn][maxn],d[maxn],pre[maxn],c[maxn]={0}; bool vis[maxn]={false};
void Dijkstra(int s){ fill(d,d+maxn,INF); d[s]=0; c[s]=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]; c[v]=c[u]+cost[u][v]; pre[v]=u; }else if(d[u]+G[u][v]==d[v]&&c[u]+cost[u][v]<c[v]){ c[v]=c[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(){ scanf("%d%d%d%d",&n,&m,&st,&ed); int c1,c2,dis,ct; fill(G[0],G[0]+maxn*maxn,INF); fill(cost[0],cost[0]+maxn*maxn,INF); for(int i=0;i<m;i++){ scanf("%d%d%d%d",&c1,&c2,&dis,&ct); G[c1][c2]=G[c2][c1]=dis; cost[c1][c2]=cost[c2][c1]=ct; } Dijkstra(st); DFS(ed); printf("%d %d",d[ed],c[ed]);
return 0; }
|