L3-007 Ladder map (30 points): the shortest path

This question requires you to implement a dedicated online map for the ladder competition. After the players enter their school location and venue location, the map should recommend two routes: one is the fastest route and the other is the shortest distance route. The title guarantees that for any query request, there is at least one reachable route on the map.

Input format:
Enter two positive integers N (2 ≤ N ≤ 500) and M in the first line, which are the number of all marked locations on the map and the number of roads connecting the locations. Then M lines, each line gives information about a road in the following format:

V1 V2 one-way length time
where V1 and V2 are the numbers of the two end points of the road (from 0 to N-1); if the road is a one-way line from V1 to V2, one-way is 1, otherwise it is 0; length is the length of the road; time is the time required to pass the road. Finally, a pair of start and end numbers are given.

Output format:
First, output the fastest arrival time T and the route represented by the node number in the following format:

Time = T: start point => node 1 =>… => end point
and then output the shortest distance D and the route represented by the node number in the following format on the next line:

Distance = D: Starting point => Node 1 =>… => End point.
If the fastest route is not unique, output the shortest one of the several fastest routes. The question guarantees that this route is unique. And if the route with the shortest distance is not unique, the route with the least number of nodes is output, and the problem is guaranteed to be unique.

If the two routes are exactly the same, output in the following format:

Time = T; Distance = D: Starting point => Node 1 =>… => End point
Input example 1:
10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
5 4 0 2 3
5 9 1 1 4
0 6 0 1 1
7 3 1 1 2
8 3 1 1 2
2 5 0 2 2
2 1 1 1 1
1 5 0 1 3
1 4 0 1 1
9 7 1 1 3
3 1 0 2 5
6 3 1 2 1
5 3
Output example 1:
Time = 6: 5 => 4 => 8 => 3
Distance = 3: 5 => 1 => 3
Input example 2:
7 9
0 4 1 1 1
1 6 1 3 1
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 3 1
3 2 1 2 1
4 5 0 2 2
6 5 1 2 1
3 5
Output example 2:
Time = 3; Distance = 4: 3 => 2 => 5

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int cnt,s[600],nt[1000010],e[1000010];
int p1[600],p2[600],visit[600];
int dis1[600],t[600],dis2[600],sum[600];
struct Edge
{
    
    
    int dis,t;
}x[1000090];
struct node1
{
    
    
    int id,dis;
    friend bool operator <(node1 a,node1 b)
    {
    
    
        return a.dis>b.dis;
    }
}pre1,nt1;
struct node2
{
    
    
    int id,t;
    friend bool operator <(node2 a,node2 b)
    {
    
    
        return a.t>b.t;
    }
}pre2,nt2;
void dij1(int ss,int ee)
{
    
    
    pre1.id=ss,pre1.dis=0;
    priority_queue<node1>q;
    memset(visit,0,sizeof visit);
    memset(dis1,0x3f3f3f3f,sizeof dis1);
    memset(sum,0x3f33f3f,sizeof sum);
    sum[ss]=0,dis1[ss]=0;
    q.push(pre1);
    while(!q.empty())
    {
    
    
        pre1=q.top();
        q.pop();
        visit[pre1.id]=1;
        if(pre1.id==ee) break;
        for(int i=s[pre1.id];~i;i=nt[i])
        {
    
    
            int ee=e[i];
            if(visit[ee]) continue;
            if(dis1[ee]>dis1[pre1.id]+x[i].dis)
            {
    
    
                dis1[ee]=dis1[pre1.id]+x[i].dis;
                sum[ee]=sum[pre1.id]+1;
                p1[ee]=pre1.id;
                nt1.dis=dis1[ee];
                nt1.id=ee;
                q.push(nt1);
            }
            else if(dis1[ee]==dis1[pre1.id]+x[i].dis&&sum[ee]>sum[pre1.id]+1)
            {
    
    
                sum[ee]=sum[pre1.id]+1;
                p1[ee]=pre1.id;
            }
        }
    }
}
 
void dij2(int ss,int ee)
{
    
    
    pre2.id=ss,pre2.t=0;
    priority_queue<node2>q;
    memset(visit,0,sizeof visit);
    memset(t,0x3f3f3f3f,sizeof t);
    memset(dis2,0x3f3f3f3f,sizeof dis2);
    t[ss]=0,dis2[ss]=0;
    q.push(pre2);
    while(!q.empty())
    {
    
    
        pre2=q.top();
        q.pop();
        visit[pre2.id]=1;
        if(pre2.id==ee) break;
        for(int i=s[pre2.id];~i;i=nt[i])
        {
    
    
            int ee=e[i];
            if(visit[ee]) continue;
            if(t[ee]>t[pre2.id]+x[i].t)
            {
    
    
                t[ee]=t[pre2.id]+x[i].t;
                dis2[ee]=dis2[pre2.id]+x[i].dis;
                p2[ee]=pre2.id;
                nt2.t=t[ee];
                nt2.id=ee;
                q.push(nt2);
            }
            else if(t[ee]==t[pre2.id]+x[i].t&&dis2[ee]>dis2[pre2.id]+x[i].dis)
            {
    
    
                dis2[ee]=dis2[pre2.id]+x[i].dis;
                p2[ee]=pre2.id;
            }
        }
    }
}
int main()
{
    
    
    int n,m;
    cin>>n>>m;
	cnt=1;
    memset(s,-1,sizeof s);
    memset(nt,-1,sizeof nt);
    int u,v,k,l,time;
    for(int i=1;i<=m;i++)
    {
    
    
        cin>>u>>v>>k>>l>>time;
        nt[cnt]=s[u],s[u]=cnt,e[cnt]=v,x[cnt].dis=l,x[cnt++].t=time;
        if(k==0) nt[cnt]=s[v],s[v]=cnt,e[cnt]=u,x[cnt].dis=l,x[cnt++].t=time;
    }
    cin>>u>>v;
    dij1(u,v);
    dij2(u,v);
    //判断路径是否重复 
    int kk=v,flag=1;
    while(p1[kk]!=u)
    {
    
    
        if(p1[kk]!=p2[kk]) {
    
    flag=0;break;}
        kk=p1[kk];
    }
	//输出 
	if(flag)
	{
    
    
		cout<<"Time = "<<t[v]<<"; Distance = "<<dis1[v]<<": "<<u; 
		stack<int>ss;
		kk=v;
		while(kk!=u)
		{
    
    
			ss.push(kk);kk=p1[kk];
		}
		while(ss.size())
		{
    
    
			cout<<" => "<<ss.top();
			ss.pop();
		}
	}
	else
	{
    
    
		cout<<"Time = "<<t[v]<<": "<<u;
		stack<int>ss;
		kk=v;
		while(kk!=u)
		{
    
    
			ss.push(kk);kk=p2[kk];
		}
		while(ss.size())
		{
    
    
			cout<<" => "<<ss.top();
			ss.pop();
		}
		cout<<endl;
		cout<<"Distance = "<<dis1[v]<<": "<<u;
		kk=v;
		while(kk!=u)
		{
    
    
			ss.push(kk);kk=p1[kk];
		}
		while(ss.size())
		{
    
    
			cout<<" => "<<ss.top();
			ss.pop();
		}
	}
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43540515/article/details/110258369