描述:

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
 
 

题意:

  有n个点,并且给了起点和终点ab,接下来的n行的第一个数k表示与第i个点相连的点数,每一行接下来有k个数,表示与i点相连接的点,并且在这k个点中,第一个点可直接到达,及路径长度为0,其他的点需要改一次扳手,路径长度为1,用dijkstra即可解决。

代码:

#include <iostream>
#include <stdio.h> using namespace std;
#define inf 100100100
bool vis[];
int n,a,b;
int map[][];
int d[]; void dijkstra()
{
int i,j,v,f;
for(i=;i<=n;i++)
{
vis[i]=;
d[i]=map[a][i];
}
vis[a]=;
d[a]=;
for(i=;i<n;i++)
{
f=inf;v=a;
for(j=;j<=n;j++)
{
if(d[j]<inf&&!vis[j]&&d[j]<f)
{
f=d[j];
v=j;
}
}
if(f>inf) break;
vis[v]=;
for(j=;j<=n;j++)
if(!vis[j]&&map[v][j]<inf&&d[v]+map[v][j]<d[j])
d[j]=d[v]+map[v][j];
}
} int main()
{
int k,m;
scanf("%d%d%d",&n,&a,&b);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(i==j) map[i][j]=;
else map[i][j]=inf;
}
for(int i=;i<=n;i++)
{
scanf("%d",&k);
for(int j=;j<k;j++)
{
scanf("%d",&m);
if(j==) map[i][m]=;
else map[i][m]=;
}
}
dijkstra();
if(d[b]>=inf) cout<<"-1"<<endl;
else
cout<<d[b]<<endl;
return ;
}
 

poj1847 Tram(最短路dijkstra)的更多相关文章

  1. poj1847 Tram 最短路Dijkstra

    题目链接:http://poj.org/problem?id=1847 Dijkstra算法的模版应用 题意:给你N个点和起点终点,点与点有铁路,接下来的N行分别为点i的情况 第一个数字表示与该点连通 ...

  2. POJ-1847 Tram( 最短路 )

    题目链接:http://poj.org/problem?id=1847 Description Tram network in Zagreb consists of a number of inter ...

  3. hdu 2544 最短路 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...

  4. 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法

    图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...

  5. 单源最短路dijkstra算法&&优化史

    一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+ML ...

  6. HUD.2544 最短路 (Dijkstra)

    HUD.2544 最短路 (Dijkstra) 题意分析 1表示起点,n表示起点(或者颠倒过来也可以) 建立无向图 从n或者1跑dij即可. 代码总览 #include <bits/stdc++ ...

  7. 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)

    layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...

  8. 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

  9. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  10. 最短路Dijkstra算法的一些扩展问题

    最短路Dijkstra算法的一些扩展问题     很早以前写过关于A*求k短路的文章,那时候还不明白为什么还可以把所有点重复的放入堆中,只知道那样求出来的就是对的.知其然不知其所以然是件容易引发伤痛的 ...

随机推荐

  1. Django(二)路由系统、视图、模板

    大纲 一.内容概要: 二.上节回顾 三.Django 视图–views  1.获取用户多个数据及文件上传  2.FBV 和 CBV  3.装饰器 四.Django模板补充  - Django模板语言循 ...

  2. java 基本数据类型初始值(默认值)

    1.int类型定义的数组,初始化默认是0 2.String类型定义的数组,默认值是null 3.char类型定义的数组,默认值是0对应的字符 4.double类型定义的数组,默认值是0.0 5.flo ...

  3. bzoj1444[Jsoi2009]有趣的游戏[AC自动机]

    题面 bzoj 我要向师父学习善待每一只数据结构 考虑成环,那么高斯消元 然鹅这道题太小了 所以直接转移矩阵自乘就好啦 终点不向外连边 有一条向自己的,概率为一的自环来作为结尾 对于其他店 若有边\( ...

  4. 如何用Electron Js创建第一个应用Hello World

    什么是Electron Node.js和Chromium的结合品.允许只使用HTML,CSS和JavaScript来开发跨平台桌面应用. 编写第一个Electron程序(Hello World) 在开 ...

  5. 「NOI2013」树的计数 解题报告

    「NOI2013」树的计数 这什么神题 考虑对bfs重新编号为1,2,3...n,然后重新搞一下dfs序 设dfs序为\(dfn_i\),dfs序第\(i\)位对应的节点为\(pos_i\) 一个暴力 ...

  6. 小白月赛13 小A的柱状图 (单调栈)

    链接:https://ac.nowcoder.com/acm/contest/549/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  7. 为Jupyter只安装目录的扩展包

    项目地址:https://github.com/minrk/ipython_extensions/tree/master/nbextensions 一般都是让安装Nbextensions,而这些扩展我 ...

  8. Hadoop-2.7.3-src 源码编译

    Hadoop-2.7.3 编译 1.需要环境梳理 BUILDING JDK1.7+ maven 3.0 or later findbugs 1.3.9 protocolBuffer 2.5.0 cma ...

  9. 081、Weave Scope 多主机监控(2019-04-29 周一)

    参考https://www.cnblogs.com/CloudMan6/p/7674011.html   Weave Scope 除了监控容器,还可以监控Docker Host.   点击顶部 HOS ...

  10. XML配置spring session jdbc实现session共享

    概述 session的基础知识就不再多说. 通常,我们会把一个项目部署到多个tomcat上,通过nginx进行负载均衡,提高系统的并发性.此时,就会存在一个问题.假如用户第一次访问tomcat1,并登 ...