Tram
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 13468 |
|
Accepted: 4954 |
Description
Tram Network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there are a switch pointing to the one of the rails going out of the intersection. When the tram enters the intersection it can leave only in the direction of the switch is pointing. If the driver wants to go some other, he/she have to manually the switch.
When a driver have do drive from intersection A to the intersection B he/she tries to choose the route that would minimize t The He number of times he/she'll has to change the switches manually.
Write a program that would calculate the minimal number of switch changes necessary to travel from intersection A to Inters Ection B.
Input
The first line of the input contains integers n, a and B, separated by A single blank character, 2 <= N <=, 1 &L t;= A, B <= N, N is the number of the intersections in the network, and intersections be numbered from 1 to N.
Each of the following N lines contain a sequence of integers separated by a single blank character. First number in the I-th line, Ki (0 <= ki <= N-1), represents the number of rails going out of the i-th Intersectio N. Next Ki numbers represents the intersections directly connected to the i-th intersection. Switch in the i-th intersection are initially pointing in the direction of the first intersection listed.
Output
The first and only line of the output should contain the target minimal number. If there is no route from A to B, the line should contain the integer "-1".
Sample Input
3 2 12 2 32 3 12 1 2
Sample Output
0
Source
Croatia OI 2002 Regional-juniors
Original title Link: http://poj.org/problem?id=1847
Reference translation: http://blog.csdn.net/zhang20072844/article/details/7761273 Test instructions: The meaning of this topic is really difficult to understand, it should be my English is too poor, first explain the following test instructions:
Is that there are n intersections, just as there are n points on the line, and then these points and other points some path, each point is a switch, the switch can only have One direction to go one way, and the first number is the default switch point, do not rotate.
This single made a mistake, that is, the default point is actually only need to rotate 0 times, and the other paths only need to rotate 1 times, regardless of which, just 1 times, originally thought, the second 1 times, 3rd 2 times.
Examples of topics given
3 2 1//with 3 switching points, calculate the minimum number of rotations required from the second to the first
2 2 3//1th switch can lead to 2 and 3, leading to 2 does not need rotation, leading to 3 need to rotate 1 times
2 3 1//2nd switch can lead to 3 and 1, leading to 3 does not need rotation, leading to 1 need to rotate 1 times
2 1 2//3rd switch can lead to 1 and 2, leading to 1 does not need rotation, leading to 2 need to rotate 1 times
Do you have any questions after you know the meaning of the title?
Dijkstra algorithm, Floyd algorithm, SPFA algorithm all come!!
AC Code:
#include <iostream> #include <cstdio> #include <queue>using namespace std;const int inf=0x3f3f3f3f; int a[105][105];int dis[105];bool vis[105];int n,s,t;void Dij () {for (int i=1; i<=n; i++) {dis[i]=a[s][i]; Vis[i]=false; } vis[s]=true; dis[s]=0; for (int i=1; i<=n; i++) {int minn=inf; int p; for (int j=1; j<=n; J + +) {if (!vis[j]&&dis[j]<minn) {Minn=dis[j] ; P=j; }} vis[p]=true; if (minn==inf) break; for (int j=1; j<=n; J + +) {if (!vis[j]&&dis[j]>dis[p]+a[p][j]) dis[i]=dis[p]+ A[P][J]; }}}void Floyd ()//floyd algorithm {for (int i = 1; I <= n; + + i) {for (int j = 1; J <= N; + + j) { for (int k = 1; k <= N; + + K) {if (A[j][k] > A[j][i] + a[i][k]) {A[j][k] = A[j][i] + a[i][k]; }}}} if (A[s][t] >= INF) printf (" -1\n"); else printf ("%d\n", A[s][t]);} void Spfa () {for (int i=1;i<=n;i++) {dis[i]=inf; Vis[i]=false; } dis[s]=0; Vis[s]=true; queue<int>q; Q.push (s); while (!q.empty ()) {int P=q.front (); Q.pop (); VIS[P]=FALSE;///!!!!!! for (int i=1;i<=n;i++) {if (Dis[i]>dis[p]+a[p][i]) {dis[i]=dis[p]+a[p][i ]; if (!vis[i]) {vis[i]=true; Q.push (i); }}}}}int main () {while (cin>>n>>s>>t) {for (int i=1; i<=n; i++) {for (int j=1; j<=n; J + +) if (i==j) a[i][j]=0; else A[i][j]=inf; } int x, y, Z; for (int i=1; i<=n; i++) {scanf ("%d", &x); for (int j=0; j<x; J + +) {scanf ("%d", &y); if (j==0) a[i][y]=0; else a[i][y]=1; }}//floyd (); Dij (); SPFA (); if (Dis[t]<inf) cout<<dis[t]<<endl; Else cout<< "-1" <<endl; } return 0;}
POJ 1847 Tram "Shortest way, SPFA algorithm, test instructions understanding is the key!! 】