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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include<stdio.h> #include<stdlib.h>
#define MAXVEX 505 #define INFINITY 65535
void CreateGraph( ); void Dijkstra( int v);
int G[MAXVEX][MAXVEX][2],Nv,Ne; int know[MAXVEX]; int distance[MAXVEX]; int pay[MAXVEX];
int main() { int s,d; scanf("%d %d %d %d",&Nv,&Ne,&s,&d); CreateGraph(); Dijkstra(s); if( distance[d]<INFINITY ){ printf("%d %d",distance[d],pay[d]); } return 0; }
void CreateGraph() { int i,j; int v1,v2; int dn,f;
for( i=0; i<Nv; i++) { for( j=0; j<Nv; j++) { G[i][j][0] = INFINITY; G[i][j][1] = INFINITY; } }
for( i=0; i<Ne; i++) { scanf("%d %d %d %d",&v1,&v2,&dn,&f); G[v1][v2][0] = G[v2][v1][0]=dn; G[v1][v2][1] = G[v2][v1][1]=f; } }
void Dijkstra( int v) { int i,j,k; int min,cost;
for( i=0; i<Nv; i++) { know[i] =0; distance[i] =G[v][i][0]; pay[i] =G[v][i][1]; }
know[v] = 1; distance[v] =0; pay[v] = 0;
for( i=1; i<Nv; i++) { min = INFINITY; for( j=0; j<Nv; j++) { if( !know[j] && distance[j]<min) { k = j; min = distance[j]; cost = pay[j]; } }
know[k] = 1; for( j=0; j<Nv; j++) { if( !know[j] && (min+G[k][j][0]<distance[j])) { distance[j] = min+G[k][j][0]; pay[j] = cost + G[k][j][1];
} else if( !know[j] && (min+G[k][j][0]==distance[j]) && (cost+G[k][j][1] < pay[j])) {
pay[j] = cost + G[k][j][1]; } }
}
}
|