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. IOS-objectForKey与valueForKey在NSDictionary中的差异

    从 NSDictionary 取值的时候有两个方法,objectForKey: 和 valueForKey:,这两个方法具体有什么不同呢? 先从 NSDictionary 文档中来看这两个方法的定义: ...

  2. ios开发中MVC模式的理解

    MVC是80年代出现的一种软件设计模式,是模型(model),视图(view)和控制(Controller)的缩写. 其中Model的主要功能包括业务逻辑的处理以及数据的访问,这是应用程序的主体部分. ...

  3. 完全背包的变形POJ1252

    话说今天做背包做到有点累了,题目是英文的--而且还很长,我看了好久(弱爆了). 题目大概的意思就是,有六种硬币,之后,求用这六种硬币最小数目支付1到100美分的平均值,以及最小数目中的最大值. 很容易 ...

  4. Qt5对付中文真好用

    Qt好多C++程序员都在用,Qt4大家可能用的多,到了Qt5不熟悉的人到是很多,其中我喜欢的特性也是和Qt4大不一样的地方就是对中文的处理. Qt4中使用“QTextCodec::setCodecFo ...

  5. ES 必备插件的安装

    1. elasticsearch-head插件的安装,非常好的插件 elasticsearch-head是一个elasticsearch的集群管理工具,它是完全由html5编写的独立网页程序,你可以通 ...

  6. ios开发中加载的image无法显示

    昨天遇到一个较奇葩的问题,imageName加载的图片显示不出来,网上查了好多资料还是没找到解决的方法: 之前图片是放在项目中SupportingFiles文件下的,怎么加载都能显示图片,于是将图片拿 ...

  7. codeblocks快捷键(转载)

    • 按住Ctrl滚滚轮,代码的字体会随你心意变大变小. • 在编辑区按住右键可拖动代码,省去拉(尤其是横向)滚动条之麻烦:相关设置:Mouse Drag Scrolling. • Ctrl+D可复制当 ...

  8. Vive开发教程汇总

    最近在整理在HTC Vive平台上开发VR应用程序的教程,现在把结果全部汇总在下面的表格里,希望更多的开发者参与到VR内容的开发之中,真的很好玩.现在主流的开发VR应用的引擎是Unity3D和Unre ...

  9. ListView复用和优化详解

    我们每一个Android开发人员对ListView的使用肯定是很熟悉的,然而多少人能真正的懂ListView的缓存机制呢,说白了就是ListView为了提高效率,而内部实现的一种优化,牺牲一点内存.而 ...

  10. 请大神帮忙解决 jquery 控制 li 标签问题

    <li class="active"><a href="#1" data-toggle="tab">日志详细情况&l ...