描述:

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. MongoDB 4.0.* 远程连接及用户名密码认证登陆配置——windows

  2. c提高第四课

    1.一维数组的初始化 , , }; //3个元素 ] = { , , }; //a[3], a[4]自动初始化为0 ] = { }; //全部元素初始化为0 memset(c, , sizeof(c) ...

  3. MonkeyRunner API简介

    MonkeyRunner工具主要有三个类: MonkeyRunner MonkeyDevice MonkeyImage 官方API文档 :http://www.android-doc.com/tool ...

  4. Microsoft Connect 2018 Summary

    https://www.microsoft.com/en-us/connectevent/

  5. codeforces476D

    Dreamoon and Sets CodeForces - 476D Dreamoon likes to play with sets, integers and .  is defined as ...

  6. Codeforces1153F Serval and Bonus Problem 【组合数】

    题目分析: 我们思考正好被k个区间覆盖的情况,那么当前这个子段是不是把所有的点分成了两个部分,那么在两个部分之间相互连k条线,再对于剩下的分别连线就很好了?这个东西不难用组合数写出来. 然后我们要证明 ...

  7. qt 视频播放器错误解决方法

    DirectShowPlayerService::doRender: Unresolved error code 0x80040266 () 当你发布的qmlproject包含QtMultimedia ...

  8. ansible-playbook用法

    一.playbook用法 1.playbook的执行文件为YAML语言编写,所以文件名为xxx.yml.YAML语法可以参考https://docs.ansible.com/ansible/lates ...

  9. 退役之战- SDOI

    嘻嘻, 从文化课中逃脱出来, 很痛苦啊, 英语已经近半年没学了,语文水平水的一批,在其他班里受虐待. 百废待兴. 因为曾经学了一段时间的省选,所以被老师拉回来送人头考试啦. 听说4.5 SDOI一轮哎 ...

  10. thinkphp5: 循环输出表格,并固定表格单元宽度(过长省略号)

    html: <table class="table table-striped" style='table-layout:fixed;'> <thead clas ...