POJ-1847 Tram( 最短路 )
题目链接:http://poj.org/problem?id=1847
Description
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
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
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个站点,站点之间有轨道相连,但是站点同时只连向一个站点,要到该站点可以到的其它站点需要使用转换器,问从A到B需要最少使用多少次转换器
解题思路:可以将使用转换器的次数看做两站点的距离,初始化图的时候,该站点直连的站点初始化为0,其它站点初始化为1,然后由于数据量太小,随便一个最短路算法即可
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<cstdio>
#include<queue> using namespace std; const int INF = 0x3f3f3f3f;
int n, a, b;
int dis[][]; int main(){
ios::sync_with_stdio( false ); cin >> n >> a >> b;
memset( dis, INF, sizeof( dis ) ); int k, to;
for( int i = ; i <= n; i++ ){
dis[i][i] = ;
cin >> k;
for( int t = ; t < k; t++ ){
cin >> to;
if( t == ) dis[i][to] = ;
else dis[i][to] = ;
}
} for( int j = ; j <= n; j++ ){
for( int i = ; i <= n; i++ ){
for( int k = ; k <= n; k++ ){
dis[i][k] = min( dis[i][k], dis[i][j] + dis[j][k] );
}
}
} if( dis[a][b] == INF ) cout << -;
else cout << dis[a][b];
cout << endl;
}
POJ-1847 Tram( 最短路 )的更多相关文章
- POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...
- 最短路 || POJ 1847 Tram
POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...
- POJ 1847 Tram (最短路)
Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...
- poj 1847 Tram
http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...
- (简单) POJ 1847 Tram,Dijkstra。
Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...
- [最短路径SPFA] POJ 1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...
- POJ 1847 Tram【Floyd】
题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...
- Floyd_Warshall POJ 1847 Tram
题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...
随机推荐
- PyQt4 在Windows下安装
快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 首先在网上下载sip文件下载完之后解压, 在Windows的开始菜单栏中进入sip的解压目录下: 在目录下面 ...
- QQ第三方登录逻辑(微信,微博等同)
实现过程:生成qq扫码登录连接(需要注册,链接里有几个参数需要按照开发文档的格式进行拼接,要后端完成),点击QQ登录按钮,前端Vue发送axios请求,后端收到请求把生成的QQ登录链接发送给vue,v ...
- sift、surf、orb 特征提取及最优特征点匹配
目录 sift sift特征简介 sift特征提取步骤 surf surf特征简介 surf特征提取步骤 orb orb特征简介 orb特征提取算法 代码实现 特征提取 特征匹配 附录 sift si ...
- 从原理层面掌握@SessionAttribute的使用【一起学Spring MVC】
每篇一句 不是你当上了火影大家就认可你,而是大家都认可你才能当上火影 前言 该注解顾名思义,作用是将Model中的属性同步到session会话当中,方便在下一次请求中使用(比如重定向场景~). 虽然说 ...
- 03、Swagger2和Springmvc整合详细记录(爬坑记录)
时间 内容 备注 2018年6月18日 基本使用 spirngmvc整合swagger2 开始之前这个系列博文基本是,在项目的使用中一些模块的内容记录,但是后期逐渐优化,不单单是整合内容. swagg ...
- 时间格式的字符串在ios中的转换问题
在移动端使用时间选择器的时候,选择了一个时间转换为时间戳,谷歌浏览器以及安卓手机使用 new Date( 选择的时间 ).getTime() 都能够拿到时间戳, 但是在ios手机上会出现出现NAN ...
- exe4j打包--exe转安装包
前面一篇已经详细的说明了打包成exe的步骤了,下面谈谈exe如何压缩成安装文件.这里用到之前的另外一个软件,具体软件看这篇文章 exe4j打包成exe 打开inno 编辑器 打开软件后我们选择 用[脚 ...
- Vue创建项目配置
前言 安装VS Code,开始vue的学习及编程,但是总是遇到各种各样的错误,控制台语法错误,格式错误.一股脑的袭来,感觉创建个项目怎么这个麻烦.这里就讲一下vue的安装及创建. 安装环境 当然第一步 ...
- 如何调教你的博客Episode2——移动端支持和UI美化
这个系列的文章是我在搭建博客园博客时所经历的过程. 在上一期如何调教你的博客Episode1——修改整体样式中,我们通过添加CSS样式,修改了页面的总体布局.但将文章发出之后,博客的布局就出现问题了: ...
- Java并发之内存模型(JMM)浅析
背景 学习Java并发编程,JMM是绕不过的槛.在Java规范里面指出了JMM是一个比较开拓性的尝试,是一种试图定义一个一致的.跨平台的内存模型.JMM的最初目的,就是为了能够支多线程程序设计的,每个 ...