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. Linux进程内消息总线设计

    文章目录 Windows平台进程内消息总线 如果没有消息总线,会产生什么问题 死循环包含关系 高耦合.低内聚 消息总线 结构图 原理 生产者与总线的关系 总线与消费者的关系 Linux进程内消息总线设 ...

  2. Mysql 分组查询出现'this is incompatible with sql_mode=only_full_group_by'的解决办法

    由于Mysql自动开启了 only_full_group_by,所以若查询的字段不在group by里面,则分组报错. 解决办法其一:mysql配置,关闭only_full_group_by,这种办法 ...

  3. 关于java自学的内容以及感受

    这周主要学习了关于数组方面的知识包括一维数组以及多维数组(他们所储存数据默认值为0),以下为我根据相关知识编写的简单程序: public class test { public static void ...

  4. ANSYS-MFC生成APDL

    目录 1. 简介 2. APDL生成 3. 调用ANSYS批处理 1. 简介 对于ANSYS-MFC二次开发,两者之间的关系非常明确,从界面中读取参数并转换成APDL语言,然后调用批处理操作. 对于简 ...

  5. 【PAT甲级】1029 Median (25 分)

    题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...

  6. word无法切换中文输入法的解决方法

    问题: 在word编辑文字的时候,莫名其妙地出现只能输入英文字母无法输入中文的现象,输入法状态条也不显示,而输入法是正常的(在其他编辑器中可正常输入汉字) 解决方法如下:word 2003:菜单工具- ...

  7. Gamma 矫正

    参考如下链接: https://www.zhihu.com/question/27467127

  8. Hot Module Replacement [热模块替换]

    安装了webpack-dev-server后 , 配置 "start": "webpack-dev-server" 然后运行 npm start 会开起一个we ...

  9. activiti 全局流程监听ActivitiEventListener,实现监听不同类型事件,不需要在acitivit中配置任务监听,非常方便

    如果我们像给任务配置监听,按照常规的做法是这样的 一个个配置,比较麻烦. 现在利用ActivitiEventListener,监听全局事件,并且可以判断不同的事件类型,进而执行不同的业务逻辑. 1.定 ...

  10. 由Nginx反向代理引出的JCaptcha验证码验证失败的问题

    搜索关键字: 1)Windows本地开发正常,部署到Linux远程服务器上JCaptcha验证失败 2)Linux远程服务器上JCpatcha验证失败 3)Nginx反向代理后JCaptcha验证失败 ...