Tram
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 11159   Accepted: 4089

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

Source

 
题解:带权值的最短路,floyd算法
代码:

 #include<iostream>
#include<cstdio>
using namespace std;
#define N 1000000
int map[][];
int main()
{
int n,sta,end,m,a;
int i,j; scanf("%d%d%d",&n,&sta,&end);
for(i=; i<=n; i++)
for(j=; j<=n; j++)
map[i][j]=N;
for(i=; i<=n; i++)
{
scanf("%d",&m);
for(j=; j<=m; j++)
{
scanf("%d",&a);
if(j==)
map[i][a]=;
else
map[i][a]=;
}
} int k;
for(k=; k<=n; k++) //floyd算法
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(map[i][k]+map[k][j]<map[i][j])
map[i][j]=map[i][k]+map[k][j]; if(map[sta][end]<N)
cout<<map[sta][end]<<endl;
else
cout<<-<<endl;
return ;
}

POJ_1847_Tram的更多相关文章

随机推荐

  1. node-load module

    javscript :脚本建共享全局名称空间(全局污染). node:实现CommonJS(公共)模块标准. Node加载模块,有两种方式: 1.通过名称 除非是核心模块,否则被引用的模块最后都会映射 ...

  2. java之Map源代码浅析

    Map是键值对.也是经常使用的数据结构. Map接口定义了map的基本行为.包含最核心的get和put操作,此接口的定义的方法见下图: JDK中有不同的的map实现,分别适用于不同的应用场景.如线程安 ...

  3. C# PDF Page操作——设置页面切换按钮 C# 添加、读取Word脚注尾注 C#为什么不能像C/C++一样的支持函数只读传参 web 给大家分享一个好玩的东西,也许你那块就用的到

    C# PDF Page操作——设置页面切换按钮   概述 在以下示例中,将介绍在PDF文档页面设置页面切换按钮的方法.示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页.下页.上页或者 ...

  4. Session与Cookie解析

    Session和Cookie这两个对象也接触了比較长的时间了,今天就来总结一下. 首先,他们都是会话跟踪中经常使用的技术.Cookie在client记录信息来确定用户身份,Session在服务端记录信 ...

  5. AVAudioSessionCategory的选择

    AVAudioSessionCategoryAmbient 或 kAudioSessionCategory_AmbientSound --用于非以语音为主的应用,使用这个category的应用会随着静 ...

  6. JSON-JSON字符串转换成JSON对象、JSON对象数组、java实体类以及保存到List列表中

    处理JSON字符串时,一直出错,写个样例后发现原来是没有弄清楚数据的格式问题. 实现的是 JSONString 转换成java对象 或是 list列表 实例类 News package lyx.ent ...

  7. Thinkphp中使用PHPmailer发送邮件

    在ThinkPHP\Extend\Vendor\目录下放入PHPMailer文件夹,里面包含以下文件 重置密码发送邮件 public function recover(){ if($this-> ...

  8. HDU 1724 Ellipse 【自适应Simpson积分】

    Ellipse Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. oracle数据库的导入 导出实例

    oracle数据库的导入 导出实例 分类: DataBase2011-09-07 23:25 377人阅读 评论(0) 收藏 举报 数据库oraclefileusercmdservice 我要从另外一 ...

  10. python调用java程序--jpype

    官方网站:http://jpype.sourceforge.net/ 官方使用文档:http://jpype.sourceforge.net/doc/user-guide/userguide.html ...