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. FaceBook开源库Fresco

    讨论学习使用 关于 Fresco Fresco 是一个强大的图片载入组件. Fresco 中设计有一个叫做 image pipeline 的模块.它负责从网络.从本地文件系统.本地资源载入图片. 为了 ...

  2. firewalld 防火墙 nat 网络地址转换

    目的:实现以下效果 一. 准备环境 @1 三台虚拟机 @2  client 端 ip  192.168.1.2      server端   两块网卡 , ip 分别是 192.168.1.1   和 ...

  3. Python随机播放电脑里的音乐

    就是找到硬盘中全部的MP3文件和wma文件.再随机打开当中的一个. import os,random disk=['D','E','F','G','H'] def search_file(filena ...

  4. C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)

    4.4 指向Member Function的指针 (Pointer-to-Member Functions) 取一个nonstatic data member的地址,得到的结果是该member在 cl ...

  5. Echarts 如何使用 bmap 的 API

    使用 Echarts 在绘制 Binning on map 的图形时(其实也就是 在地图上绘制热力色块图) 解决因为数据量过大,希望在拖拽加载或者缩放加载的时候,根据可视区域的经纬度范围,来请求相应的 ...

  6. android:模拟水波效果的自己定义View

    Github地址:https://github.com/nuptboyzhb/WaterWaveView 欢迎Fork.欢迎Star 1.先看效果 watermark/2/text/aHR0cDovL ...

  7. Android Studio常见问题

    1.导入他们项目时出现R文件出错 首先我们须要了解的是Android studio 是基于gradle的编译模式,内部没有gen文件夹更没有R文件,可是既然它报了这个错.肯定是有原因的.即Gradle ...

  8. Linux 系统内核空间与用户空间通信的实现与分析

    本文转载自:https://www.ibm.com/developerworks/cn/linux/l-netlink/index.html 多数的 Linux 内核态程序都需要和用户空间的进程交换数 ...

  9. 解决 django 中 mysql gone away 的问题

    最近在项目中,我使用 Django Command 模块写了一个脚本,处理从 MQ 发来的消息,并入库.在测试过程中,程序运行良好,但是在程序上线并运行一段时间后,出现了以下错误: Operation ...

  10. Eclipse使用Tomcat发布项目时出现YadisException异常解决方案

    调整使用Eclipse的JDK版本,大概JDK版本过低会出现这个org.openid4java.discovery.yadis.YadisException: 0x704: I/O transport ...