POJ1847--Tram 【最短路spfa】
2015-07-24 21:48
363 查看
Tram
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
Sample Output
有N个点 接下来的N行分别为点i的情况:第一个数字k表示与该点连通的点的个数,接下来输入k个数,表示与点i相连的点的编号,第一个所连的点为不用动改而直接通过,权值设为0,其余的点通过的话要改一次,权值设为1,求从A点到B点改扳手的最小次数。
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11606 | Accepted: 4241 |
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个点 接下来的N行分别为点i的情况:第一个数字k表示与该点连通的点的个数,接下来输入k个数,表示与点i相连的点的编号,第一个所连的点为不用动改而直接通过,权值设为0,其余的点通过的话要改一次,权值设为1,求从A点到B点改扳手的最小次数。
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> #define INF 0x3f3f3f3f #define maxn 100100 using namespace std; int n, st, ed; int head[maxn], cnt; bool vis[maxn]; int dist[maxn]; struct node { int u, v, w, next; }; node edge[maxn]; void init (){ cnt = 0; memset(head, -1 ,sizeof(head)); } void add(int u, int v, int w){ edge[cnt] = {u, v, w, head[u]}; head[u] = cnt++; } void SPFA(){ for(int i = 1; i <=n; ++i){ vis[i] = 0; dist[i] = INF; } dist[st] = 0; queue<int>q; q.push(st); vis[st] = 1; while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0; for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].v; int w = edge[i].w; if(dist[v] > dist[u] + w){ dist[v] =dist[u] + w; if(!vis[v]){ vis[v] = 1; q.push(v); } } } } if(dist[ed] != INF) printf("%d\n", dist[ed]); else printf("-1\n"); } int main(){ while(scanf("%d%d%d", &n, &st, &ed)!=EOF){ init(); for(int i = 1 ; i <= n; ++i){ int m; scanf("%d", &m); int k; for(int j = 0; j < m; ++j){ scanf("%d", &k); if(j == 0) add(i, k, 0); else add(i, k, 1); } } SPFA(); } return 0; }
相关文章推荐
- 常用函数/功能块 记录
- hdu3729 I'm Telling the Truth(二分图最大匹配)
- 杭电 1201 18岁生日
- 3.1 linux进程 2015/7/22
- JavaScript弹出框
- 定时访问网站
- 单元测试覆盖率生成网页报告----(一)
- poj 2828 Buy Tickets
- C语言的inline
- poj1611 - The Suspects
- .net版本之间的关系
- 黑马程序员--集合框架
- Dom4j使用
- win8、win10下卸载程序报错误2502、2503的解决办法
- Android Api Demos登顶之路(三)
- 拍摄美食教程
- 数据库设计原则(摘抄)
- poj3270--Cow Sorting(置换群)
- js函数在frame中的相互调用详解
- Ognl表达式 值栈对象ValueStack