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

  1. 3 2 1
  2. 2 2 3
  3. 2 3 1
  4. 2 1 2

Sample Output

  1. 0

Source

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

  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. #define N 1000000
  5. int map[][];
  6. int main()
  7. {
  8. int n,sta,end,m,a;
  9. int i,j;
  10.  
  11. scanf("%d%d%d",&n,&sta,&end);
  12. for(i=; i<=n; i++)
  13. for(j=; j<=n; j++)
  14. map[i][j]=N;
  15. for(i=; i<=n; i++)
  16. {
  17. scanf("%d",&m);
  18. for(j=; j<=m; j++)
  19. {
  20. scanf("%d",&a);
  21. if(j==)
  22. map[i][a]=;
  23. else
  24. map[i][a]=;
  25. }
  26. }
  27.  
  28. int k;
  29. for(k=; k<=n; k++) //floyd算法
  30. for(i=; i<=n; i++)
  31. for(j=; j<=n; j++)
  32. if(map[i][k]+map[k][j]<map[i][j])
  33. map[i][j]=map[i][k]+map[k][j];
  34.  
  35. if(map[sta][end]<N)
  36. cout<<map[sta][end]<<endl;
  37. else
  38. cout<<-<<endl;
  39. return ;
  40. }

POJ_1847_Tram的更多相关文章

随机推荐

  1. 中文分词实践(基于R语言)

    背景:分析用户在世界杯期间讨论最多的话题. 思路:把用户关于世界杯的帖子拉下来.然后做中文分词+词频统计,最后将统计结果简单做个标签云.效果例如以下: 兴许:中文分词是中文信息处理的基础.分词之后.事 ...

  2. android中怎么将桌面较长的图标名称显示完整

    找到相应的 res 资源, 改动其 styles.xml <style name="WorkspaceIcon.Portrait">         <item ...

  3. Cg入门20:Fragment shader - 片段级模型动态变色(实现汽车动态换漆)

    Unity 一个面片的最大顶点数为65524,所以大于这个数,请拆分成多个面片 1.获取汽车x轴的最大值和最小值[-2.5,2.5]+R watermark/2/text/aHR0cDovL2Jsb2 ...

  4. JavaScript你所不知道的困惑(1)

    困惑一: 先看一个样例: function test(){ message = "hi"; } test(); alert(message); 会输出字符串"hi&quo ...

  5. cp和scp

    1 两个命令的格式一样 cp src dst scp src dst 将src文件拷贝到dst目的地.cp是本机拷贝,即从本机的一个地方拷贝到另外一个地方. 而scp是拷贝到远程及其还是从远程机器拷贝 ...

  6. javax.servlet.http.Part 文件上传

    编辑jsp页面: <html> <head> <base href="<%=basePath%>"> <title>My ...

  7. Java 相关计数问题及其实现

    数(三声)数(四声)问题自然使用非负整数: 0. 一个类作为一个计数器 java 语法 -- final class Counter { private static long counter; pr ...

  8. 使用nginx搭建媒体点播服务器

    使用nginx搭建媒体点播服务器 最新由于兴趣,对ubuntu和安卓上的视频点播直播等应用比较感兴趣,所以在vmware的虚拟机里面搭建了一个视频点播网站,参考了fengzhanhai的文章Nginx ...

  9. source命令用法(转载)

    转自:http://zhidao.baidu.com/link?url=mNfsPHSjTEm7llgyMYx0UVNwkJmD_cxLeHtZnHcM6Ms8LDXofVHka_EzHi6GltbR ...

  10. bzoj 1045: [HAOI2008] 糖果传递【瞎搞】

    感觉我的智商可能不够写题解,就直接截了hzwer的blog 地址http://hzwer.com/2656.html #include<iostream> #include<cstd ...