题目链接:http://poj.org/problem?id=1847
Description
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
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
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 题目大意:有N个站点,站点之间有轨道相连,但是站点同时只连向一个站点,要到该站点可以到的其它站点需要使用转换器,问从A到B需要最少使用多少次转换器
解题思路:可以将使用转换器的次数看做两站点的距离,初始化图的时候,该站点直连的站点初始化为0,其它站点初始化为1,然后由于数据量太小,随便一个最短路算法即可
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<cstdio>
#include<queue> using namespace std; const int INF = 0x3f3f3f3f;
int n, a, b;
int dis[][]; int main(){
ios::sync_with_stdio( false ); cin >> n >> a >> b;
memset( dis, INF, sizeof( dis ) ); int k, to;
for( int i = ; i <= n; i++ ){
dis[i][i] = ;
cin >> k;
for( int t = ; t < k; t++ ){
cin >> to;
if( t == ) dis[i][to] = ;
else dis[i][to] = ;
}
} for( int j = ; j <= n; j++ ){
for( int i = ; i <= n; i++ ){
for( int k = ; k <= n; k++ ){
dis[i][k] = min( dis[i][k], dis[i][j] + dis[j][k] );
}
}
} if( dis[a][b] == INF ) cout << -;
else cout << dis[a][b];
cout << endl;
}