PAT甲级1111

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
#include
#include
using namespace std;
const int maxn = 510;
const int INF = 1000000000;
int N, M, e, s;
bool vis[maxn] = { false };
int G[maxn][maxn],Gt[maxn][maxn];
int d[maxn];//边权
int w[maxn];//点权
int t[maxn];//边权
int preMIND[maxn],preMINT[maxn];
void DijkstraD(int s)
{
	fill(d, d + maxn, INF);
	fill(t, t + maxn, INF);
	fill(vis, vis + maxn, false);
	fill(preMIND, preMIND + maxn, -1);
	d[s] = 0;
	t[s] = 0;
	for (int i = 0; i < N; i++)
	{
		int MIN = INF, id=-1;
		for (int j = 0; j < N; j++)
		{
			if (!vis[j]&&MIN>d[j])
			{
				MIN = d[j];
				id = j;
			}
		}//找最小
		if (id == -1)return;
		vis[id] = true;
		for (int j = 0; j < N; j++)
		{
			if (!vis[j] && G[id][j] != INF)
			{
				if (d[id] + G[id][j] < d[j])
				{
					d[j] = d[id] + G[id][j];
					t[j] = t[id] + Gt[id][j];//注意这个也要顺便更新
					preMIND[j] = id;
				}
				else if (d[j] == d[id] + G[id][j])
				{
					if (t[j] > t[id] + Gt[id][j])
					{
						t[j] = t[id] + Gt[id][j];
						preMIND[j] = id;
					}
				}
			}
		}
	}
}
void DijkstraT(int s)
{
	fill(t, t + maxn, INF);
	fill(w, w + maxn, 1);
	fill(vis, vis + maxn, false);
	fill(preMINT, preMINT + maxn, -1);
	w[s] = 1;
	t[s] = 0;
	for (int i = 0; i < N; i++)
	{
		int MIN = INF, id = -1;
		for (int j = 0; j < N; j++)
		{
			if (!vis[j] && MIN>t[j])
			{
				MIN = t[j];
				id = j;
			}
		}//找最小
		if (id == -1)return;
		vis[id] = true;
		for (int j = 0; j < N; j++)
		{
			if (!vis[j] && Gt[id][j] != INF)
			{
				if (t[id] + Gt[id][j] < t[j])
				{
					t[j] = t[id] + Gt[id][j];
					w[j] = w[id] + 1;//这样的尤其要注意,不是只在比较第二标尺时才更新
					preMINT[j] = id;
				}
				else if (t[j] == t[id] + Gt[id][j])
				{
					if (w[j] > w[id] + 1)
					{
						w[j] = w[id] +1;
						preMINT[j] = id;
					}
				}
			}
		}
	}
}
void printPath(int *pre,int e1)
{
	if (e1 == -1)return;
	printPath(pre, pre[e1]);
	printf("%d", e1);
	if (e1 != e)
		printf(" -> ");
}
int main()
{
	scanf("%d %d", &N, &M);
	fill(G[0], G[0] + maxn*maxn, INF);//记住一定要初始化
	fill(Gt[0], Gt[0] + maxn*maxn, INF);//记住一定要初始化
	int V1, V2, oneway, length, time;
	for (int i = 0; i < M; i++)
	{
		scanf("%d%d%d%d%d", &V1, &V2, &oneway, &length, &time);
		G[V1][V2] = length;
		Gt[V1][V2] = time;
		if (oneway==0)
		{
			G[V2][V1] = length;
			Gt[V2][V1] = time;
		}
	}
	scanf("%d %d", &s, &e);
	DijkstraD(s);
	DijkstraT(s);
	bool flag = false;
	int e2 = e;
	while (preMIND[e2] != -1 && preMINT[e2] != -1)
	{
		if (preMIND[e2] == preMINT[e2])
		{
			e2 = preMIND[e2];
		}
		else break;
	}
	if (!(preMIND[e2] == -1 && preMINT[e2] == -1))
		flag = true;
	printf("Distance = %d", d[e]);
	if (flag)
	{
		printf(": ");
		printPath(preMIND, e);
		printf("\n");
	}
	else printf("; ");
	printf("Time = %d: ", t[e]);
	printPath(preMINT, e);
	return 0;
}

你可能感兴趣的:(PAT)