the best carTime limit:3000/1000ms (java/other) Memory limit:65535/32768k (java/other)Total Submission (s): Accepted submission (s): 6problem DescriptionH City is a tourist attraction. There are thousands of people visiting every year. For the convenience of tourists. Bus companies in various tourist attractions and hotels, restaurants and other places have set up bus stops and opened some one-way bus routes.
Each one-way bus line departs from a bus stop. Route several bus stops in turn. Finally arrive at the finish bus stop.
A traveler has recently traveled to H City, and he is very interested in his S park. But assuming that there is no one bus from his hotel to go directly to S Park, then he may have to take a bus ride a few stops, and then down to the same platform and a bus, so transfer several times to the S park.
Now,... with integers n give the full number of bus stops to H city. The bus stop at the hotel where the passenger is located is numbered N for the 1,s Park bus stop.
Write a program. Help the traveler find an optimal ride that allows him to transfer the fewest number of cars from the hotel to S park.
Input There are several sets of input data, the first row of each group of data has two numbers m and N (1<=m<=100 1<n<=500), indicating the opening of M-bus route, Total co-owned N stations.
From line 2nd to line m+1, the information on the bus route 1th to M is given in turn.
Section i+1 gives the information of the bus line I, from left to right in the order of execution to give the line of the entire station number, adjacent to two station number separated by a space.
Output There is only one row for each set of data output. Suppose you can't get to S park by bus from the hotel. The output is "NO". Otherwise output your program to find the minimum number of change, change the number of 0, indicating that no change can be reached. Sample Input
3 76 74 7 3 62 1 3 5
Sample Output
2
Basic or the processing of data, a bus line on the station is only to take once, then we set up a diagram, suppose I site and J site at the same time a bus line on a side, they have a weight of 1. And then with the shortest path, Dijkstra and floyed can handle
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
#define MAX 100000099
#define Q 501
using namespace Std;
void Dijkstra (int);
int num[q], a[q][q], dist[q];
int main ()
{
int M, N;
while (Cin >> M >> N)
{
for (int i = 1; I <= N; i++)
{
for (int j = 1; J <= N; j + +)
A[i][j]=max;
Dist[i]=max;
}
GetChar ();
int Len, x;
String str;
for (int i = 0; i < M; i++)
{
Getline (CIN,STR);
StringStream in (str);
len = 0;
while (in >> x) Num[++len] = x;
for (int j = 1; j < Len; j + +)
for (int k = j+1; k <= Len; k++)
A[NUM[J]][NUM[K]] = 1;
}
/*for (int i = 0; I <= N; i++)
{
for (int j = 0; J <= N; j + +)
cout << a[i][j] << "\ t";
cout << Endl;
}*/
Dijkstra (N);
if (dist[n] = = MAX) cout << "NO" << Endl;
else cout << dist[n]-1 << Endl;
}
}
void Dijkstra (int n)
{
S[n] to mark
int s[q],newdist,i;
for (i=1;i<=n;i++)
{
Dist[i]=a[1][i];
s[i]=0;
}
dist[1]=0;
S[1]=1;
for (i=2;i<=n;i++)
{
Find the smallest point from the initial position
int J,tem=max;
int u=1;
for (j=2;j<=n;j++)
if (!s[j]&&dist[j]<tem)
{
U=j;
TEM=DIST[J];
}
S[u]=1;
Update the value of Dist[n]
for (j=2;j<=n;j++)
{
if (!s[j]&&a[u][j]<max)
{
NEWDIST=DIST[U]+A[U][J];
if (Newdist<dist[j])
Dist[j]=newdist;
}
}
}
}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
The best car