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. MongoDB升级导致启动失败

    起因 最近项目使用MongoDB,但是作为一个技术菜鸟,NoSQL数据库我还真不会用,于是我就在自己的阿里云服务器上安装了一个MongoDB4.0.9. 现象 但是当我使用yum -y update升 ...

  2. Java千百问_03基本的语法(001)_局部变量、类变量、实例变量有什么差别

    点击进入_很多其它_Java千百问 局部变量.类变量.实例变量有什么差别 在聊局部变量.类变量.实例变量有什么差别之前,我们须要了解一下Java变量. 1.Java变量是什么 在数学世界中,我们知道有 ...

  3. POJ 3294 UVA 11107 Life Forms 后缀数组

    相同的题目,输出格式有区别. 给定n个字符串,求最长的子串,使得它同时出现在一半以上的串中. 不熟悉后缀数组的童鞋建议先去看一看如何用后缀数组计算两个字符串的最长公共子串 Ural1517 这道题的思 ...

  4. Java IO 字节流与字符流 (三)

    概述 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作的数据分为:字节流和字符流 流按流向不同分为:输入流和输出流 IO流常用基类 ...

  5. 不温不火WindowsPhone

    最近在考虑是否转其他平台,如iOS或者Android或者javascript等. 已经以Windows Phone 开发作为工作就一年了(也不算是真正的Windows Phone开发吧,仅仅是开发高德 ...

  6. 28. extjs中Ext.BLANK_IMAGE_URL的作用

    转自:https://blog.csdn.net/yiyuhanmeng/article/details/6960132 在使用ExtJS时,我们往往需要在使用之前加入这么一句:Ext.BLANK_I ...

  7. angularJs模版注入的两种方式

    一,声名式注入 1:app.js: var myApp = angular.module("myApp",["ngRoute"]); 2:controller. ...

  8. java 线程开元篇

    学习java的读者都知道,Java的每个对象都会有默认的12个方法,这12个方法分别是 object() finalize() hashCode() equals() wait() wait(long ...

  9. 基于Hexo且在GitHub上搭建博客

    title: 基于Hexo且在GitHub上搭建博客 Welcome to Fofade's Blog! 搭建初衷 大大小小,大学两年,玩了很多,也学了很多. 回首望之,曾经不知道的,现在是知道了,但 ...

  10. array_column() 函数[二维数组转为一维数组]

    array_column() 函数 输出数组中某个键值的集合[二维数组转为一位数组] <?php // 表示由数据库返回的可能记录集的数组 $a = array( array( 'id' =&g ...