L3 ladder map

L3 ladder map

Title of TIANTI map
When I saw this question, the first thought was that it must be search. As for whether it is deep search or wide search, I chose deep search. In fact, I'm afraid of such topics as search, because there are a few topics written recently, but I still understand the principle of facing difficulties, so I started to knock on the code, and after a period of time, I finally wrote the deep search code of this topic, so I submitted it with confidence, but there was a problem, part of the examples Timeout, it doesn't matter. It must be that the complexity of deep search time is too high. Do I change my mind to guangsearch? After a period of time, the code of guangsearch has been written by me. Now it must be OK. So I submitted the code of guangsearch again with confidence. What, or timeout. This result made me despair. Now I finally realized that my thinking was wrong. Ah, I was defeated by difficulties finally. I manually Baidu for a while, and then crtl C, Ctrl V, ah, it's amazing. The process of the author's one question is really too hard. I can't help feeling it.

Enter the main topic, use dijkstra twice for this topic, but it needs to be changed a little. For example, in the case of seeking the fastest and shortest, the slow need to be updated to the fast one. At the same time, whether the path length needs to be updated should be considered for the same fast one. Here is the code of this topic

#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int n,m,way,s,e;
int tmp[510];
int l[510][510],t[510][510];//Distance between two points, time 
vector<int>g[550];//Adjacency relation 
int dis[510];//Distance to source array 
int _ime[510];//Time array to source 
const int inf=0x3f3f3f3f;
int book1[510],book2[510];
vector<int>f1[510];//Record shortest path information 
vector<int>f2[510];//Record the fastest path information 
vector<int>dans; //Final shortest output path 
vector<int>tans;//Final fastest output path 
struct node1{
	int x,d;
	friend bool operator <(node1 n1,node1 n2)
	{
		return n1.d>n2.d;
	}
};
struct node2{
	int  x,tt;
	friend bool operator <(node2 n1,node2 n2)
	{
		return n1.tt>n2.tt;
	}
};
//dijkstra() for heap optimization. Finding the shortest path 
void dijkstra1()
{
	memset(dis,0x3f,sizeof(dis));
	memset(book1,0,sizeof(book1));
	
	priority_queue<node1>q;
	q.push(node1{s,0});
	
	dis[s]=0;
	while(!q.empty())
	{
		node1 tmp=q.top();q.pop();
		
		int x=tmp.x;
		if(book1[x])continue;
		book1[x]=1;
		for(int i=0;i<g[x].size();i++)
		{
			int nx=g[x][i];
			if(dis[nx]>dis[x]+l[x][nx])//Relax and record precursor nodes 
			{
				dis[nx]=dis[x]+l[x][nx];
				q.push(node1{nx,dis[nx]});
				f1[nx].clear();         //If the shortest circuit is found, remember the previously saved precursor node 
				f1[nx].push_back(x);
			}
			else if(dis[nx]==dis[x]+l[x][nx])
			{
				f1[nx].push_back(x);//The shortest circuit is not unique 
			}
		}
		
	}
	
}
 
void dijkstra2()
{
	memset(_ime,0x3f,sizeof(_ime));
	memset(book2,0,sizeof(book2));
	
	priority_queue<node2>qq;
	qq.push(node2{s,0});
	_ime[s]=0;
	while(!qq.empty())
	{
		node2 tmp=qq.top();qq.pop();
		
		int x=tmp.x;
		if(book2[x])continue;
		book2[x]=1;
		for(int i=0;i<g[x].size();i++)
		{
			int nx=g[x][i];
			if(_ime[nx]>_ime[x]+t[x][nx])//Relax and record precursor nodes 
			{
				_ime[nx]=_ime[x]+t[x][nx];
				qq.push(node2{nx,_ime[nx]});
				f2[nx].clear();
				f2[nx].push_back(x);
			}
			else if(_ime[nx]==_ime[x]+t[x][nx])
			{
				f2[nx].push_back(x);//The shortest circuit is not unique 
			}
		}
		
	}
	
}
int sz=inf;
void dfs1(int x,int step)
{
	tmp[step]=x;
	if(x==s)
	{
		if(step<sz)
		{
			sz=step;
			dans.clear();
			for(int i=step;i>=1;i--)dans.push_back(tmp[i]);
		}
		return ;
	}
	for(int i=0;i<f1[x].size();i++)
	{
		dfs1(f1[x][i],step+1);
	}
}
int maxn=inf;
void dfs2(int x,int step,int cnt) 
{
	tmp[step]=x;
	if(x==s)
	{
		if(cnt<maxn)
		{
			maxn=cnt;
			tans.clear();
			for(int i=step;i>=1;i--)tans.push_back(tmp[i]);
		}
		return;
	}
	for(int i=0;i<f2[x].size();i++)
	{
		dfs2(f2[x][i],step+1,cnt+l[f2[x][i]][x]);//Note here, f2[x][i][x] never write backwards, because there may be a one-way road 
	}
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		int x,y,way,len,_ime;cin>>x>>y>>way>>len>>_ime;
		l[x][y]=len;
		t[x][y]=_ime;
		g[x].push_back(y);
		if(!way)g[y].push_back(x),l[y][x]=len,t[y][x]=_ime;
	}
	cin>>s>>e;
	dijkstra1();//shortest path 
	dfs1(e,1);//The shortest path with the least number of nodes 
	dijkstra2();// Fastest way 
	dfs2(e,1,0);//The shortest of the fastest 
	
	 
	if(tans==dans)
	{
		cout<<"Time = "<<_ime[e]<<"; Distance = "<<dis[e]<<": ";
		cout<<dans[0];
		for(int i=1;i<dans.size();i++)
		{
			cout<<" => "<<dans[i];
		}
		cout<<endl;
	}
	else{
		cout<<"Time = "<<_ime[e]<<": "<<tans[0];
		for(int i=1;i<tans.size();i++)
		{
			cout<<" => "<<tans[i];
		}
		cout<<endl;
		cout<<"Distance = "<<dis[e]<<": "<<dans[0];
		for(int i=1;i<dans.size();i++)
		{
			cout<<" => "<<dans[i];
		}
		cout<<endl;
	}
	
	return 0;
	
}
Published 47 original articles, won praise 23, visited 6976
Private letter follow

Posted by adam119 on Wed, 05 Feb 2020 23:22:48 -0800