PAT A1111 2019.09.07 【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 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; lengthis 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

思路分析

最短路径+第二标尺:时间(边权)

最短时间+第二标尺:路口数(点权)

最后分别输出最短路径或最短时间

用两个dijkstra搞就完了,基本也是 模板吧

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#define MAX 600
#define INF 1000000000
using namespace std;
 
int n,m;
int s,d;
 
//公用 
bool vis[MAX]={false};
int pre[MAX];
 
//最短中找最快   dij+边权 
int adt[MAX][MAX];
int dis[MAX];
int tim[MAX][MAX];
int ttime[MAX];
vector<int>path1;
 
//最快中找最短   dij+点权 
//int tim[MAX][MAX]  公用 
int fast[MAX];
int num[MAX];//经过几站 
vector<int>path2;

void dijkstraDis(int s)
{
	dis[s]=0;
	ttime[s]=0;
	pre[s]=s;
 
	
	for(int i=0;i<n;i++)//注意这个大循环 
	{
		int u=-1,min=INF;
		for(int j=0;j<n;j++)
		{
			if(vis[j]==false&&dis[j]<min)//注意这里的条件 
			{
				u=j;
				min=dis[j];
			}
		}
		
		if(u==-1)return;
		
		vis[u]=true;
		for(int v=0;v<n;v++)
		{
			if(vis[v]==false&&adt[u][v]!=INF)//注意这里的条件 
			{
				
				if(dis[u]+adt[u][v]<dis[v])
				{
					dis[v]=dis[u]+adt[u][v];
					ttime[v]=ttime[u]+tim[u][v];
					pre[v]=u;
				}
				
				else if(dis[u]+adt[u][v]==dis[v])
				{
					if(ttime[u]+tim[u][v]<ttime[v])
					{
						ttime[v]=ttime[u]+tim[u][v];
						pre[v]=u;
					}
				}
				
			}
		}	
		
	}
 
	
}
 
void dijkstraTime(int s)
{
	fast[s]=0;
	num[s]=1;
	pre[s]=s;
 
	
	for(int i=0;i<n;i++)//注意这个大循环 
	{
		int u=-1,min=INF;
		for(int j=0;j<n;j++)
		{
			if(vis[j]==false&&fast[j]<min)//注意这里的条件 
			{
				u=j;
				min=fast[j];
			}
		}
		
		if(u==-1)return;
		
		vis[u]=true;
		for(int v=0;v<n;v++)
		{
			if(vis[v]==false&&tim[u][v]!=INF)//注意这里的条件 
			{
				
				if(fast[u]+tim[u][v]<fast[v])
				{
					fast[v]=fast[u]+tim[u][v];
					pre[v]=u;
					num[v]=num[u]+1;
				}
				
				else if(fast[u]+tim[u][v]==fast[v])
				{
					if(num[u]+1<num[v])
					{
						num[v]=num[u]+1;
						pre[v]=u; 
					} 
 
				}
				
			}
		}	
		
	}
 
	
}

void savePath1(int s,int v)
{
	if(s==v)
	{
		path1.push_back(v);
		return;
	}
	savePath1(s,pre[v]);
	path1.push_back(v);
}

void savePath2(int s,int v)
{
	if(s==v)
	{
		path2.push_back(v);
		return;
	}
	savePath2(s,pre[v]);
	path2.push_back(v);
}


 
int main()
{
	fill(adt[0],adt[0]+MAX*MAX,INF);//记录两站间距离 
	fill(tim[0],tim[0]+MAX*MAX,INF);//记录两站间时间 
	fill(dis,dis+MAX,INF);//起点到结点最短距离 
	fill(fast,fast+MAX,INF);//起点到结点最短时间 
	fill(ttime,ttime+MAX,INF);//计算最短距离中的第二标尺--时间 
	fill(num,num+MAX,INF);//计算最短时间中的第二标尺--路口数(结点数) 
 
	
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int a,b,ow,l,t;
		cin>>a>>b>>ow>>l>>t;
		if(ow==1)
		{
			adt[a][b]=l;
			tim[a][b]=t;
		}
		if(ow==0)
		{
			adt[a][b]=l;
			adt[b][a]=l;
			tim[a][b]=t;
			tim[b][a]=t;
		}
	}
	cin>>s>>d;
	
	dijkstraDis(s);
	savePath1(s,d);
	for(int i=0;i<MAX;i++) 
	{
		pre[i]=0;//公用 
		vis[i]=false;//公用 
	}
	dijkstraTime(s);
	savePath2(s,d);
	
	
	bool identical=true;
	if(path1.size()==path2.size())
	{
		for(int i=0;i<path1.size();i++)
		{
			if(path1[i]!=path2[i])identical=false;
		}
	} 
	else identical=false;
	
	if(identical==true)
	{
		cout<<"Distance = "<<dis[d]<<"; ";
		cout<<"Time = "<<fast[d]<<": ";
		cout<<path2[0];
		for(int i=1;i<path2.size();i++)
		  cout<<" -> "<<path2[i];
		cout<<endl;
	}
	else
	{
		cout<<"Distance = "<<dis[d]<<": ";
		cout<<path1[0];
		for(int i=1;i<path1.size();i++)
		  cout<<" -> "<<path1[i];
		cout<<endl;
		
		cout<<"Time = "<<fast[d]<<": ";
		cout<<path2[0];
		for(int i=1;i<path2.size();i++)
		  cout<<" -> "<<path2[i];
		cout<<endl;
	}
	
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值