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;
}

  

poj 1847 Tram【spfa最短路】的更多相关文章

  1. POJ 1847 Tram (最短路)

    Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...

  2. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

  3. 最短路 || POJ 1847 Tram

    POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...

  4. POJ 1847 Tram --set实现最短路SPFA

    题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...

  5. [最短路径SPFA] POJ 1847 Tram

    Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...

  6. poj 1847( floyd && spfa )

    http://poj.org/problem?id=1847 一个水题,用来熟悉熟悉spfa和floyd的. 题意:有m条的铁路,要从x,到y, 之后分别就是条铁路与其他铁路的交点.第一个输入的为有n ...

  7. poj 1847 Tram

    http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...

  8. (简单) POJ 1847 Tram,Dijkstra。

    Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...

  9. POJ 1847 Tram【Floyd】

    题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...

随机推荐

  1. ES 的CRUD 简单操作(小试牛刀)

    URL的格式: http://localhost:9200/<index>/<type>/[<id>] 其中index.type是必须提供的. id是可选的,不提供 ...

  2. 更改css element.style

    样式后面加 !important就可以更改element.style的优先级了

  3. iPhone分辨率

    分辨率和像素 1.iPhone5           4"     分辨率320x568,像素640x1136,@2x 2.iPhone6           4.7"  分辨率3 ...

  4. Installing MySQL Server

    Installing MySQL Server Here we will learn how to Compile and Install the MySQL Server from source c ...

  5. PinchArea QML Type

    PinchArea类型是在QtQuick 1.1中添加进去的.PinchArea是一个不可见的对象,常用在与一个可见对象连接在一起,为对应的可见对象提供手势操作.enabled属性被用来去设置绑定对象 ...

  6. css3 3D盒子效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 自定义Web控件写事件

    --------------------myRegister1.ascx前台代码----------------------- <script src="js/Jquery1.7.js ...

  8. 用Enterprise Architect从源码自动生成类图

    http://blog.csdn.net/zhouyong0/article/details/8281192 /*references:感谢资源分享者.info:简单记录如何通过工具从源码生成类图,便 ...

  9. RestKit:iOS开发必备,告别众多无聊代码

    http://www.csdn.net/article/2014-04-15/2819312-RestKit-frameworkRestKit是一款专为iOS设计的Objective-C框架,旨在与R ...

  10. SQL Server索引 (原理、存储)聚集索引、非聚集索引、堆

    http://www.cnblogs.com/kissdodog/archive/2013/06/12/3132380.html