acmPOJ--1847

来源:互联网 发布:linux查看内存占用前10 编辑:程序博客网 时间:2024/04/28 11:13

Problem 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”.

题意:
火车要从A路口到B路口(可能A直接到B,也可能有中间结点),各个路口又分别连接其他路口,其中有一个方向是默认的不需要增加操作(即权为0),其他的则是需要一个转向操作(即权为1),求火车从A到B最少要几个操作(最短路径)

思路:
Floyd算法,根据给出的起始点算出到其余各点的最短路径,最后求出A到B的最短路径。(输入时各路口给出的方向序列,默认第一个权为0,其余权为1)

import java.util.Scanner;public class MainC1002 {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int N,A,B;        int K,D;        int i,j;        N = scanner.nextInt();        A = scanner.nextInt();        B = scanner.nextInt();        int[][] graph = new int[N][N];        for(i=0;i<N;i++){            for(j=0;j<N;j++){                graph[i][j] = 9999999;                if(i==j){                    graph[i][i] = 0;                }            }        }        for(i=0;i<N;i++){            K = scanner.nextInt();            int flag = 0;            for(j=0;j<K;j++){                D = scanner.nextInt();                if(flag == 0){                    graph[i][D-1] = 0;                    flag = 1;                }else{                    graph[i][D-1] = 1;                }            }        }        for(i=0;i<N;i++){            for(j=0;j<N;j++){                for(int k=0;k<N;k++){                    if(graph[j][k]>graph[j][i]+graph[i][k]){                        graph[j][k] = graph[j][i] + graph[i][k];                    }                }            }        }        if(graph[A-1][B-1]== 9999999){            System.out.println(-1);        }else{            System.out.println(graph[A-1][B-1]);        }    }}
0 0
原创粉丝点击