#include <cstdio>
#include<cstring>
#include <algorithm>
using namespace std;
const int N = 1005;
const int INF = 0xfffffff;
int G[N][N],C[N][N];
int d[N],c[N];
int vis[N];
int n,m;
void Dijkstra(int s){
fill(d,d+N,INF);
fill(c,c+N,INF);
memset(vis,0,sizeof(vis));
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] == 0&& d[j] < MIN){
u = j;
MIN = d[j];
}
}
if(u == -1) return; //s点无法到达剩余顶点
vis[u] = 1;
for(int v = 0; v <= n; v++){
if(vis[v] == 0 && G[u][v] != INF){
if(d[u] + G[u][v] < d[v]){
d[v] = d[u] + G[u][v];
c[v] = c[u] + C[u][v];
}else if(d[u] + G[u][v] == d[v]&&c[u] + C[u][v] < c[v]){
c[v] = c[u] + C[u][v];
}
}
}
}
}
int main(){
int a,b,w,p,st,ed;
while(scanf("%d%d",&n,&m) != EOF){
if(n==0&&m==0) break;
fill(G[0],G[0]+N*N,INF);
fill(C[0],C[0]+N*N,INF);
for(int i = 0; i < m; i++){
scanf("%d%d%d%d",&a,&b,&w,&p);
//下面这一步是核心,应该保证接收的图是最优的,再进一步做优化
if(w < G[a][b])
{
G[a][b] = G[b][a] = w;
C[a][b] = C[b][a] = p;
}
else if(w == G[a][b] && p < C[a][b]){
C[a][b] = C[b][a] = p;
}
}
scanf("%d%d",&st,&ed);
Dijkstra(st);
printf("%d %d\n",d[ed],c[ed]);
}
return 0;
}
最短路径问题【HDOJ3790】
最新推荐文章于 2022-01-30 17:20:11 发布