poj 1847 Tram【spfa最短路】

Tram
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 12005   Accepted: 4365

Description

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
题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是火车默认的是第一个指向的方向,如果选择别的方向需要 进行一次切换操作 ,给定一个起点一个终点 ,问最少进行几次 切换操作 能够 使 火车从起点到达终点 , 若无法到达输出“-1”。
输入:第i行指的就是第i个路口,第i行的第一个数k表示这一行后边有k个数每个数都与i路口相连,但是只于k后边第一个数直接相连
思路:设默认路径边权为0,备选路径边权为1,求单源最短路即可。
#include<stdio.h>
#include<string.h>
#define MAX 1100
#define INF 0x3f3f3f
#include<queue>
using namespace std;
int head[MAX];
int n,beg,en,ans;
int dis[MAX],vis[MAX];
struct node
{
int u,v,w;
int next;
}edge[MAX];
void add(int u,int v,int w)
{
edge[ans].u=u;
edge[ans].v=v;
edge[ans].w=w;
edge[ans].next=head[u];
head[u]=ans++;
}
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void getmap()
{
int i,j;
for(i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
for(j=0;j<k;j++)
{
int a;
scanf("%d",&a);
if(j==0)
add(i,a,0);
else
add(i,a,1);
}
}
}
void spfa(int sx)
{
int i,j;
queue<int>q;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
dis[i]=INF;
vis[sx]=1;
dis[sx]=0;
q.push(sx);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(i=head[u];i!=-1;i=edge[i].next)
{
int top=edge[i].v;
if(dis[top]>dis[u]+edge[i].w)
{
dis[top]=dis[u]+edge[i].w;
if(!vis[top])
{
vis[top]=1;
q.push(top);
}
}
}
}
if(dis[en]==INF)
printf("-1\n");
else
printf("%d\n",dis[en]);
}
int main()
{
while(scanf("%d%d%d",&n,&beg,&en)!=EOF)
{
init();
getmap();
spfa(beg);
}
return 0;
}

  

上一篇:hiho(1081),SPFA最短路,(非主流写法)


下一篇:HNU 13375 Flowery Trails (spfa最短路)