PTA ladder map c++ version - Shandong University of Science and Technology

Topic:
This question requires you to implement an exclusive online map for ladder competitions. After players enter their school location and venue location, the map should recommend two routes: one is the fastest arrival route; the other is the shortest distance route. The topic guarantees that for any query request, there is at least one reachable route on the map.
Input format:
Input Two positive integers N (2 ≤ N ≤ 500) and M are given in the first line, which are the number of all marked locations in the map and the number of roads connecting the locations, respectively. Followed by M lines, each line gives the information of a road in the following format: V1 V2 one-way length time
where V1 and V2 are the numbers of the two endpoints of the road (from 0 to N-1); if the road is from V1 For the one-way line 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

Then on the next line output the shortest distance D and the route represented by the node number in the following format:

Distance = D: start point => node 1 => ... => end point

If the fastest arrival route is not unique, output the shortest one among the fastest routes, and the title guarantees that this route is unique. And if the route with the shortest distance is not unique, then output the route with the least number of nodes, and the title guarantees that this route is unique.

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

Time = T; Distance = D: start point => node 1 => ... => end point

Input sample 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 sample 1:

Time = 6: 5 => 4 => 8 => 3
Distance = 3: 5 => 1 => 3

Input sample 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 sample 2:

Time = 3; Distance = 4: 3 => 2 => 5
#include<bits/stdc++.h>
using namespace std;
const int MAX=0x3f3f3f3f;
const int maxn=505;
int n,m,s,d;
struct ROAD
{
    
    
    bool one_way;
    int length;
    int time;
} road[maxn][maxn];
struct NODE
{
    
    
    int time_pre;
    int length_pre;
    int nodenum;
    bool vis;
    int dist;
} node[maxn];
bool judge(int a[],int b[],int x,int y)
{
    
    
    if(x==y)
    {
    
    
        for(int i=0; i<x; i++)
        {
    
    
            if(a[i]!=b[i])
                return false;
        }
        return true;
    }
    return false;
}
void init()
{
    
    
    for(int i=0; i<maxn; i++)
        for(int j=0; j<maxn; j++)
            road[i][j].length=road[i][j].time=MAX;
    cin>>n>>m;
    int v1,v2,one_way,length,time;
    for(int i=0; i<m; i++)
    {
    
    
        scanf("%d%d%d%d%d",&v1,&v2,&one_way,&length,&time);
        if(!one_way)
        {
    
    
            road[v1][v2].length=road[v2][v1].length=length;
            road[v1][v2].time=road[v2][v1].time=time;
        }
        else
        {
    
    
            road[v1][v2].length=length;
            road[v1][v2].time=time;
        }
    }
    cin>>s>>d;
}
void Dijkstra_time()
{
    
    
    for(int i=0; i<maxn; i++)
    {
    
    
        node[i].vis=false;
        node[i].dist=MAX;
        node[i].time_pre=-1;
    }
    road[s][s].time=0;
    node[s].dist=0;
    for(int i=0; i<n; i++)
    {
    
    
        int minn=MAX,f=-1;
        for(int j=0; j<n; j++)
        {
    
    
            if(!node[j].vis&&road[s][j].time<minn)
            {
    
    
                minn=road[s][j].time;
                f=j;
            }
        }
        if(f==-1)
            break;
        node[f].vis=true;
        for(int t=0; t<n; t++)
        {
    
    
            if(node[t].vis)
                continue;
            if(road[s][f].time+road[f][t].time<road[s][t].time)
            {
    
    
                road[s][t].time=road[s][f].time+road[f][t].time;
                node[t].time_pre=f;
                node[t].dist=node[f].dist+road[f][t].length;
            }
            else if(road[s][f].time+road[f][t].time==road[s][t].time)
            {
    
    
                if(node[f].dist+road[f][t].length<node[t].dist)
                {
    
    
                    node[t].time_pre=f;
                    node[t].dist=node[f].dist+road[f][t].length;
                }
            }
        }
    }
}
void Dijkstra_length()
{
    
    
    for(int i=0; i<maxn; i++)
    {
    
    
        node[i].vis=false;
        node[i].nodenum=MAX;
        node[i].length_pre=-1;
    }
    node[s].nodenum=1;
    road[s][s].length=0;
    for(int i=0; i<n; i++)
    {
    
    
        int minn=MAX,f=-1;
        for(int j=0; j<n; j++)
        {
    
    
            if(!node[j].vis&&road[s][j].length<minn)
            {
    
    
                minn=road[s][j].length;
                f=j;
            }
        }
        if(f==-1)
            break;
        node[f].vis=true;
        for(int t=0; t<n; t++)
        {
    
    
            if(node[t].vis)
                continue;
            if(road[s][f].length+road[f][t].length<road[s][t].length)
            {
    
    
                road[s][t].length=road[s][f].length+road[f][t].length;
                node[t].length_pre=f;
                node[t].nodenum=node[f].nodenum+1;
            }
            else if(road[s][f].length+road[f][t].length==road[s][t].length)
            {
    
    
                if(node[f].nodenum+1<node[t].nodenum)
                {
    
    
                    node[t].nodenum=node[f].nodenum+1;
                    node[t].length_pre=f;
                }
            }
        }
    }
}
void solve()
{
    
    
    int cnt=d;
    int a[maxn],x=0,b[maxn],y=0;
    while(cnt!=-1)
    {
    
    
        a[x++]=cnt;
        cnt=node[cnt].time_pre;
    }
    cnt=d;
    while(cnt!=-1)
    {
    
    
        b[y++]=cnt;
        cnt=node[cnt].length_pre;
    }
    if(judge(a,b,x,y))
    {
    
    
        printf("Time = %d; Distance = %d:",road[s][d].time,road[s][d].length);
        for(int i=x-1; i>=0; i--)
        {
    
    
            cout<<' '<<a[i];
            if(i)
                cout<<" =>";
        }
    }
    else
    {
    
    
        printf("Time = %d:",road[s][d].time);
        for(int i=x-1; i>=0; i--)
        {
    
    
            cout<<' '<<a[i];
            if(i)
                cout<<" =>";
        }

        cout<<endl;
        printf("Distance = %d:",road[s][d].length);
        for(int i=y-1; i>=0; i--)
        {
    
    
            cout<<' '<<b[i];
            if(i)
                cout<<" =>";
        }

    }
}
int main()
{
    
    
    //freopen("in.txt","r",stdin);
    init();
    Dijkstra_length();
    Dijkstra_time();
    solve();
}

For more PTA codes, please refer to my blog

ps: The code is for reference only, please do not plagiarize

Guess you like

Origin blog.csdn.net/scorpion_legend/article/details/109692247