题目:
Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 14423 | Accepted: 5332 |
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
样例注释:
3 2 1 //有3个开关点,计算从第二个到第一个最少需要旋转几次
2 2 3 //第1个开关可以通向2个开关,通向2不需要旋转,通向3需要旋转1次
2 3 1 //第2个开关可以通向2个开关, 通向3不需要旋转,通向1需要旋转1次
2 1 2 //第3个开关可以通向2个开关, 通向1不需要旋转,通向2需要旋转1次
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
typedef pair<int,int> p;//<a,b> 起点到b的最短距离为a
struct edge{
int to;
int cost;
edge(int tto=0,int ccost=0):to(tto),cost(ccost){}
};
int n,m,start,end,d[maxn];
vector<edge> g[maxn];
void dijkstra(int s){
priority_queue<p,vector<p>,greater<p> >qu;//堆按照p的first(最短距离)排序,小在前
fill(d,d+maxn,inf);
d[s]=0;
qu.push(p(0,s));//从起点出发到顶点s的最短距离为0
while(!qu.empty()){
p temp=qu.top();
qu.pop();
if(temp.first>d[temp.second]) continue;//跳过更新过程中入队的非最小值
for(int i=0;i<g[temp.second].size();++i){//遍历该顶点连出的每条边
edge e=g[temp.second][i];
if(d[e.to]>d[temp.second]+e.cost){
d[e.to]=d[temp.second]+e.cost;
qu.push(p(d[e.to],e.to));
}
}
}
}
int main(){
int t,a;
scanf("%d%d%d",&n,&start,&end);
for(int i=1;i<=n;++i){//顶点编号从1到N
scanf("%d",&t);
if(!t) continue;
scanf("%d",&a);
g[i].push_back(edge(a,0));//从i到a权值为零
for(int j=1;j<t;++j){
scanf("%d",&a);
g[i].push_back(edge(a,1));//权值为 1
}
}
dijkstra(start);
if(d[end]!=inf) printf("%d\n",d[end]);
else puts("-1");
return 0;
}