您好,欢迎来到年旅网。
搜索
您的当前位置:首页PAT (Advanced Level) Practise 1111. Online Map (30) Dijstra单源最短路

PAT (Advanced Level) Practise 1111. Online Map (30) Dijstra单源最短路

来源:年旅网

1111. Online Map (30)

时间
300 ms
内存
65536 kB
代码长度
16000 B
判题程序
Standard
作者
CHEN, Yue

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not;length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:
10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5
Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5
Sample Input 2:
7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5
Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5


#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int maxn = 500 + 10, INF = 0x3f3f3f3f;
int N, M, s, e;
int dis[maxn][maxn], tim[maxn][maxn], cnt[maxn][maxn];
int pd[maxn], pt[maxn], dd[maxn], dt[maxn];
bool vis[maxn];

//dijstra(dis, tim, dd, pd);
//dijstra(tim, cnt, dt, pt);
void dijstra(int map1[][maxn], int map2[][maxn], int *d1, int *p) {
	int d2[maxn];
	for (int i = 0; i < N; i++) {
		d1[i] = INF;
		d2[i] = INF;
	}
	memset(vis, false, sizeof(vis));
	for (int i = 0; i < N; i++) {
		d1[i] = map1[s][i];
		d2[i] = map2[s][i];
	}
	d1[s] = 0;
	p[s] = s;
	vis[s] = true;
	for (int t = 1; t <= N - 1; t++) {
		int MIN = INF, min;
		for (int i = 0; i < N; i++) {
			if (!vis[i]) {
				if (d1[i] < MIN) {
					min = i;
					MIN = d1[i];
				}
			}
		}
		vis[min] = true; //不要忘记标记
		for (int i = 0; i < N; i++) {
			if (!vis[i]) {
				if (d1[min] + map1[min][i] < d1[i]) {
					d1[i] = d1[min] + map1[min][i];
					d2[i] = d2[min] + map2[min][i];
					p[i] = min;
				}
				else if (d1[min] + map1[min][i] == d1[i] && d2[min] + map2[min][i] < d2[i]) {
					d2[i] = d2[min] + map2[min][i];
					p[i] = min;
				}
			}
		}
	}
}

void output(int *a) {
	int out[maxn], len = 0;
	int j = e;
	while (j != s) {
		out[len++] = j;
		j = a[j];
	}
	out[len++] = j;
	for (int i = len - 1; i >= 0; i--) {
		if (i != 0) {
			printf("%d -> ", out[i]);
		}
		else {
			printf("%d\n", out[i]);
		}
	}
}

int main()
{
	scanf("%d%d", &N, &M);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (i != j)
				dis[i][j] = tim[i][j] = INF;
			else
				dis[i][j] = tim[i][j] = 0;
		}
	}
	for (int t = 0; t < M; t++) {
		int v1, v2, one, len, time;
		scanf("%d%d%d%d%d", &v1, &v2, &one, &len, &time);
		dis[v1][v2] = len;
		tim[v1][v2] = time;
		if (!one) {
			dis[v2][v1] = len;
			tim[v2][v1] = time;
		}
	}
	scanf("%d%d", &s, &e);

	for (int i = 0; i < N; i++) {
		pd[i] = s;
	}
	dijstra(dis, tim, dd, pd);

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cnt[i][j] = 1;
		}
	}

	for (int i = 0; i < N; i++) {
		pt[i] = s;
	}
	dijstra(tim, cnt, dt, pt);
	bool flag = true;
	for (int i = 0; i < N; i++) {
		if (pd[i] != pt[i]) {
			flag = false;
			break;
		}
	}
	if (flag) {
		printf("Distance = %d; Time = %d: ", dd[e], dt[e]);
		output(pd);
	}
	else {
		printf("Distance = %d: ", dd[e]);
		output(pd);
		printf("Time = %d: ", dt[e]);
		output(pt);
	}
	return 0;
}




因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务