结构体形式的Dijkstra
注意,费用和总路程都要处理,总路程为先!
#include"stdio.h"#include"string.h"#define INF 999999struct node{ int dis,cost;}map[1001][1001],f[1001];int n,m,s,e;int mark[1001];void dijkstra(){ int i,j,k,min; memset(mark,0,sizeof(mark)); for(i=1;i<=n;i++) { f[i].dis=map[s][i].dis; f[i].cost=map[s][i].cost; } mark[s]=0;f[s].cost=0;f[s].dis=0; for(i=1;i<=n;i++) { min=INF; for(j=1;j<=n;j++) { if(!mark[j]&&f[j].disf[k].dis+map[k][j].dis&&map[k][j].dis!=INF) { f[j].dis=f[k].dis+map[k][j].dis; f[j].cost=f[k].cost+map[k][j].cost; } else if(f[j].dis==f[k].dis+map[k][j].dis&&map[k][j].dis!=INF) { if(f[j].cost>f[k].cost+map[k][j].cost) f[j].cost=f[k].cost+map[k][j].cost; } } } }}int main(){ int i,j,a,b,d,p; while(scanf("%d%d",&n,&m)!=EOF,n+m) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j) map[i][j].cost=map[i][j].dis=0; else map[i][j].cost=map[i][j].dis=INF; } } for(i=0;i d) { map[a][b].dis=map[b][a].dis=d; map[a][b].cost=map[b][a].cost=p; } } scanf("%d%d",&s,&e); dijkstra(); printf("%d %d\n",f[e].dis,f[e].cost); } return 0;}