【PAT】【Advanced Level】1111. Online Map (30)

9 篇文章 0 订阅

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
原题链接:

https://www.patest.cn/contests/pat-a-practise/1111

思路:

DFS 超时

DJ+DFS AC

对于针对时间的最短路,用多维数组存储多个可能的前驱,然后用DFS搜索最少节点的路径

CODE:

DFS:

#include<iostream>
#include<vector>
#include<cstring>
#define N 510

using namespace std;

typedef struct S
{
	int le;
	int ti;
};

vector<int> pa[N];

S dis[N][N];

bool flag[N];

int ori;
int des;
int minti;
int mintile;
int minle;
int minleti;
vector<int> re1;
vector<int> re2;

vector<int> re;


void dfs(int n,int lc,int tc)
{

	if (lc>minle && tc>minti) return ;
	if (n==des)
	{
		if (lc<minle || (lc==minle && tc<minleti))
		{
			minle=lc;
			minleti=tc;
			re1.clear();
			for (int i=0;i<re.size();i++)
			{
				re1.push_back(re[i]);
			}
		}
		if (tc<minti || (tc==minti && re.size()<re2.size()))
		{
			minti=tc;
			mintile=lc;
			re2.clear();
			for (int i=0;i<re.size();i++)
			{
				re2.push_back(re[i]);
			}
		}	
	}
	for (int i=0;i<pa[n].size();i++)
	{
		if (flag[pa[n][i]]==1) continue;
		flag[pa[n][i]]=1;
		re.push_back(pa[n][i]);
		dfs(pa[n][i],lc+dis[n][pa[n][i]].le,tc+dis[n][pa[n][i]].ti);
		flag[pa[n][i]]=0;
		re.erase(re.end()-1);
	}
	return ;
}

int judge(int n)
{
	for (int i=0;i<n;i++)
	if (re1[i]!=re2[i]) return -1;
	
	return 1;
}


int main()
{
	int n,m;
	cin>>n>>m;
	for (int i=0;i<m;i++)
	{
		int st,ds,ow;
		S te;
		cin>>st>>ds>>ow>>te.le>>te.ti;
		pa[st].push_back(ds);
		dis[st][ds]=te;
		if (ow==0)
		{
			pa[ds].push_back(st);
			dis[ds][st]=te;
		}
	}
	
	cin>>ori>>des;
	memset(flag,0,sizeof(flag));
	minti=10000000;
	mintile=10000000;
	minle=10000000;
	minleti=10000000;
	flag[ori]=1;
	re.push_back(ori);
	dfs(ori,0,0);
	int pd;
	if (re1.size()==re2.size())
	{
		pd=judge(re1.size());
	}
	if (pd==1)
	{
		cout<<"Distance = "<<minle<<"; Time = "<<minti<<": ";
		for (int i=0;i<re1.size();i++)
		{
			if (i!=0) cout<<" -> ";
			cout<<re1[i];
		}
	}
	else
	{
		cout<<"Distance = "<<minle<<": ";
		for (int i=0;i<re1.size();i++)
		{
			if (i!=0) cout<<" -> ";
			cout<<re1[i];
		}
		cout<<endl;
		cout<<"Time = "<<minti<<": ";
		for (int i=0;i<re2.size();i++)
		{
			if (i!=0) cout<<" -> ";
			cout<<re2[i];
		}
	}
	return 0;
}


DJ+DFS:

#include<iostream>
#include<cstdio>
#include<vector>
#define N 510
using namespace std;
int from,to;
int m,n;
int minsize;
int le[N][N], ti[N][N];
int distl[N], distt[N];
int pathl[N];
vector<int> patht[N], re1, re2, _re2;
using namespace std;
void dfsl(int n)
{
	re1.push_back(n);
	if (n==from)	return;
	dfsl(pathl[n]);
	return;
}
void dfst(int n)
{
	_re2.push_back(n);
	if (n==from)
	{
		if (_re2.size()<minsize)
		{
			re2.clear();
			minsize=_re2.size();
			re2=_re2;
		}
	}
	for (int i=0;i<patht[n].size();i++)
	{
		dfst(patht[n][i]);
	}
	_re2.pop_back();
}
void dj1()
{
	bool flag[N];
	int _ti[N];
	fill(_ti,_ti+N,10000000);
	_ti[from]=0;
	distl[from]=0;
	fill(flag,flag+N,false);
	for (int i=0;i<n;i++)	pathl[i]=i;
	for (int i=0;i<n;i++)
	{
		int mi=10000000;
		int fi=-1;
		for (int j=0;j<n;j++)
		{
			if (flag[j]==1) continue;
			if (distl[j]<mi)
			{
				mi=distl[j];
				fi=j;
			}
		}
		if (fi==-1)	break;
		flag[fi]=1;
		for (int j=0;j<n;j++)
		{
			if (flag[j]==1 || le[fi][j]==-1) continue;
			if (distl[j]>distl[fi]+le[fi][j])
			{
				pathl[j]=fi;
				distl[j]=distl[fi]+le[fi][j];
				_ti[j]=_ti[fi]+ti[fi][j];
				
			}
			else if (distl[j]==distl[fi]+le[fi][j])
			{
				if (_ti[j]>_ti[fi]+ti[fi][j])
				{
					pathl[j]=fi;
					distl[j]=distl[fi]+le[fi][j];
					_ti[j]=_ti[fi]+ti[fi][j];
				}	
			}
		}
	}
}

void dj2()
{
	bool flag[N];
	distt[from]=0;
	fill(flag,flag+N,false);
	for (int i=0;i<n;i++)
	{
		int mi=10000000;
		int fi=-1;
		for (int j=0;j<n;j++)
		{
			if (flag[j]==1) continue;
			if (distt[j]<mi)
			{
				mi=distt[j];
				fi=j;
			}
		}
		if (fi==-1)
			break;
		flag[fi]=1;
		for (int j=0;j<n;j++)
		{
			if (flag[j]==1 || ti[fi][j]==-1) continue;
			if (distt[j]>distt[fi]+ti[fi][j])
			{
				patht[j].clear();
				patht[j].push_back(fi);
				distt[j]=distt[fi]+ti[fi][j];
			}
			else if (distt[j]==distt[fi]+ti[fi][j])
			{
				patht[j].push_back(fi);
			}
		}
	}
}

int main()
{
	fill(le[0],le[0]+N*N,-1);
	fill(ti[0],ti[0]+N*N,-1);
	fill(distl,distl+N,10000000);
	fill(distt,distt+N,10000000);
	cin>>n>>m;
	for (int i=0;i<m;i++)
	{
		int fr,to,ow,l,t;
		cin>>fr>>to>>ow>>l>>t;
		le[fr][to]=l;
		ti[fr][to]=t;
		if (ow!=1)
		{
			le[to][fr]=l;
			ti[to][fr]=t;
		}
	}	
	cin>>from>>to;
	minsize=10000000;
	dj1();
	dj2();
	dfsl(to);
	dfst(to);
	if (re1==re2)
	{
		cout<<"Distance = "<<distl[to]<<"; Time = "<<distt[to]<<": ";
		for (int i=re1.size()-1;i>=0;i--)
		{
			cout<<re1[i];
			if (i!=0) cout<<" -> ";
		}
	}
	else
	{
		cout<<"Distance = "<<distl[to]<<": ";
		for (int i=re1.size()-1;i>=0;i--)
		{
			cout<<re1[i];
			if (i!=0) cout<<" -> ";
		}
		cout<<endl;
		cout<<"Time = "<<distt[to]<<": ";
		for (int i=re2.size()-1;i>=0;i--)
		{
			cout<<re2[i];
			if (i!=0) cout<<" -> ";
		}
	}
	return 0;
}









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值