1111 Online Map (30 分) dijstra最短路+dfs回溯路径

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

题意:7个结点,9条边

从x到y,如果k==1则为单向边,随后是长度和花费时间。

输出最短路和最少花费时间

1如果最短路径有多条,输出最快的
2如果最快路径有多条,输出经过节点数最少的(不是路径最短的!!!!不然测试点2过不去呜呜呜)
3如果两条路径一样则输出样例2这样的 

思路:两次dijstra计算最短路和最少花费时间,然后两次dfs回溯路径,先比较再输出。

#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
int ma[505][505];
int cost[505][505];
int dis[505];
int sum[505];
int node[505];
int vis[505];
int pre1[505],pre2[505];
vector<int> path1,path2;
int n,m,s,e;
void dfs1(int x)
{
	if(x==-1)return;
	path1.push_back(x);
	x=pre1[x];
	dfs1(x);
}
void dfs2(int x)
{
	if(x==-1)return;
	path2.push_back(x);
	x=pre2[x];
	dfs2(x);
}
int main()
{
	int i,j,next;
	scanf("%d%d",&n,&m);
	memset(ma,INF,sizeof ma);
	memset(cost,INF,sizeof cost);
	memset(dis,INF,sizeof dis);
	memset(sum,INF,sizeof sum);
	memset(vis,0,sizeof vis);
	memset(pre1,-1,sizeof pre1);
	memset(pre2,-1,sizeof pre2);
	for(i=0;i<m;i++)
	{
		int x,y,k,len,ti;
		scanf("%d%d%d%d%d",&x,&y,&k,&len,&ti);
		ma[x][y]=len;
		cost[x][y]=ti;
		if(k==0)
		{
			ma[y][x]=len;
			cost[y][x]=ti;
		}
	}
	scanf("%d%d",&s,&e);
	//Distance
	dis[s]=0;
	for(i=0;i<n;i++)
	{
		int d=INF;
		for(j=0;j<n;j++)
		{
			if(vis[j]==0&&dis[j]<d)
			{
				d=dis[j];
				next=j;
			}
		}
		vis[next]=1;
		for(j=0;j<n;j++)
		{
			if(dis[j]>dis[next]+ma[next][j])
			{
				dis[j]=dis[next]+ma[next][j];
				sum[j]=sum[next]+cost[next][j];
				pre1[j]=next;
			}
			else if(dis[j]==dis[next]+ma[next][j])
			{
				if(sum[j]>sum[next]+cost[next][j])
				{
					sum[j]=sum[next]+cost[next][j];
					pre1[j]=next;
				}
			}
		}
	}
	//Time
	memset(sum,INF,sizeof sum);
	memset(node,INF,sizeof node);
	memset(vis,0,sizeof vis);
	sum[s]=0;
	node[s]=1;
	for(i=0;i<n;i++)
	{
		int d=INF;
		for(j=0;j<n;j++)
		{
			if(vis[j]==0&&sum[j]<d)
			{
				d=sum[j];
				next=j;
			}
		}
		vis[next]=1;
		for(j=0;j<n;j++)
		{
			if(sum[j]>sum[next]+cost[next][j])
			{
				sum[j]=sum[next]+cost[next][j];
				node[j]=node[next]+1;
				pre2[j]=next;
			}
			else if(sum[j]==sum[next]+cost[next][j])
			{
				if(node[j]>node[next]+1)
				{
					node[j]=node[next]+1;
					pre2[j]=next;
				}
			}
		}
	}
	dfs1(e);
	dfs2(e);
	if(path1.size()==path2.size())
	{
		int flag=0;
		for(i=0;i<path1.size();i++)
		{
			if(path1[i]!=path2[i])
			{
				flag=1;
				break;
			}
		}
		if(flag)
		{
			printf("Distance = %d: %d",dis[e],s);
			for(j=path1.size()-2;j>=0;j--)
			printf(" -> %d",path1[j]);
			printf("\n");
			printf("Time = %d: %d",sum[e],s);
			for(j=path2.size()-2;j>=0;j--)
			printf(" -> %d",path2[j]);
		}
		else //路径相同 
		{
			printf("Distance = %d; Time = %d: %d",dis[e],sum[e],s);
			for(j=path1.size()-2;j>=0;j--)
			printf(" -> %d",path1[j]);
		}
	}
	else
	{
		printf("Distance = %d: %d",dis[e],s);
		for(j=path1.size()-2;j>=0;j--)
		printf(" -> %d",path1[j]);
		printf("\n");
		printf("Time = %d: %d",sum[e],s);
		for(j=path2.size()-2;j>=0;j--)
		printf(" -> %d",path2[j]);
	}
	printf("\n");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值