PAT 甲级 1111 Online Map

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a845717607/article/details/87889899

1111 Online Map (30 point(s))

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

经验总结:

emmmm  这一题,实际上就是求两个点之间的最短距离路径以及最少耗时的路径,并且在最短路径不止一条时,选择耗时最少的那一条,在最少耗时路径不止一条时,选择经过顶点最少的那一条,于是乎,此题需要进行两次Dijkstra算法,并且利用DFS求出路径并进行判断输出,难度还好,就是。。。。挺多的,考试写这一题如果留的时间不够可能写不完- -|||,自己还需要注意在进行扩展更新数据时,相应数组的更新不能忘了!

AC代码

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=510;
const int INF=0x3fffffff;
int n,m,d[maxn],t[maxn],start,ter,pre1[maxn],pre2[maxn],pot[maxn];
bool flag[maxn];
struct node
{
	int time,length,v;
	node(int a,int b,int c):v(a),length(b),time(c){}
};
vector<int> shortest,leasttime,temp;
vector<node> adj[maxn];
void Dijkstra1(int s)
{
	memset(flag,false,sizeof(flag));
	fill(d,d+maxn,INF);
	fill(t,t+maxn,INF);
	d[s]=0;
	t[s]=0;
	for(int i=0;i<n;++i)
	{
		int min=INF,u=-1;
		for(int j=0;j<n;++j)
		{
			if(flag[j]==false&&d[j]<min)
			{
				min=d[j];
				u=j;
			}
		}
		if(u==-1)
			return ;
		flag[u]=true;
		for(int j=0;j<adj[u].size();++j)
		{
			int v=adj[u][j].v;
			if(flag[v]==false)
			{
				int l=adj[u][j].length;
				int tim=adj[u][j].time;
				if(d[u]+l<d[v])
				{
					d[v]=d[u]+l;
					pre1[v]=u;
					t[v]=t[u]+tim;
				}
				else if(d[u]+l==d[v]&&t[u]+tim<t[v])
				{
					t[v]=t[u]+tim;
					pre1[v]=u;
				}
			}
		}
	}
}
void Dijkstra2(int s)
{
	memset(flag,false,sizeof(flag));
	fill(pot,pot+maxn,INF);
	fill(t,t+maxn,INF);
	t[s]=0;
	pot[s]=1;
	for(int i=0;i<n;++i)
	{
		int min=INF,u=-1;
		for(int j=0;j<n;++j)
		{
			if(flag[j]==false&&t[j]<min)
			{
				min=t[j];
				u=j;
			}
		}
		if(u==-1)
			return ;
		flag[u]=true;
		for(int j=0;j<adj[u].size();++j)
		{
			int v=adj[u][j].v;
			if(flag[v]==false)
			{
				int tim=adj[u][j].time;
				if(t[u]+tim<t[v])
				{
					t[v]=t[u]+tim;
					pot[v]=pot[u]+1;
					pre2[v]=u;
				}
				else if(t[u]+tim==t[v]&&pot[u]+1<pot[v])
				{
					pot[v]=pot[u]+1;
					pre2[v]=u;
				}
			}
		}
	}
}
void DFS(int d,int f)
{
	temp.push_back(d);
	if(d==start)
	{
		if(f==1)
			shortest=temp;
		else
			leasttime=temp;
		return ;
	}
	if(f==1)
		DFS(pre1[d],f);
	else
		DFS(pre2[d],f);
}
int main()
{
	int v1,v2,f,len,tim;
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;++i)
	{
		scanf("%d%d%d%d%d",&v1,&v2,&f,&len,&tim);
		if(f==0)
			adj[v2].push_back(node(v1,len,tim));
		adj[v1].push_back(node(v2,len,tim));
	}
	scanf("%d%d",&start,&ter);
	Dijkstra1(start);
	Dijkstra2(start);
	DFS(ter,1);
	temp.clear();
	DFS(ter,2);
	if(shortest==leasttime)
	{
		printf("Distance = %d; Time = %d: ",d[ter],t[ter]);
		for(int i=shortest.size()-1;i>=0;--i)
			printf("%d%s",shortest[i],i>0?" -> ":"\n");
	}
	else
	{
		printf("Distance = %d: ",d[ter]);
		for(int i=shortest.size()-1;i>=0;--i)
			printf("%d%s",shortest[i],i>0?" -> ":"\n");
		printf("Time = %d: ",t[ter]);
		for(int i=leasttime.size()-1;i>=0;--i)
			printf("%d%s",leasttime[i],i>0?" -> ":"\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/a845717607/article/details/87889899