하루

모각코 5일차 (19.07.12) 플로이드 알고리즘 문제 본문

19년 하계 모.각.코

모각코 5일차 (19.07.12) 플로이드 알고리즘 문제

따뜻한라떼한잔 2019. 7. 12. 17:00

문제

n(1 ≤ n ≤ 100)개의 도시가 있다. 그리고 한 도시에서 출발하여 다른 도시에 도착하는 m(1 ≤ m ≤ 100,000)개의 버스가 있다. 각 버스는 한 번 사용할 때 필요한 비용이 있다.

모든 도시의 쌍 (A, B)에 대해서 도시 A에서 B로 가는데 필요한 비용의 최솟값을 구하는 프로그램을 작성하시오.

입력

첫째 줄에 도시의 개수 n(1 ≤ n ≤ 100)이 주어지고 둘째 줄에는 버스의 개수 m(1 ≤ m ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스의 출발 도시의 번호가 주어진다. 버스의 정보는 버스의 시작 도시 a, 도착 도시 b, 한 번 타는데 필요한 비용 c로 이루어져 있다. 시작 도시와 도착 도시가 같은 경우는 없다. 비용은 100,000보다 작거나 같은 자연수이다.

시작 도시와 도착 도시를 연결하는 노선은 하나가 아닐 수 있다.

출력

N개의 줄을 출력해야 한다. i번째 줄에 출력하는 j번째 숫자는 도시 i에서 j로 가는데 필요한 최소 비용이다. 만약, i에서 j로 갈 수 없는 경우에는 그 자리에 0을 출력한다.

각 도시의 번호를 하나의 정점이라 생각하고 비용을 가중치라 두고 그래프 행렬로 표현을 한 뒤 Dijkstra 알고리즘을 이용하여 각 도시간의 최소 비용값을 계산하도록 자바로 구현하였다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static int[] distance;
    static boolean[] visit;
    static int[][] matrix;
    static int n;
    static int m;
    static int[][] result;

    public static void main(String[] args) {
        Main out = new Main();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try {
            n = Integer.parseInt(br.readLine());
            m = Integer.parseInt(br.readLine());
            matrix = new int[n][n];
            for (int x = 0; x < n; x++) {
                for (int y = 0; y < n; y++) {
                    matrix[x][y] = Integer.MAX_VALUE;

                }
            }

            for (int inputcount = 0; inputcount < m; inputcount++) {
                String[] input = br.readLine().split(" ");
                if (matrix[Integer.parseInt(input[0]) - 1][Integer.parseInt(input[1]) - 1] == Integer.MAX_VALUE) {
                    matrix[Integer.parseInt(input[0]) - 1][Integer.parseInt(input[1]) - 1] = Integer.parseInt(input[2]);
                } else {
                    if (matrix[Integer.parseInt(input[0]) - 1][Integer.parseInt(input[1]) - 1] > Integer
                            .parseInt(input[2])) {
                        matrix[Integer.parseInt(input[0]) - 1][Integer.parseInt(input[1]) - 1] = Integer
                                .parseInt(input[2]);
                    }
                }
            }

            result = new int[n][n];
            for (int index = 0; index < n; index++) {
                distance = new int[n];
                visit = new boolean[n];
                for (int i = 0; i < n; i++) {
                    distance[i] = matrix[index][i];
                }
                out.Dijkstra(index);
                for (int k = 0; k < n; k++) {
                    result[index][k] = distance[k];
                    if (distance[k] == Integer.MAX_VALUE) {
                        result[index][k] = 0;
                    }

                }
            }

            for (int x = 0; x < n; x++) {
                for (int y = 0; y < n; y++) {
                    System.out.print(result[x][y] + " ");
                }
                System.out.println();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void Dijkstra(int search) {
        int next = -1;
        int min = Integer.MAX_VALUE;
        distance[search] = 0;
        for (int index = 0; index < n; index++) {
            if (distance[index] != Integer.MAX_VALUE && min > distance[index] && !visit[index]) {
                min = distance[index];
                next = index;
            }
        }
        if (next == -1) {
            return;
        }

        visit[next] = true;
        for (int j = 0; j < n; j++) {
            if (!visit[j] && matrix[next][j] != Integer.MAX_VALUE && (distance[j] > distance[next] + matrix[next][j])) {
                distance[j] = distance[next] + matrix[next][j];
            }
        }
        Dijkstra(search);
    }

}
Comments