PAT甲级A1111 Online Map (30 分)

22 篇文章 0 订阅
18 篇文章 0 订阅

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

题意:给出一张路径图,共n个结点,m条路,结点编号0-n-1,让你求出 一条最短的路径,在求出一条最快的路,并按格式输出。如果最短的路不唯一,则输出其中最快的那条,如果最快的那条路不唯一,则输出其中经过结点最少的那条。

思路:由于每条路有两个参数,一个距离,一个时间,因此使用邻接表结构,然后两次使用dijkstra()算法,一次以距离为第一标尺,时间为第二标尺;一次以时间为第一标尺,经过的结点数为第二标尺即可。最后通过DFS函数输出路径。注意如果两条路经相同,则只输出第二条路经。

参考代码:

#include<cstdio>
#include<vector>
using namespace std;
const int maxn=510;
const int inf=(1<<30);
int n,m,st,ed,D[maxn],T[maxn],num_p[maxn],vis[maxn];	//D存放距离,T存放时间,num_p存放经过的结点数
struct node{
	int v,length,time;
	node(int a,int b,int c):v(a),length(b),time(c){}
};
vector<node> adj[maxn];
int pre1[maxn],pre2[maxn];
void dijkstra_D(int st){
	fill(D,D+n,inf);		//初始化
	fill(T,T+n,inf);		//初始化
	D[st]=0;
	for(int i=0;i<n;i++) pre1[i]=i;	//初始化
	for(int i=0;i<n;i++){		
		int u=-1,min=inf;
		for(int j=0;j<n;j++){
			if(!vis[j]&&D[j]<min){
				u=j;
				min=D[j];
			}
		}
		if(u==-1) return ;
		vis[u]=1;
		for(int j=0;j<adj[u].size();j++){
			int v=adj[u][j].v,d=adj[u][j].length,t=adj[u][j].time;
			if(!vis[v]&&D[u]+d<D[v]){
				D[v]=D[u]+d;
				T[v]=T[u]+t;
				pre1[v]=u;
			}else if(!vis[v]&&D[u]+d==D[v]&&T[u]+t<T[v]){
				T[v]=T[u]+t;
				pre1[v]=u;
			}
		}
	}
}
void dijkstra_T(int st){
	fill(T,T+n,inf);
	fill(vis,vis+n,0);
	T[st]=0;
	for(int i=0;i<n;i++) pre2[i]=i;
	for(int i=0;i<n;i++){
		int u=-1,min=inf;
		for(int j=0;j<n;j++){
			if(!vis[j]&&T[j]<min){
				u=j;
				min=T[j];
			}
		}
		if(u==-1) return ;
		vis[u]=1;
		for(int j=0;j<adj[u].size();j++){
			int v=adj[u][j].v,t=adj[u][j].time;
			if(!vis[v]&&T[u]+t<T[v]){
				T[v]=T[u]+t;
				pre2[v]=u;
				num_p[v]=num_p[u]+1;
			}else if(!vis[v]&&T[u]+t==T[v]&&num_p[u]+1<num_p[v]){
				num_p[v]=num_p[u]+1;
				pre2[v]=u;
			}
		}
	}
}
void DFS(int u,int tag){		//输出函数
	if(u==st){
		printf("%d",st);
		return;
	}
	if(tag==1)
		DFS(pre1[u],tag);
	else 
		DFS(pre2[u],tag);
	printf(" -> %d",u);
}
int main()
{
	scanf("%d%d",&n,&m);
	int u,v,tag,length,time;
	for(int i=0;i<m;i++){
		scanf("%d%d%d%d%d",&u,&v,&tag,&length,&time);
		adj[u].push_back(node(v,length,time));
		if(!tag)		//tag==0表示为双向路径
			adj[v].push_back(node(u,length,time));
	}
	scanf("%d%d",&st,&ed);
	dijkstra_D(st);
	dijkstra_T(st);
	bool flag=true;
	for(int i=0;i<n&&flag;i++){
		if(pre1[i]!=pre2[i])
			flag=false;
	}
	if(flag){
		printf("Distance = %d; Time = %d: ",D[ed],T[ed]);
		DFS(ed,1);
	}else{
		printf("Distance = %d: ",D[ed]);
		DFS(ed,1);
		printf("\nTime = %d: ",T[ed]);
		DFS(ed,2);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值