题目链接: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. xamarin studio And linq 查询方式分析

    在 Windows 操作系统可以正常读取网络上的 https 数据流,在 Linux 操作系统中会失败:http://www.cnblogs.com/skyivben/archive/2012/03/ ...

  2. HDU 4679 Terrorist’s destroy

    如果不在最长路的边,那么肯定是w*最长路. 如果在最长路,那么把最长路分成两段,左边树的最长路就是左段+左边点的次短路(不包含最长路上的点的最长路) ,右边同理. 还有就是更新,经过左端点的最长路,不 ...

  3. POJ 1095 Trees Made to Order(卡特兰数列)

    题目链接 中间计算的各种细节.有的细节没处理好,就wa了...主要思路就是根据卡特兰数列的: h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n&g ...

  4. 全面解析Linux数字文件权限

    全面解析Linux数字文件权限 来源:   时间:2013-09-04 20:35:13   阅读数:11433 分享到:0 [导读] 在刚开始接触Linux时对于文件权限的理解并不是很透彻,这里详细 ...

  5. Microsoft.AlphaImageLoader滤镜讲解

    Microsoft.AlphaImageLoader是IE滤镜的一种,其主要作用就是对图片进行透明处理.虽然FireFox和IE7以上的IE浏览器已经支持透明的PNG图片,但是就IE5-IE6而言还是 ...

  6. Scala _ [underscore] magic

    I started learning Scala a few days before. Initially i was annoyed by the use of too many symbols i ...

  7. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  8. php扩展redis,编译安装redis服务

    首先安装redis扩展 https://github.com/phpredis/phpredis 下载http://redis.io/download 服务软件 cd到软件存放目录unzip phpr ...

  9. ThinkPHP公共配置文件与各自项目中配置文件组合的方法

    ThinkPHP公共配置文件与各自项目中配置文件组合的方法 文章TAG:thinkphp 公共配置文件 时间:2014-11-25来源:www.aspku.com 作者:源码库 文章热度: 146 ℃ ...

  10. DataSet key points

    In a typical multiple-tier implementation, the steps for creating and refreshing a DataSet, and in t ...