Luogu P1346 tram (shortest path problem)

Locke P1346 Tram


Original title address



1. Subject content

There is a special tram network in a magical town, which is composed of some intersections and tracks. Each intersection is connected to several tracks, and each track leads to an intersection (do not rule out that some sightseeing tracks make a circle It is possible to return to the intersection afterwards). At each intersection, there is a switch that determines the exit track, and each switch has a default state. After each tram reaches the intersection, it can only exit from the track pointed by the switch. If the tram driver wants to go another Track, he must get off to switch the state of the switch.

In order to drive to the target location, tram drivers have to get off frequently to switch the switch, so they want you to write a program to calculate the minimum number of times a car needs to get off and switch the switch from junction A to junction B.

Input format The
first line has 3 integers N, A, B (2≤N≤100,1≤A,B≤N), which respectively represent the number of intersections, and the start and end points of the tram.
Next, there are N rows. At the beginning of each row, there is a number Ki (0≤Ki≤N−1), which means that this intersection is connected to Ki tracks. Next, there are Ki numbers that indicate the intersection that each track leads to. Switch By default, it points to the track indicated by the first number.

Output format The
output file has only one number, which means the minimum number of switches required to go from A to B. If it is not possible to go from A to B, output −1.

Input and output sample
input #1

3 2 1
2 2 3
2 3 1
2 1 2

Output #1

0

Two, problem-solving ideas

You can use djikstra for this question, or floyd. This question is equivalent to walking from the starting point to the end, finding the minimum total distance. For each point, if the point reached is the first edge connected by this point, the total distance is not added; if it is other edges, add one. Therefore, all edges are initialized to positive infinity before creating edges. Because the data is not very big, so just use floyd. I habitually use my favorite violent solution (no need to use my brain)

code show as below:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=100+10;
const int inf=1e+8;//定义无穷大
int g[maxn][maxn];//地图
int n,m,k,a,b;
int main()
{
    
    
    cin>>n>>a>>b;//路口数,起点和终点
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
    
    
            g[i][j]=inf;//初始化地图
            g[i][i]=0;//自己踩过的地方不用按开关
        } 
    for(int i=1;i<=n;i++)
    {
    
    
        cin>>k;//每个路口与k条轨道相连
        for(int j=1;j<=k;j++)
        {
    
    
            cin>>m;//k个表示每条轨道所通向的路口
            if(j==1)//是否是第一条边
                g[i][m]=0;
            else 
                g[i][m]=1;//需要按开关
        }
    }
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                g[i][j]=min(g[i][j],g[i][k]+g[k][j]);//通过中间点k求得是从i-j近还是从i-k-j近,从而求得a到b的最短路径
    if(g[a][b]==inf)//没找到
        cout<<"-1";
    else 
        cout<<g[a][b];
    return 0;
}

Three, matters needing attention

This problem is the shortest path problem, a classic problem that can be solved by dijksra, floyd and spfa. For Floyd alone, this problem must first find the core algorithm g[i][j]=min(g[i][j],g[i][k]+g[k][j]), and then figure out how to build the map and how to judge it. Another thing we should pay attention to is that every side we build is only one-way, so don't use it g[i][j]=g[j][i].
Konjac writes poorly and has reference to the explanations of some bigwigs. Please criticize and correct the bad points.

Guess you like

Origin blog.csdn.net/jotaro07/article/details/110298231