PAT(图)——1111 Online Map (30 分)

1111 Online Map (30 分)

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

题目大意:

给出点到点的距离,时间,求两点之间,一条最短路径和一条用时最少路径。当两条路径距离相同,选择用时最少的路径;当两条路径用时相同,选择中间节点最少的路径。

题目解析:

迪杰斯特拉算法,类似1003,1030,1087,注意输出格式。

具体代码:

#include<iostream>
#include<algorithm>
#include<map>
#include<climits>
using namespace std;
#define MAXN 510
map<string,int> m;
int e[MAXN][MAXN];
int Time[MAXN][MAXN];
int dis[MAXN];//原点到i点的距离
int t[MAXN];//原点到i点的时间
int pre[MAXN];//i点的前驱结点
int pre0[MAXN];
int num[MAXN];
bool vis[MAXN];
int tmpdis;
void dfs(int v,int start){
	if(v==start){
		printf("%d",v);
		return;
	}
	dfs(pre[v],start);
	printf(" -> %d",v);
}
void dfs0(int v,int start){
	if(v==start){
		printf("%d",v);
		return;
	}
	dfs0(pre0[v],start);
	printf(" -> %d",v);
}
int main() {
	fill(e[0],e[0]+MAXN*MAXN,INT_MAX);
	fill(Time[0],Time[0]+MAXN*MAXN,INT_MAX);
	fill(dis,dis+MAXN,INT_MAX);
	fill(t,t+MAXN,INT_MAX);
	fill(vis,vis+MAXN,false);
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		int v1,v2,flag,len,ti;
		scanf("%d%d%d%d%d",&v1,&v2,&flag,&len,&ti);
		if(flag==1){
			e[v1][v2]=len;
			Time[v1][v2]=ti;
		}else{
			e[v1][v2]=e[v2][v1]=len;
			Time[v1][v2]=Time[v2][v1]=ti;
		}
	}
	int start,end;
	scanf("%d%d",&start,&end);
	dis[start]=0;
	t[start]=0;
	pre[start]=start;
	for(int i=0;i<n;i++){
		int k=-1,min=INT_MAX;
		for(int j=0;j<n;j++){
			if(vis[j]==false&&dis[j]<min){
				k=j;
				min=dis[j];
			}
		}
		if(k==-1) break;
		vis[k]=true;
		for(int j=0;j<n;j++){
			if(vis[j]==false&&e[k][j]<INT_MAX){
				if(dis[j]>dis[k]+e[k][j]||(dis[j]==dis[k]+e[k][j]&&t[j]>t[k]+Time[k][j])){
					dis[j]=dis[k]+e[k][j];
					t[j]=t[k]+Time[k][j];
					pre[j]=k;
				}
			}
		}
	}
	tmpdis=dis[end];
	//求最短时间 
	fill(dis,dis+MAXN,INT_MAX);
	fill(t,t+MAXN,INT_MAX);
	fill(vis,vis+MAXN,false);
	num[start]=1;
	t[start]=0;
	pre0[start]=start;
	for(int i=0;i<n;i++){
		int k=-1,min=INT_MAX;
		for(int j=0;j<n;j++){
			if(vis[j]==false&&t[j]<min){
				k=j;
				min=t[j];
			}
		}
		if(k==-1) break;
		vis[k]=true;
		for(int j=0;j<n;j++){
			if(vis[j]==false&&Time[k][j]<INT_MAX){
				if(t[j]>t[k]+Time[k][j]||(t[j]==t[k]+Time[k][j]&&num[j]>num[k]+1)){
					num[j]=num[k]+1;
					t[j]=t[k]+Time[k][j];
					pre0[j]=k;
				}
			}
		}
	}
	//比较两条路径是否重合
	int flag=1,v=end;
	while(pre[v]==pre0[v]){
		if(v==start) break;
		v=pre[v];
	}
	if(pre[v]==start&&pre0[v]==start)
		flag=0;
	printf("Distance = %d",tmpdis);
	if(flag){
		printf(": ");
		dfs(end,start);
		printf("\n");
	}else{
		printf("; ");
	}
	printf("Time = %d: ",t[end]);
	dfs0(end,start);
	return 0;
}
全部评论

相关推荐

中电45所 测试开发岗 可以解决北京户口,提供员工宿舍,早 8 晚 5(不过一般会加班到7-8点,周六一般也会去,面试官说的) 硕士
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务