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. HDU1215--七夕节

    找出小于N的全部因子的和,N比較大,非常明显要打表来做,不然肯定会超时 方法就是枚举范围内每一个整数.然后再枚举范围内这个整数的全部的倍数,加上这个数 由于这个整数的倍数中一定含有这个整数因子,这样速 ...

  2. 工作总结 for 另类写法 循环加时间 集合合并 也是用的 static class Enumerable (IEnumerable<T>的扩展方法) (IEnumerable<T> 的 工具类) (所有集合 数组都实现IEnumerable<T>)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

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

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

  4. B. Error Correct System (CF Round #296 (Div. 2))

    B. Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. [测试]单元测试框架NUnit

    说到测试,相信大家都或多或少了解. 按照各自分类,就自己知道包括 A.单元测试.集成测试.系统测试 B.白盒测试.黑盒测试 C.压力测试.性能测试.安全测试 ...... 反正是太多太多.就做开发以来 ...

  6. 【前端】jQuery的animate在火狐浏览器上不支持backgroundPosition的解决方法

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/4375678.html jQuery的animate是一个非常好用的东东,但某些动画效果支持得不够好,比如back ...

  7. 洛谷 P2585 [ ZJOI 2006 ] 三色二叉树 —— 树形DP

    题目:https://www.luogu.org/problemnew/show/P2585 首先,三色其实记录两种状态:是绿色,不是绿色 即可,因为红蓝可以随意取反: 一开始因为懒得还原出树,所以写 ...

  8. LED全彩显示屏色度空间

    摘要:LED全彩显示屏.LED电子大屏幕如果要有一个良好的视觉效果,其中色度占有一席重要的位置,那么该如何让LED显示屏的色度更均匀.合理呢,下面为大家总结出以下几点,供大家参考. LED全彩显示屏. ...

  9. bzoj2338

    计算几何 我们先把所有的线段求出来,我们发现只有两个线段等长且中点重合时才能构成矩形,那么线段有n*n条,我们按中点,长度排序,然后对于一条线段扫描所有符合条件的线段计算答案,这样看起来是O(n^3) ...

  10. jquery input 赋值和取值

    记录一下: 在写一个input赋值,二话不说就直接利用了$('#xx').val()来进行取值和赋值,取值ok,赋值后利用alert显示正确,但是在html上并没有正确的显示出来,后来改为使用如下代码 ...