Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 14401 | Accepted: 5319 |
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
Source
Croatia OI 2002 Regional - Juniors
http://poj.org/problem?id=1847
此题明显的卡题面 输入三个数N,A,B N代表有几个点 A代表起点 B终点
下面每行 第一个数表示 有几条路 后面几个数代表的是终点 起点是(行数-1) (行数-1)到第二个数路径是0 其他都是1
最后得到的路是
1 2 0
2 1 1
1 3 1
3 1 0
2 3 0
3 2 1
#include<stdio.h>
#include<string.h>
using namespace std;
#define inf 0x3f3f3f3f
int ap[110][110];
int dis[110],vis[110];
int main()
{
int n,m,t,i,j,kk,tt,MIN,a,u,b;
while(scanf("%d%d%d",&t,&n,&m)!=EOF)
{
for(i=1; i<=t; i++)
for(j=1; j<=t; j++)
ap[i][j]=inf;
for(i=1;i<=t;i++)
{
scanf("%d",&a);
for(j=0;j<a;j++)
{
scanf("%d",&b);
if(j==0)
ap[i][b]=0;
else ap[i][b]=1;
}
}
memset(vis,0,sizeof(vis));
for(i=1; i<=t; i++)
dis[i]=ap[n][i];
vis[n]=1;
dis[n]=0;
for(i=1; i<t; i++)
{
MIN=inf;
u=-1; //没赋初值被POJ WA了
for(j=1; j<=t; j++)
if(!vis[j]&&dis[j]<MIN)
{
MIN=dis[j];
u=j;
}
if(u==-1) break;//牢记
vis[u]=1;
for(j=1; j<=t; j++)
{
if(!vis[j]&&dis[j]>dis[u]+ap[u][j]&&ap[u][j]<inf)
dis[j]=dis[u]+ap[u][j];
}
}
if(dis[m]<inf) printf("%d\n",dis[m]);
else printf("-1\n");
}
return 0;
}