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. iOS开发架构学习记录

    闲着没事看了一些iOS开发架构的视频,简单的介绍了几个常用的架构设计,现将它记录如下,以后有时间再专门写这方面的内容,大家可以看看,感兴趣的就进一步学习. 一.架构基础 1.架构设计的目的 进一步解耦 ...

  2. Lesson 15 Secrecy in industry

    Why is secrecy particularly important in the chemical industries? Two factors weigh heavily against ...

  3. LeetCode633. Sum of Square Numbers(双指针)

    题意:给定一个非负整数c,确定是否存在a和b使得a*a+b*b=c. class Solution { typedef long long LL; public: bool judgeSquareSu ...

  4. Windows程序设计学习笔记(1):一个简单的windows程序

    <Windows程序设计>(第五版)(美Charles Petzold著) #include<windows.h> LRESULT CALLBACK WndProc(HWND, ...

  5. ToString 奇淫技巧

    int和float同样结果 decimal decTemp = 2.1m; Console.WriteLine(decTemp.ToString("#0.00")); //输出2. ...

  6. 《容器化.NET应用架构指南》脑图学习笔记(第一部分)

    一.关于这本官方“圣经” 作为.NET程序员,对于微软官方推动的架构示例总是特别关注,从PetShop到MusicStore再到eShopOnContainers,每一次关注,都会了解到业界最新的架构 ...

  7. Lamda表达式学习笔记二

    Lamda表达式学习笔记二 lamda表达式----方法引用 上一篇讲到Lamda体就是对函数式接口方法的实现 ,在方法体中我们可能会引用其他方法实现逻辑,所以在lamda体中我们可以直接引用器方法 ...

  8. leetcode295 Find Median from Data Stream

    """ Median is the middle value in an ordered integer list. If the size of the list is ...

  9. 三 基于Java动态数组手写队列

    手写队列: package dataStucture2.stackandqueue; import com.lt.datastructure.MaxHeap.Queue; import dataStu ...

  10. QQ企业通知识点---ClassSerializers

    Serializers  串列器 序列化器 串行器 MemoryStream   创建其支持存储区为内存的流. BinaryFormatter   以二进制格式将对象或整个连接对象图形序列化和反序列化 ...