判断路径相等时的情况
#include <iostream> #include <cstring> #include <algorithm> #include <queue> using namespace std; #define pf printf #define sf scanf #define mp make_pair #define INF 0x3f3f3f3f const int MAXN = 1000 + 5; int n,m; struct node { int d,fee; }g[MAXN][MAXN]; int dis[MAXN],f[MAXN],vis[MAXN]; void dijkstra(int v) { int i,j,mind,minf,dir; for(i=1;i<=n;i++) { dis[i] = f[i] = INF; vis[i] = 0; } dis[v] = f[v] = 0; vis[v] = 1; for(i=1;i<=n;i++) { mind = minf = INF; for(j=1;j<=n;j++) { if(!vis[j] && (dis[j]<mind || (dis[j]==mind && f[j]<minf))) { mind = dis[j]; minf = f[j]; dir = j; } } vis[dir] = 1; for(j=1;j<=n;j++) { if(!vis[j] && g[dir][j].d<INF && (dis[j] > (dis[dir]+g[dir][j].d) || (dis[j] == (dis[dir]+g[dir][j].d) && f[j]>f[dir]+g[dir][j].fee))) { dis[j] = dis[dir]+g[dir][j].d; f[j]=f[dir]+g[dir][j].fee; } } } } int main() { int i,j; int a,b,d,fee,st,ed; while(sf("%d %d",&n,&m)==2,m+n) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { g[i][j].d = g[i][j].fee = (i==j ? 0:INF); } } for(i=0;i<m;i++) { sf("%d%d%d%d",&a,&b,&d,&fee); if(d < g[a][b].d) { g[a][b].d = g[b][a].d = d; g[a][b].fee = g[b][a].fee = fee; } else if(d == g[a][b].d) g[a][b].fee = g[b][a].fee = min(g[a][b].fee,fee); } sf("%d%d",&st,&ed); dijkstra(st); pf("%d %d ",dis[ed],f[ed]); } return 0;