Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 8743 | Accepted: 3184 |
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
这题只要读懂题用flody就可以了
输入描述:给你n个路口数,以及你的起点和终点
然后是n行
每一行的第一个数是该路口可以到达的路口数k 然后是k个路口,在这k个路口中第一个路口是可以直达的也就是不用按按钮的,要想到达别的路口就得按按钮了
思路:K个路口的第一个路口记为0,其他的记为1,然后直接用flody的行了。
#include<stdio.h> #include<string.h> int dis[105][105]; #define INF 0x3f3f3f3f int Min(int a,int b) { return a<b?a:b; } int main() { int n,m,a,b,i,j,k,x,y; while(scanf("%d %d %d",&n,&a,&b)!=EOF) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) dis[i][j]=(i==j)?0:INF; for(i=1;i<=n;i++) { scanf("%d",&m); for(j=1;j<=1;j++) { scanf("%d",&x); dis[i][x]=0; } for(j=2;j<=m;j++) { scanf("%d",&x); dis[i][x]=1; } } for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) dis[i][j]=Min(dis[i][j],dis[i][k]+dis[k][j]); if(dis[a][b]<INF) printf("%d ",dis[a][b]); else printf("-1 "); } return 0; }