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 ...
随机推荐
- 【iOS】UILabel 常用属性设置
UILabel 的一些常用属性,示例代码如下: // 字体大小 label.font = [UIFont systemFontOfSize:14.0]; label.font = [UIFont fo ...
- codeforces 327 B. Hungry Sequence
题目链接 题目就是让你输出n个数的序列,要保证该序列是递增的,并且第i个数的前面不能保护它的约数,我直接先对前100000的素数打表,然后输出前n个,so easy. //cf 191 B #incl ...
- HC-08 BLE资料
1.1 特点简介 HC-08蓝牙串口通信模块是新一代的基于Bluetooth Specification V4.0 BLE蓝牙协议的数传模块.无线工作频段为 2.4GHz ISM,调制方式是 GFSK ...
- MySQL5.7运行CPU达百分之400处理方案
用户在使用 MySQL 实例时,会遇到 CPU 使用率过高甚至达到 100% 的情况.本文将介绍造成该状况的常见原因以及解决方法,并通过 CPU 使用率为 100% 的典型场景,来分析引起该状况的原因 ...
- HackBar收费版绕过
一段时间没用HackBar,近期做渗透,打开火狐浏览器,按F12键调出HackBar,发现居然需要收费买license才能使用. 经过研究,整理了以下两个绕过HackBar收费版的方法. 第一种:用其 ...
- 02、Java的lambda表达式和JavaScript的箭头函数
前言 在JDK8和ES6的语言发展中,在Java的lambda表达式和JavaScript的箭头函数这两者有着千丝万缕的联系:本次试图通过这篇文章弄懂上面的两个"语法糖". 简介 ...
- SQL Labs刷题补坑记录(less31-less53)
LESS31: 双引号直接报错,那么肯定可以报错注入,并且也过滤了一些东西,^异或没有过滤,异或真香 -1" and (if(length(database())=8,1,0)) and & ...
- Samba:基于公网 IP 的服务访问
写在前面的话 由于使用过程中,发现如果 Samba 只用于内网访问,同事在外面甚至其它不是一个网段的同事就无法访问了.这显然不符合我们最终的需求,最后没法,只能把访问部署到云服务器上面去,此时问题来了 ...
- 喜大普奔 | 微信小程序支持PC端打开了
微信小程序可以在PC端打开啦 微信PC版发布了v2.7.0测试版,其中一个重磅的功能就是:支持打开聊天中分享的小程序 咖啡君这么喜欢尝鲜的人自然是在第一时间下载进行了体验 安装成功,会有功能更新说明 ...
- CSS3: perspective 3D属性
本文引自:http://blog.csdn.net/cddcj/article/details/52956540 perspective 属性指定了观察者与z=0平面的距离,使具有三维位置变换的元素产 ...