描述:

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. [Cordova 之 入门篇]

    1. cordova是什么 Apache Cordova是一个开源的移动开发框架.允许你用标准的web技术-HTML5,CSS3和JavaScript做跨平台开发. 2. 为什么用cordova 基于 ...

  2. Spring Boot(二):数据库操作

    本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...

  3. Ansible小记

    参考网址: https://www.iyunv.com/thread-385359-1-1.html http://blog.51cto.com/215687833/1886305

  4. react 入坑笔记(六) - 组件的生命周期

    React 组件生命周期 详细参考: react 组件生命周期 组件的生命周期可分为三个状态: 1.Mounting:已经挂载/插入到真实 DOM 树上: 2.Updating:正在被重新渲染: 3. ...

  5. loj121-动态图连通性

    Solution 线段树分治, 然后直接在线段树上dfs, 在进入/回溯的过程中维护并查集的merge/split. 对于split操作, 可以在merge时按秩合并, 然后利用栈记录, split时 ...

  6. Apache Flink教程

    1.Apache Flink 教程 http://mp.weixin.qq.com/mp/homepage?__biz=MzIxMTE0ODU5NQ==&hid=5&sn=ff5718 ...

  7. Neovim中NERDTree等多处cursorline不高亮

    标题表达的不是很清楚,看下图把 解决方法 添加下面内容到init.vim " 针对NERDTree " https://github.com/scrooloose/nerdtree ...

  8. 从redis中取值如果不存在设置值,使用Redisson分布式锁【我】

    用到的jar包: <!-- Redis客户端 --> <dependency> <groupId>redis.clients</groupId> < ...

  9. 四、Tensorflow的分布式训练

    TensorFlow中的集群(cluster)指的是一系列能够针对图(Graph)进行分布式计算任务(task).每个任务是同服务(server)相关联的.TensorFlow中的服务会包含一个用于创 ...

  10. 2018-2019-2 网络对抗技术 20165232 Exp3 免杀原理与实践

    2018-2019-2 网络对抗技术 20165232 Exp3 免杀原理与实践 免杀原理及基础问题回答 一.免杀原理 一般是对恶意软件做处理,让它不被杀毒软件所检测.也是渗透测试中需要使用到的技术. ...