Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11606 | Accepted: 4241 |
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.
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.
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;
}