PAT (Advanced Level)-1111 Online Map(dijkstra+dfs)

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 Mlines 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

要求:

 给予 起点 和 终点 求出两条路 最短路径(不唯一 但要求输出时间最短的)最快路径(不唯一 但要求经过的点尽可能少)

搜索姿势:

1)裸dfs 暴力

2)繁琐:dijkstra先处理路径 再深搜剪枝得到 满足要求的情况  两遍:dijkstra+dfs 

dijkstra算法-单源最短路 不可解决负边权 时间复杂度普通O(n2)

dijkstra基本步骤 :

1源点s  最短路程的顶点集合p  未知最短路径的顶点集合q 可以用<bool>book[i]划分 

2源点s到自己本身 最短路径为0 dis[s]=0 有源点能直接到达的顶点i 则dis[i]=v[s][i] 源点无法到达的为inf 无限大

3在集合q的所有顶点中选择一个离源点s最近的顶点u 即dis[u]最小加入到集合p中,并考察以点u为起点的边,对每一条边进行松弛操作 dis[u]+v[u][w] 扩展一条从u到w的路径 若这个值比dis[w]值要小 更新该值

4重复第3步直到集合q为空 算法结束

针对于路径的处理 保存 能够到达顶点w的前驱顶点u (权值尽可能小的顶点)因此在dfs时 从一个顶点出发 向其前驱顶点探测 到开始位置 结束

#include <iostream>
#include <vector>
using namespace std;

const int inf(999999);
vector<vector<int> >vL(512,vector<int>(512,inf));
vector<vector<int> >vT(512,vector<int>(512,inf));
int n,m,s,e;
vector<int>shortpath;
int mintime=inf,resdis;
vector<int>fastpath;
int minexc=inf,restim;

void dijkstra(vector<vector<int> >&v,vector<vector<int> >&path)
{
	vector<int>book(n,0);
	vector<int>dis(v[s]);
	book[s]=1;
	for(int i=0;i<n;i++)
	{
		int u,min=inf;
		for(int j=0;j<n;j++)
		{
			if(!book[j]&&dis[j]<min)
			{
				min=dis[j];
				u=j;
			}
		}
		book[u]=1;
		for(int w=0;w<n;w++)
		{
			if(!book[w]&&dis[w]>dis[u]+v[u][w])
			{
				dis[w]=dis[u]+v[u][w];
				path[w].clear();
				path[w].push_back(u);
			}
			else if(!book[w]&&dis[w]==dis[u]+v[u][w])
				path[w].push_back(u);
		}
	}
}
void dfs_shortpath(vector<vector<int> >&lenp,vector<int>resp,int cur,int timesum,int dis)
{
	if(cur==s&&timesum<mintime)
	{
		mintime=timesum;
		shortpath=resp;
		resdis=dis;
		return;
	}
	for(auto pre:lenp[cur])
	{
		resp.push_back(pre);
		dfs_shortpath(lenp,resp,pre,timesum+vT[pre][cur],dis+vL[pre][cur]);
		resp.pop_back();
	}
}
void dfs_fastpath(vector<vector<int> >&timp,vector<int>resp,int cur,int timecnt,int exc)
{
	if(cur==s&&exc<minexc)
	{
		minexc=exc;
		fastpath=resp;
		restim=timecnt;
		return;
	}
	for(auto pre:timp[cur])
	{
		resp.push_back(pre);
		dfs_fastpath(timp,resp,pre,timecnt+vT[pre][cur],exc+1);
		resp.pop_back();
	}
}
int main()
{
	scanf("%d %d",&n,&m);
	for(int i=0;i<m;i++)
	{
		int u,w,oneway,length,tim;
		scanf("%d %d %d %d %d",&u,&w,&oneway,&length,&tim);
		vL[u][w]=length;
		vT[u][w]=tim;
		if(!oneway)
		{
			vL[w][u]=length;
			vT[w][u]=tim;
		}
	}
	scanf("%d %d",&s,&e);
	vector<vector<int> >lenp(n);
	vector<vector<int> >timp(n);
	for(int i=0;i<n;i++)
	{
		if(vL[s][i]<inf)lenp[i].push_back(s);
		if(vT[s][i]<inf)timp[i].push_back(s);
	}
	dijkstra(vL,lenp);
	dijkstra(vT,timp);
	vector<int>resp;
	dfs_shortpath(lenp, resp, e, 0, 0);
	dfs_fastpath(timp, resp, e, 0, 0);

	if(shortpath==fastpath)
	{
		printf("Distance = %d; Time = %d: ",resdis,restim);
		for(auto it=shortpath.rbegin();it!=shortpath.rend();it++)
			printf("%d -> ",*it);
		printf("%d\n",e);
		
	}
	else
	{
		printf("Distance = %d: ",resdis);
		for(auto it=shortpath.rbegin();it!=shortpath.rend();it++)
			printf("%d -> ",*it);
		printf("%d\n",e);
		printf("Time = %d: ",restim);
		for(auto it=fastpath.rbegin();it!=fastpath.rend();it++)
			printf("%d -> ",*it);
		printf("%d\n",e);
	}
	
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值