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,节点标号1-N,接下来每一行第一个数表示i-th连接有几个节点,后面的第一个数是默认方向不用改变,后续的都是需要改变一次方向。
思路:看懂题意后就是一个最短路问题,默认方向权为0,改变为1,四种算法选一种即可,我这里用的是dijkstra,(其他三种在A题中有,这里就不写了),代码如下:
const int maxm = ;
const int INF = 0x7ffffff; int N, A, B, d[maxm], vis[maxm]; struct Edge {
int from, to, dist;
Edge(int _from, int _to, int _dist) : from(_from), to(_to), dist(_dist){};
}; struct Node {
int from, dist;
Node(int _from, int _dist) : from(_from), dist(_dist){}
bool operator<(const Node &a)const {
return a.dist < dist;
}
}; vector<Edge> edges;
vector<int> G[maxm]; void addedge(int u, int v, int dist) {
edges.push_back(Edge(u, v, dist));
G[u].push_back(edges.size() - );
} void init() {
for(int i = ; i <= N; ++i) {
d[i] = INF;
G[i].clear();
}
edges.clear();
memset(vis, , sizeof(vis));
} int main() {
while(scanf("%d%d%d", &N, &A, &B) != EOF) {
init();
for (int i = ; i <= N; ++i) {
int t1, t2;
scanf("%d", &t1);
for(int j = ; j < t1; ++j) {
scanf("%d", &t2);
addedge(i, t2, j == ? : );
}
}
priority_queue<Node> q;
q.push(Node(A, ));
d[A] = ;
while(!q.empty()) {
Node p = q.top();
q.pop();
if(vis[p.from])
continue;
vis[p.from] = ;
int len = G[p.from].size();
for(int i = ; i < len; ++i) {
if(d[edges[G[p.from][i]].to] > d[p.from] + edges[G[p.from][i]].dist) {
d[edges[G[p.from][i]].to] = d[p.from] + edges[G[p.from][i]].dist;
q.push(Node(edges[G[p.from][i]].to, d[edges[G[p.from][i]].to]));
}
}
}
printf("%d\n", d[B] >= INF?-:d[B]);
}
return ;
}
												

Day4 - L - Tram POJ - 1847的更多相关文章

  1. Tram POJ - 1847

    题目链接:https://vjudge.net/problem/POJ-1847 思路:想从A到B使用开关少,想清楚了就是个简单的最短路,可以把不用开开关为权值0, 要开开关为权值1,就是求A到B开开 ...

  2. Tram POJ - 1847 spfa

    #include<iostream> #include<algorithm> #include<queue> #include<cstdio> #inc ...

  3. POJ 1847 Tram (最短路径)

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

  4. 最短路 || POJ 1847 Tram

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

  5. poj 1847 最短路简单题,dijkstra

    1.poj  1847  Tram   最短路 2.总结:用dijkstra做的,算出a到其它各个点要改向的次数.其它应该也可以. 题意: 有点难懂.n个结点,每个点可通向ki个相邻点,默认指向第一个 ...

  6. poj 1847 Tram

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

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

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

  8. POJ 1847 Tram (最短路)

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

  9. poj 1847 Tram【spfa最短路】

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

随机推荐

  1. [转]JSP自定义标签

    原文链接 当jsp的内置标签和jstl标签库内的标签都满足不了需求,这时候就需要开发者自定义标签. 自定义标签 下面我们先来开发一个自定义标签,然后再说它的原理吧! 自定义标签的开发步骤 步骤一 编写 ...

  2. CODE 大全网站整站源码分享(带数据库)

    CODE 大全是一个偏向于 JavaEE.JavaWeb,WEB 前端,HTML5,数据库,系统运维,编程技术开发的纯个人学习.交流性质的技术博客,一个很不错的网站,现在我免费分享给大家.对 java ...

  3. Markdown中实现折叠代码块

    <details> <summary>展开查看</summary> <pre><code> System.out.println(" ...

  4. 理解错误的 Arrays.asList()

    简介 Arrays.asList() 作用是将一个数组转换为一个List 集合. String[] myArray = { "Apple", "Banana", ...

  5. 37 java序列化与反序列化

    一.java序列化与反序列化 1.序列化: 是指把java对象转换为字节序列的过程: 2.反序列化:是指把字节序列恢复为java对象的过程. 二.为什么要序列化 我们知道,当两个进程进行远程通信时,可 ...

  6. UniGui学习之部署(06)只 有Loading...,

    procedure TUniServerModule.UniGUIServerModuleBeforeInit(Sender: TObject);begin Self.ExtRoot:='ext-6. ...

  7. vs code 批量替换

    源内容 .icon-user, .icon-people, .icon-user-female, .icon-user-follow, .icon-user-following, .icon-user ...

  8. APIView 的请求生命周期

    目录 APIView 的请求生命周期 请求解析模块 响应渲染模块 序列化组件 Django 配置 """ 1)应用是否需要在INSTALLED_APPS中注册 在没有使用 ...

  9. Android加载手机磁盘上的资源---decodeFile方法的使用

    一般在写Android程序时,通常会将图片资源放在/res/drawable/文件夹下,读取时,通过R.drawable.imageId即可读取图片内容,但用户在使用时,一般会想要读取存放在存储卡上的 ...

  10. 通过CrawlSpider对招聘网站进行整站爬取(拉勾网实战)

    爬虫首先要明确自己要爬取的网站以及内容 进入拉勾网的网站然后看看想要爬取什么内容职位,薪资,城市,经验要求学历要求,全职或者兼职职位诱惑,职位描述提取公司的名称 以及 在拉勾网的url等等 然后在na ...