Shortest best practice of car

H City is a tourist attraction, every year thousands of people come to visit.

For the convenience of tourists, bus companies in various tourist attractions and hotels, restaurants and other places have set up a bus stop and opened a number of one-way bus line.

Each one-way bus lines depart from a bus station, followed by passing through a number of bus stops, finally reach the end of the bus stop.

Recent visitors to a city tour H, S he wanted to go to the park to play, but if not a bus route can be reached directly from S Park Hotel where he was, he must first take a bus route may take a few stations, and then transfer down another bus route the same site after such transfer times reach S Park.

Now with integers 1,2, ... N to all of the city's bus station number H, agreed to the bus stop where passenger numbers this hotel is 1, No. S Park bus stop is N.

Write a program that helps passengers find an optimal travel plan, make him the least number of transfers in the car park from the hotel to the S process.
Input Format

The first line has two numbers N and M, the M-way opened represent bus lines, a total of N stations.

From the second row to row M + 1 are sequentially given to the information of the article M a bus line, wherein the first row i + 1 is given by the i-th information of the bus lines, running from left to right gives the order numbers to all stations on the line, separated by a space between two adjacent station number.
Output Format

Total line, if you can not reach the hotel by bus from the Park S, the output "NO", otherwise a minimum number of transfers, 0 indicates the number of transfers can be reached without change.
data range

1≤M≤100
,
1≤N≤500

Sample input:

3 7
6 7
4 7 3 6
2 1 3 5

Sample output:

2

Report problem solving: stringstream can be used to read, because the data nausea, followed by attention to detail, with getline eat it in front of the space, if the start and end in the same spot to take a max, with bfs than ordinary algorithm spfa do what you want save time, do not open st arrays, due to the weight between each point is 1 before they can do with bfs.

#include<iostream>
#include<sstream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const   int N=2010;
int n,m;
int g[N][N];
int stop[N];
int dist[N];
void bfs()
{
    memset(dist,0x3f,sizeof dist);
    dist[1]=0;
    queue<int>q;
    q.push(1);
    while(q.size())
    {
        int t=q.front();
        q.pop();
        for(int j=1;j<=n;j++)
        {
            if(dist[j]>dist[t]+g[t][j]&&g[t][j])
            {
                dist[j]=dist[t]+g[t][j];
                q.push(j);
            }
        }
    }
}
int main()
{
    cin>>m>>n;
    string line;
    getline(cin,line);
    while(m--)
    {
        int cnt=0;
        getline(cin,line);
        stringstream   ss(line);
        int d;
        while(ss>>d)
        {
            stop[cnt++]=d;
        }
        for(int i=0;i<cnt;i++)
        for(int j=i+1;j<cnt;j++)
        g[stop[i]][stop[j]]=1;
    }
    bfs();
    if(dist[n]==0x3f3f3f3f)    cout<<"NO"<<endl;
    else    cout<<max(dist[n]-1,0)<<endl;
    return 0;
}
Published 165 original articles · won praise 8 · views 2470

Guess you like

Origin blog.csdn.net/qq_45961321/article/details/105001022