A1111. Online Map (30)

本文介绍了一种算法,该算法能找出从起点到终点的最短路径与最快路径,并确保推荐给用户的路径是最优解。通过两次Dijkstra算法分别求取距离与时间上的最优解,并采用深度优先搜索确定具体的路径细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

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 fromV1 toV2, or 0 if not; length is the length of the street; andtime 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

参考代码

#include <cstdio>
#include <vector>
#include <algorithm>
#include <math.h>
#include <iostream>

using namespace std;

const int maxn = 1810;
const double INF = 1000000000;

int n,m,st,ed;

double G[maxn][maxn],cost[maxn][maxn],d[maxn],c[maxn];
bool vis[maxn] = {false};

vector<int> pre[maxn],pre1[maxn],tempObj,path,pathG,tempPath;

void Dijkstra(int st)
{
	fill(d,d+maxn,INF);
	fill(c,c+maxn,INF);
	d[st] = 0;
	c[st] = 0;
	for(int i = 0;i<n;i++)
	{
		pre[i].push_back(i);
	}
	for(int i=0;i<n;i++)
	{
		int u = -1;
		double Min = INF;
		for(int j = 0;j<n;j++)
		{
			if(vis[j] == false && d[j] < Min)
			{
				u = j;
				Min = d[j];
			}
		}
		if(u == -1)
			return;
		vis[u] = true;
		for(int v = 0;v<n;v++)
		{
			if(vis[v] == false && G[u][v] != INF)
			{
				if(d[u] + G[u][v] < d[v])
				{
					d[v] = d[u] + G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if(d[u] + G[u][v] == d[v])
				{
					pre[v].push_back(u);
				}
			}
		}
	}
}

void Dijsktra1(int st)
{
	fill(c,c+maxn,INF);
	fill(vis,vis+maxn,false);
	c[st] = 0;
	for(int i=0;i<n;i++)
	{
		pre1[i].push_back(i);
	}
	for(int i=0;i<n;i++)
	{
		int u = -1;
		double Min = INF;
		for(int j = 0;j<n;j++)
		{
			if(vis[j] == false && c[j] < Min)
			{
				u = j;
				Min = c[j];
			}
		}
		if(u == -1)
			return;
		vis[u] = true;
		for(int v=0;v<n;v++)
		{
			if(vis[v] == false && cost[u][v] != INF)
			{
				if(c[u] + cost[u][v] < c[v])
				{
					c[v] = c[u] + cost[u][v];
					pre1[v].clear();
					pre1[v].push_back(u);
				}
				else if(c[u] + cost[u][v] == c[v])
				{
					pre1[v].push_back(u);
				}
			}
		}
	}
}

int MIN = 1000000000;

void DFS(int index)
{
	if(index == st)
	{
		tempObj.push_back(index);

		if(tempObj.size() < MIN)
		{
			MIN = tempObj.size();
			path = tempObj;
		}
		tempObj.pop_back();
		return;
	}
	tempObj.push_back(index);
	for(int i=0;i<pre1[index].size();i++)
	{
		DFS(pre1[index][i]);
	}
	tempObj.pop_back();
}

double total_cost = 1000000000;

void DFSG(int index)
{
	if(index == st)
	{
		tempPath.push_back(index);
		double temp_cost = 0;
		for(int i=tempPath.size()-1;i>=1;i--)
		{
			int now_index = tempPath[i];
			int next_index = tempPath[i-1];
			temp_cost = temp_cost + cost[now_index][next_index];
		}
		if(temp_cost < total_cost)
		{
			pathG = tempPath;
			total_cost = temp_cost;
		}
		tempPath.pop_back();
		return;
	}
	tempPath.push_back(index);
	for(int i=0;i<pre[index].size();i++)
	{
		DFSG(pre[index][i]);
	}
	tempPath.pop_back();
}

int main()
{
	fill(G[0],G[0]+maxn*maxn,INF);
	fill(cost[0],cost[0]+maxn*maxn,INF);
	scanf("%d %d",&n,&m);
	for(int i=0;i<m;i++)
	{
		int id1,id2,k;
		double dis,time;
		scanf("%d%d%d%lf%lf",&id1,&id2,&k,&dis,&time);
		if(k == 1)
		{
			G[id1][id2] = dis;
			cost[id1][id2] = time;
		}
		else
		{
			G[id1][id2] = G[id2][id1] = dis;
			cost[id1][id2] = cost[id2][id1] = time;
		}
	}
	scanf("%d%d",&st,&ed);
	Dijkstra(st);
	Dijsktra1(st);
	DFSG(ed);
	DFS(ed);
	bool found = true;
	if(pathG.size() == path.size())
	{
		for(int i=0;i<path.size();i++)
		{
			if(pathG[i] != path[i])
			{
				found = false;
				break;
			}
		}
	}
	else
	{
		found = false;
	}
	if(st != ed)
	{
		if(found)
		{
			printf("Distance = %.0lf; Time = %.0lf: ",d[ed],c[ed]);
			for(int i =pathG.size()-1;i>=0;i--)
			{
				if(i == pathG.size()-1)
					printf("%d",pathG[i]);
				else
					printf(" -> %d",pathG[i]);
			}
			printf("\n");
		}
		else
		{
			printf("Distance = %.0lf: ",d[ed]);
			for(int i =pathG.size()-1;i>=0;i--)
			{
				if(i == pathG.size()-1)
					printf("%d",pathG[i]);
				else
					printf(" -> %d",pathG[i]);
			}
			printf("\n");
			printf("Time = %.0lf: ",c[ed]);
			for(int i=path.size()-1;i>=0;i--)
			{
				if(i == path.size() - 1)
					printf("%d",path[i]);
				else
					printf(" -> %d",path[i]);
			}
		}
	}
	else
	{
		printf("Distance = 0; Time = 0: %d -> %d\n",st,ed);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值