#include <stdio.h>
#define MaxVNum 500
#define INF 10000
void dj(int l[][MaxVNum],int t[][MaxVNum],int path[], int S,int dist[],int n,int time[]);
void dj2(int t[][MaxVNum], int path[], int S, int time[], int n, int cnt[]);
int FindMinDist(int n, int collected[], int dist[]);
void PrintPath(int path[], int D,int S);
int main()
{
int t[MaxVNum][MaxVNum],l[MaxVNum][MaxVNum];
int n, m, oneway,i,v1,v2,S,D,dist1[MaxVNum],path1[MaxVNum],path2[MaxVNum],time1[MaxVNum],cnt[MaxVNum],time2[MaxVNum];
scanf("%d %d", &n, &m);
for (v1 = 0; v1 < n; v1++)
for (v2 = 0; v2 < n; v2++)
l[v1][v2] = t[v1][v2] = INF;
for (i = 0; i < m; i++) {
scanf("%d %d %d", &v1, &v2,&oneway);
scanf("%d %d", &l[v1][v2], &t[v1][v2]);
if (!oneway) {
l[v2][v1] = l[v1][v2];
t[v2][v1] = t[v1][v2];
}
}
scanf("%d %d", &S, &D);
dj(l,t,path1,S, dist1,n,time1);
dj2(t,path2, S, time2,n,cnt);
for (v2 = v1 = D;v1 == v2&&v1 != -1; v1 = path1[v1], v2 = path2[v2])
;
if (v1 == -1 && v2 == -1) {
printf("Distance = %d; Time = %d: ", dist1[D],time1[D]);
PrintPath(path1, D, S);
}
else {
printf("Distance = %d: ", dist1[D]);
PrintPath(path1, D, S);
printf("\n");
printf("Time = %d: ", time2[D]);
PrintPath(path2, D, S);
}
return 0;
}
void dj(int l[][MaxVNum], int t[][MaxVNum], int path[], int S, int dist[], int n,int time[])
{
int v,w;
int collected[MaxVNum];
for (v = 0; v < n; v++) {
collected[v] = 0;
dist[v] = l[S][v];
time[v] = t[S][v];
path[v] = -1;
}
dist[S] = 0;
time[S] = 0;
collected[S] = 1;
while (1) {
v = FindMinDist(n,collected, dist);
if (v == -1)
return;
collected[v] = 1;
for (w = 0; w < n; w++) {
if ((!collected[w]) && (l[v][w] < INF))
if (dist[v] + l[v][w] < dist[w]) {
dist[w] = dist[v] + l[v][w];
time[w] = time[v] + t[v][w];
path[w] = v;
}
else if (dist[v] + l[v][w] == dist[w] && time[v] + t[v][w] < time[w]) {
time[w] = time[v] + t[v][w];
path[w] = v;
}
}
}
}
int FindMinDist(int n, int collected[], int dist[])
{
int MinV, V;
int MinDist = INF;
for (V = 0; V<n; V++) {
if (!collected[V] && dist[V]<MinDist) {
MinDist = dist[V];
MinV = V;
}
}
if (MinDist < INF)
return MinV;
else return -1;
}
void PrintPath(int path[], int D,int S)
{
if (D != -1) {
PrintPath(path, path[D], S);
printf(" -> %d",D);
}
else
printf("%d", S);
}
void dj2(int t[][MaxVNum], int path[], int S, int time[], int n, int cnt[])
{
int v, w;
int collected[MaxVNum];
for (v = 0; v < n; v++) {
collected[v] = 0;
time[v] = t[S][v];
path[v] = -1;
cnt[v] = INF;
}
time[S] = 0;
cnt[S] = 0;
collected[S] = 1;
while (1) {
v = FindMinDist(n, collected, time);
if (v == -1)
return;
collected[v] = 1;
for (w = 0; w < n; w++) {
if ((!collected[w]) && (t[v][w] < INF))
if (time[v] + t[v][w] < time[w]) {
time[w] = time[v] + t[v][w];
cnt[w] = cnt[v] + 1;
path[w] = v;
}
else if (time[v] + t[v][w] == time[w] && cnt[w] > cnt[v] + 1) {
cnt[w] = cnt[v] + 1;
path[w] = v;
}
}
}
}
1111. Online Map
最新推荐文章于 2020-04-19 15:32:48 发布