题目链接:http://poj.org/problem?id=1847

题意:给了N个交叉口,每个交叉口有自己能转到的交叉口。

注意这里:First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection.

即每一行的第二个数字代表该交叉口默认的通向,是不需要手动转的,剩下的交叉口想去的话都需要手动转一次。

现在想要从A口走到B口,走的路程想要转的次数时最少的,问最少转的值。

建图求最短路即可

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <string>
typedef long long LL;
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
#define N 515 using namespace std; int G[N][N], n; void Floyd()
{
for(int k=; k<=n; k++)
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
G[i][j] = min(G[i][j], G[i][k]+G[k][j]);
}
} int main()
{
int a, b, num, k;
while(scanf("%d %d %d", &n, &a, &b) != EOF)
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
G[i][j] = INF;
G[i][i] = ;
} for(int i=; i<=n; i++)
{
scanf("%d", &k);
for(int j=; j<=k; j++)
{
scanf("%d", &num);
if(j == ) G[i][num] = ;
else G[i][num] = ;
}
}
Floyd();
if(G[a][b] == INF)puts("-1");
else printf("%d\n", G[a][b]);
}
return ;
}

Tram---poj1847(简单最短路)的更多相关文章

  1. N - Tram - poj1847(简单最短路)

    题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是 默认的是 第一个指向的方向,所以如果要选择别的方向的话得 进行一次切换操作 ,给定一个起点一个 ...

  2. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  3. hdu2112(HDU Today 简单最短路)

    Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD ...

  4. Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)

    problem Bessie is out in the field and wants to get back to the barn to get as much sleep as possibl ...

  5. POJ 1847 Tram --set实现最短路SPFA

    题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...

  6. hdu2722 简单最短路,处理好输入就行

    题意:       从左上角走到右下角,有的最短时间,每段路径的长度都是2520,每段上都有自己的限制速度,方向. 思路:      直接写就行了,就是个最短路,权值是2520/限制,输入的时候细心点 ...

  7. HDU 2544(简单最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=2544 /* 使用pair代替结构 */ #include <iostream> #include & ...

  8. HDU 1874(简单最短路) (大优化)

    优先队列那里用greater会报错 http://acm.hdu.edu.cn/showproblem.php?pid=1874 /* 使用pair代替结构 */ #include <iostr ...

  9. LightOJ 1321 - Sending Packets 简单最短路+期望

    http://www.lightoj.com/volume_showproblem.php?problem=1321 题意:每条边都有概率无法经过,但可以重新尝试,现给出成功率,传输次数和传输时间,求 ...

随机推荐

  1. Origami

    Origami 是一个来自 Facebook 设计团队的作品,是 Quartz Composer 的免费工具包,可在无需编程的情况下轻松实现与设计原型进行交互.

  2. Google Code Jam 2010 Round 1A Problem A. Rotate

    https://code.google.com/codejam/contest/544101/dashboard#s=p0     Problem In the exciting game of Jo ...

  3. Java正则表达式教程

    地址:http://www.java3z.com/cwbwebhome/article/article8/Regex/Java.Regex.Tutorial.html#reg0_1

  4. 1.Java为什么能跨平台运行?请简述原理。

    使用不同操作系统的JVM(JAVA虚拟机)解释运行编译好的字节码文件(.class)

  5. DotNetBar RibbonControl 控件动态添加项

    想做个插件式开发,界面用Dotnetbar的RibbonControl,需要通过代码动态的向RibbonControl控件添加项 示例代码如下: RibbonTabItem rti = new Rib ...

  6. 矢量图标转成16*16大小的SVG格式

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:一一链接:http://www.zhihu.com/question/32233782/answer/68629385来源:知 ...

  7. react-native 学习

    官网 https://facebook.github.io/react-native/docs/getting-started.html#content 下载 npm install -g react ...

  8. 数组机、局域网ip查找

    cmd ipconfig 以太网适配器 VMware Network Adapter VMnet8: IPv4 地址 . . . . . . . . . . . . : 192.168.233.1

  9. JSP编程-步步为营

    [第一个JSP举例] header.jsp <%@ page language="java" contentType="text/html; charset=utf ...

  10. 【转】const和static readonly

    我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声 ...