POJ 1847 Tram

来源:互联网 发布:皇甫圣华淘宝 编辑:程序博客网 时间:2024/06/06 03:00
Tram
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 14701 Accepted: 5420

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

3 2 12 2 32 3 12 1 2

Sample Output

0
题解:首先吐槽下,你说说就一个题,至于弄那么长的英文吗?欺负我英文不好是不?大哭

题意大概就是一个交通网……给你N  A  B三个数,N表示站点的数目,A为源点B为终点。接下来N行分别表示地i个车站的情况,每行行首有一个数K,K表示第i个车站通往其他车站的数目,接下来K个数,表示车站的编号,第一个数便是和i车站直达开关次数为零,其他为1……

#include<stdio.h>#include<string>#include<iostream>#include<algorithm>#include<string.h>#include<stdlib.h>#include<time.h>#include<math.h>#include<queue>;#include<stack>;#define INF 0x3f3f3f3fusing namespace std;int d[110],w[110][110];bool v[110];int n,a,b;void dijkstra(){    for(int i=1;i<=n;i++)        d[i]=w[a][i];              /*d[i]保存起点到i点的距离*/    d[a]=0;v[a]=true;    for(int i=1;i<=n;i++)    {        int x,m=INF;        for(int j=1;j<=n;j++)     /*找寻最小权值*/        {            if(!v[j]&&d[j]<=m)                m=d[x=j];        }        v[x]=true;                  /*标记,已被访问过*/        for(int j=1;j<=n;j++)         /*进行松弛操作,更新权值*/            if(!v[j]&&d[x]+w[x][j]<d[j])            d[j]=d[x]+w[x][j];    }    if(d[b]==INF)printf("-1\n");    else printf("%d\n",d[b]);}int main(){    while(cin>>n>>a>>b)    {        memset(v,false,sizeof(v));        memset(d,INF,sizeof(d));        memset(w,INF,sizeof(w));        for(int i=1;i<=n;i++)        {            int t,aa;            cin>>t;            for(int j=1;j<=t;j++)            {                cin>>aa;                w[i][aa]=j==1?0:1;            }        }        dijkstra();    }}



0
1 0