1111. Online Map (30)

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

这题用2个dijkstra,存储2条路径,比较即可


#include<stdio.h>
#include<string.h>
#include<limits.h> 
#define inf INT_MAX
int n,m,start,end;
int mark[510];
int path[510];
int dis[510];
int time[510];
int dismap[510][510];
int timemap[510][510];
int citynum[510];
int s1[510],s2[510],s[510];
int cou1,cou2,cou;
void init(){
	int i,j;
	for(i=0;i<n;i++){
		mark[i]=0;
		path[i]=-1;
		dis[i]=inf;
		time[i]=inf;
		for(j=0;j<n;j++){
			dismap[i][j]=inf;
			timemap[i][j]=inf;
		}
		dismap[i][i]=0;
		timemap[i][i]=0;
	}
}
void init2(){
	int i,j;
	for(i=0;i<n;i++){
		mark[i]=0;
		path[i]=-1;
		time[i]=inf;
		dis[i]=inf;
		citynum[i]=inf; 
	}
}
void dijkstra(int s){//路径最短,相同则时间最少 
	mark[s]=1;
	path[s]=-2;
	dis[s]=0;
	time[s]=0;
	int i,newp=s;
	while(1){
		for(i=0;i<n;i++){
			if(mark[i]==0&&dismap[newp][i]<inf){
				if(dis[i]>dis[newp]+dismap[newp][i]){
					dis[i]=dis[newp]+dismap[newp][i];
					path[i]=newp;
					time[i]=time[newp]+timemap[newp][i];
				}
				else if(dis[i]==dis[newp]+dismap[newp][i]){
					if(time[i]>time[newp]+timemap[newp][i]){
						time[i]=time[newp]+timemap[newp][i];
						path[i]=newp;
					}
				}
			}
		}
		int min=inf;
		for(i=0;i<n;i++){
			if(mark[i]==0&&dis[i]<inf){
				if(dis[i]<min){
					min=dis[i];
					newp=i;
				}
			} 
		}
		mark[newp]=1;
		if(min==inf||newp==end){
			break;
		}
	}
}
void dijkstra2(int s){//时间最少,相同则经过城市数最少 
	mark[s]=1;
	path[s]=-2;
	citynum[s]=1;
	dis[s]=0;
	time[s]=0;
	int i,newp=s;
	while(1){
		for(i=0;i<n;i++){
			if(mark[i]==0&&timemap[newp][i]<inf){
				if(time[i]>time[newp]+timemap[newp][i]){
					time[i]=time[newp]+timemap[newp][i];
					path[i]=newp;
					citynum[i]=citynum[newp]+1;
					dis[i]=dis[newp]+dismap[newp][i];
				}
				else if(time[i]==time[newp]+timemap[newp][i]){
					if(citynum[i]>citynum[newp]+1){
						citynum[i]=citynum[newp]+1;
						path[i]=newp;
						dis[i]=dis[newp]+dismap[newp][i];
					}
				}
			}
		}
		int min=inf;
		for(i=0;i<n;i++){
			if(mark[i]==0&&time[i]<inf){
				if(time[i]<min){
					min=time[i];
					newp=i;
				}
			} 
		}
		mark[newp]=1;
		if(min==inf||newp==end){
			break;
		}
	}
}
void store(int e){ 
	if(path[e]==-2){
		s[cou++]=e;
		return;
	}
	store(path[e]);
	s[cou++]=e; 
}
void print(int s[],int length){//打印路径 
	int i;
	for(i=0;i<length;i++){
		if(i==0){
			printf("%d",s[i]);
		}
		else{
			printf(" -> %d",s[i]);
		}
	}
}
void copy(int a[],int length){
	int i;
	for(i=0;i<length;i++){
		a[i]=s[i];
	}
}
int cmp(int a[],int b[]){
	if(cou1!=cou2){
		return 0;
	}
	int i;
	for(i=0;i<cou1;i++){
		if(a[i]!=b[i]){
			return 0;
		} 
	}
	return 1;
}
int main(){
	int i,j,v1,v2,flag,length,time1;
	scanf("%d %d",&n,&m);
	init();
	for(i=0;i<m;i++){
		scanf("%d %d %d %d %d",&v1,&v2,&flag,&length,&time1);
		if(flag==0){
			dismap[v1][v2]=dismap[v2][v1]=length;
			timemap[v1][v2]=timemap[v2][v1]=time1;
		}
		else{
			dismap[v1][v2]=length;
			timemap[v1][v2]=time1;
		}
	}
	scanf("%d%d",&start,&end);
	dijkstra(start);
	int distance=dis[end];
	cou=0;
	store(end);
	cou1=cou;
	copy(s1,cou1); 
	init2();
	dijkstra2(start);
	int TIME=time[end];
	cou=0;
	store(end);
	cou2=cou;
	copy(s2,cou2);
	if(cmp(s1,s2)==1){
		printf("Distance = %d; Time = %d: ",distance,TIME); 
		print(s1,cou1);
	}
	else{
		printf("Distance = %d: ",distance);
		print(s1,cou1);
		printf("\n");
		printf("Time = %d: ",TIME);
		print(s2,cou2);
	}
} 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值