POJ1847 Tram

                                                                                             Tram
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7102   Accepted: 2570

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 1
2 2 3
2 3 1
2 1 2

Sample Output

0

Source

 
 
思路:有一系列的点,题目首先给出N,A,B。N--点的总数,A--开始点,B--终点。然后接着N行,第I行的第1个数表示,与点I相连的点的数目X,接着有X个点,为与这个点相连的点。从点I到这个清单中的第一个点是不需要转弯的,到其他点需要转一次弯。最后求从点A到点B所转的弯的最小次数。
        如果点X到点Y相连且不要转弯,则点X到点Y之间的权重为0,如果点X与点Y相连且需要转弯,则点X到点Y之间的权重为1。最后构成了一个有向图。利用DIJKSTRA即可求解。
 
 
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cmath>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <map>
  8 #define MAXINT 99999999
  9 
 10 #define MININT -1
 11 
 12 using namespace std;
 13 
 14 
 15 
 16 int data[1000+1][1000+1];
 17 int vis[1000+1];
 18 int lowcost[1000+1];
 19 
 20 
 21 
 22 
 23 int main()
 24 {
 25     
 26            
 27            int n,a,b;
 28            int i,j,k;
 29            scanf("%d%d%d",&n,&a,&b);
 30            
 31            for(i=1;i<=n;i++)
 32            for(j=1;j<=n;j++)
 33            data[i][j]=MAXINT;
 34            
 35            
 36            
 37            for(i=1;i<=n;i++)
 38            {
 39                             int m;
 40                             scanf("%d",&m);
 41                             for(j=1;j<=m;j++)
 42                             {
 43                                              int d;
 44                                              scanf("%d",&d);
 45                                              if(j==1)
 46                                              {data[i][d]=0;}
 47                                              else
 48                                              data[i][d]=1;
 49                             }
 50                             
 51            }
 52            
 53            if(a==b)
 54            {printf("0\n");return 0;}
 55            
 56            
 57            for(i=1;i<=n;i++)
 58            {vis[i]=0;lowcost[i]=data[a][i];}
 59            
 60            vis[a]=1;
 61            
 62            int tag=0;
 63            
 64            int sum=0;
 65            
 66            for(i=1;i<n;i++)
 67            {
 68                            int mincost=MAXINT;
 69                            int k=-1;
 70                            for(j=1;j<=n;j++)
 71                            {
 72                                             if((vis[j]==0)&&(lowcost[j]<mincost))
 73                                             {k=j;mincost=lowcost[j];}
 74                            }
 75                            
 76                            
 77                            if(k==-1)
 78                            {tag=1;
 79                             break;
 80                            }
 81                            else
 82                            {
 83                                vis[k]=1;
 84                                if(k==b)
 85                                break;
 86                            }
 87                            
 88                            for(j=1;j<=n;j++)
 89                            {
 90                                             if((vis[j]==0)&&(lowcost[j]>lowcost[k]+data[k][j]))
 91                                             lowcost[j]=lowcost[k]+data[k][j];
 92                            }
 93            }
 94            
 95            if(tag==1)
 96            {printf("-1\n");}
 97            else
 98            {
 99                printf("%d\n",lowcost[b]);
100            }
101            
102            
103                             
104     
105     //system("PAUSE");
106     
107     return 0;
108 }

 

posted @ 2012-08-07 21:24  cseriscser  阅读(345)  评论(0编辑  收藏  举报