题目链接:http://poj.org/problem?id=1847

Description

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个站点,站点之间有轨道相连,但是站点同时只连向一个站点,要到该站点可以到的其它站点需要使用转换器,问从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( 最短路 )的更多相关文章

  1. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

  2. 最短路 || POJ 1847 Tram

    POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...

  3. POJ 1847 Tram (最短路)

    Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...

  4. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  5. POJ 1847 Tram --set实现最短路SPFA

    题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...

  6. poj 1847 Tram

    http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...

  7. (简单) POJ 1847 Tram,Dijkstra。

    Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...

  8. [最短路径SPFA] POJ 1847 Tram

    Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...

  9. POJ 1847 Tram【Floyd】

    题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...

  10. Floyd_Warshall POJ 1847 Tram

    题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...

随机推荐

  1. 2019.7 佳木斯培训A层

    day1题目及题解 day2题目及题解 day3题目及题解 day4题目及题解 day5题目及题解

  2. 【Java】Caused by: com.ibatis.sqlmap.client.SqlMapException: There is no statement named *** in this SqlMap.

    如题: 可能原因: 在xxx.xml文件中有两个标签的id命名相同: DAO实现类方法中没有写对应xxx.xml的id名称: 实体映射文件xxx.xml未加入到sqlMap-Config.xml文件中 ...

  3. ansible-service

    #service#查询服务状态 ansible server01 -m service -a "name=httpd state=started" #停止服务 ansible se ...

  4. Java集合系列(三):HashSet、LinkedHashSet、TreeSet的使用方法及区别

    本篇博客主要讲解Set接口的三个实现类HashSet.LinkedHashSet.TreeSet的使用方法以及三者之间的区别. 注意:本文中代码使用的JDK版本为1.8.0_191 1. HashSe ...

  5. 【原创】HashMap复习精讲

    引言 由于近期忙着搬家,又偷懒了几个礼拜! 其实我很早以前就想写一篇关于HashMap的面试专题.对于JAVA求职者来说,HashMap可谓是集合类的重中之重,甚至你在复习的时候,其他集合类都不用看, ...

  6. SonarQube+Jenkins安装工程中遇到的吭

    1. SonarQube是不是有点飘了,居然要java11+才能运行 解决方案: 重新下载老版本 也不知道哪个版本才好用,就下载了7.0 和6.6,这两个版本用jdk1.8就可以用 2. 配置数据库u ...

  7. 佳木斯集训Day5

    今天是ACM赛制...本来可以400的,结果毒瘤T2模拟硬生生卡掉了我90分 T1是个大水题,找规律,5分钟AC没啥压力 #include <bits/stdc++.h> #define ...

  8. SpringBoot第二天

    一,SpringBoot 整合 jsp 技术 1,创建项目 2,修改 pom 文件,添加坐标 <project xmlns="http://maven.apache.org/POM/4 ...

  9. kpm字符串匹配算法

    首先是简单的朴素匹配算法 /* * 返回子串t在主串s的位置,若不存在则返回0 */ public static int index(String s, String t) { int i = 0;/ ...

  10. 【java提高】(17)---Java 位运算符

    Java 位运算符 &.|.^.~.<<.>> 以前学过有关java的运算符,不过开发了这么久也很少用过这个.现在由于开发需要,所以现在再来回顾整理下有关java的运算 ...