Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there is 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 the switch is pointing. If the driver wants to go some other way, he/she has to manually change the switch.
When a driver has do drive from intersection A to the intersection B he/she tries to choose the route that will minimize the number of times he/she will have to change the switches manually.
Write a program that will calculate the minimal number of switch changes necessary to travel from intersection A to intersection B.
Input
The first line of the input contains integers N, A and B, separated by a single blank character, 2 <= N <= 100, 1 <= A, B <= N, N is the number of intersections in the network, and intersections are 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 intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is 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 1
2 2 3
2 3 1
2 1 2
Sample Output
0
题意:第一行先给出N,A,B,;
N是下面有N行,A是过来的铁轨,B是要出的轨道;
下面每行第一个数为k,表示这一行有k个数,
这k个数中,第一个数是本来要去的轨道,后k-1个数是可以挪到的位置。
弗洛伊得
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
const int INF=0x3f3f3f3f;
#define MAX 105
int map[MAX][MAX];//开一个数组
int N,A,B;
void Flyd()//弗洛伊得关键点,懂这个即可
{
//第四步,直接找个中间转换点来找两点之间的最短路,虽然方便但是时间复杂度较高
for(int k=1; k<=N; k++)//中间转换点
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
if(map[i][k]<INF&&map[k][j]<INF&&map[i][k]+map[k][j]<map[i][j])
map[i][j]=map[i][k]+map[k][j];
}
int main()
{
scanf("%d%d%d",&N,&A,&B);
//第一步,初始化
for(int i=0; i<=N; i++)
for(int j=0; j<=N; j++)
i==j?map[i][j]=0:map[i][j]=INF;
//第二步,存图
for(int i=1; i<=N; i++)
{
int k;
scanf("%d",&k);
for(int j=0; j<k; j++)
{
int u;
scanf("%d",&u);
j==0?map[i][u]=0:map[i][u]=1;//第一个数是原本要去的轨道,不用挪动
}
}
//第三步
Flyd();
map[A][B]<INF?printf("%d\n",map[A][B]):printf("-1\n");
return 0;
}
迪杰斯特拉
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int spot=1000+10;
int mm[spot][spot];
int dis[spot];
int vis[spot];
int N,A,B;
void Dijkstra(int A)
{
memset(vis,0,sizeof(vis));
vis[A]=1;
for(int i=1; i<=N; i++)
dis[i]=mm[A][i];
for(int i=1; i<N; i++)
{
int minn=INF;
int point;
for(int j=1; j<=N; j++)
{
if(vis[j]==0&&dis[j]<minn)
{
minn=dis[j];
point=j;
}
}
vis[point]=1;
for(int j=1; j<=N; j++)
{
if(vis[j]==0&&mm[point][j]<INF&&dis[point]+mm[point][j]<dis[j])
dis[j]=dis[point]+mm[point][j];
}
}
}
int main()
{
scanf("%d%d%d",&N,&A,&B);
for(int i=0; i<=N; i++)
for(int j=0; j<=N; j++)
i==j?mm[i][j]=0:mm[i][j]=INF;
for(int i=1; i<=N; i++)
{
int k;
scanf("%d",&k);
for(int j=0; j<k; j++)
{
int v;
scanf("%d",&v);
j==0?mm[i][v]=0:mm[i][v]=1;
}
}
Dijkstra(A);
if(dis[B]<INF)
printf("%d\n",dis[B]);
else
printf("-1\n");
return 0;
}