标签:
#include <iostream> #include <cstring> #include <cstdio> #define maxn 1007 #define INF 65535 using namespace std; int start,e; int n,m; int map[maxn][maxn]; int cost[maxn][maxn]; void Dijkstra() { int v,Min,vis[maxn]; int d[maxn],c[maxn]; for(int i = 1;i <= n;i++) { d[i] = map[start][i]; c[i] = cost[start][i]; } memset(vis,0,sizeof(vis)); vis[start] = 1; for(int i = 1;i <= n;i++) { if(vis[e]) break; Min = INF; for(int j = 1;j <= n;j++) if(!vis[j] && d[j]<Min) Min = d[v=j]; vis[v] = 1; for(int j = 1;j <= n;j++) if(!vis[j] && map[v][j]<INF) { if(d[j] > d[v]+map[v][j]) { d[j] = d[v]+map[v][j]; c[j] = c[v]+cost[v][j]; } else if(d[j] == d[v]+map[v][j]) if(c[j] > c[v]+cost[v][j]) c[j] = c[v]+cost[v][j]; } } printf("%d %d\n",d[e],c[e]); } int main() { while(scanf("%d%d",&n,&m) && n+m) { for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) { map[i][j] = i==j?0:INF; cost[i][j] = i==j?0:INF; } int a,b,c,d; for(int i = 1;i <= m;i++) { scanf("%d%d%d%d",&a,&b,&c,&d); if(map[a][b]>c) { map[a][b]=map[b][a]=c; cost[a][b]=cost[b][a]=d; } else if(map[a][b]==c) { if(cost[a][b]>d) cost[a][b]=cost[b][a]=d; } } scanf("%d%d",&start,&e); Dijkstra(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/immortal-worm/p/5185053.html