Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 13658 | Accepted: 5032 |
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
熟悉vector用的练习题
给出N,A,B,表示N个路口,计算从A到B最短距离,下面N行,每行一个数Ki表示第i个路口可以连接到几个其他路口,然后有Ki个数,第一个与它相邻的路口不需要转换,其他的需要转换一次,问最少转换距离,唯一要注意的就是单向边
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define ll long long
#define maxn 110
#define inf 0x3f3f3f3f
#define mes(a, b) memset(a, b, sizeof(a))
using namespace std;
int dis[maxn];
bool vis[maxn];
struct node{
int to, val;
node(int v, int w){
to = v;
val = w;
}
};
vector<node > edge[maxn];
void spfa(int t){
mes(dis, inf);
mes(vis, false);
vis[t] = true;
dis[t] = 0;
queue<int > Q;
Q.push(t);
while(!Q.empty()){
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = 0; i < edge[u].size(); i++){
int to = edge[u][i].to;
int val = edge[u][i].val;
if(dis[to] > dis[u] + val){
dis[to] = dis[u] + val;
if(!vis[to]){
Q.push(to);
vis[to] = true;
}
}
}
}
}
int main(){
int n, a, b, s, e;
scanf("%d%d%d", &n, &a, &b);
for(int i = 1; i <= n; i++){
scanf("%d%d", &s, &e);
edge[i].push_back(node(e, 0));
for(int j = 1; j < s; j++){
scanf("%d", &e);
edge[i].push_back(node(e, 1));
}
}
spfa(a);
if(dis[b] == inf){
printf("-1\n");
}
else{
printf("%d\n", dis[b]);
}
return 0;
}
邻接表版本
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define ll long long
#define maxn 110
#define inf 0x3f3f3f3f
#define mes(a, b) memset(a, b, sizeof(a))
using namespace std;
int dis[maxn], head[maxn];
bool vis[maxn];
int num;
struct node{
int to, val, next;
}edge[maxn*2];
void add(int u, int v, int w){
edge[num].to = v;
edge[num].val = w;
edge[num].next = head[u];
head[u] = num++;
}
void spfa(int t){
mes(dis, inf); mes(vis, false);
vis[t] = true;
dis[t] = 0;
queue<int > Q;
Q.push(t);
while(!Q.empty()){
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to;
int val = edge[i].val;
if(dis[v] > dis[u] + val){
dis[v] = dis[u] + val;
if(!vis[v]){
Q.push(v);
vis[v] = true;
}
}
}
}
}
int main(){
int n, a, b, s, e;
scanf("%d%d%d", &n, &a, &b);
num = 0;
mes(head, -1);
for(int i = 1; i <= n; i++){
scanf("%d%d", &s, &e);
add(i, e, 0);
for(int j = 1; j < s; j++){
scanf("%d", &e);
add(i, e, 1);
}
}
spfa(a);
if(dis[b] == inf){
printf("-1\n");
}
else{
printf("%d\n", dis[b]);
}
return 0;
}