백준 14938 java

magicdrill·2024년 10월 23일
0

백준 문제풀이

목록 보기
470/654

백준 14938 java


플로이드 워셜을 사용한다. 플로이드 워셜로 각 점에서 다른 점까지 이동하는 최소비용경로를 구하고 그 값이 m 아래인 지점의 아이템을 파밍할 수 있다.
불필요한 반복문을 줄여볼 수 있을거 같다.

import java.util.Arrays;
import java.util.Scanner;

public class bj14938 {
    static Scanner scanner = new Scanner(System.in);
    static int n, m, r;
    static int [][] ground;
    static int [] item;

    public static void main(String[] args) {
        inputData();
        findAnswer();

        scanner.close();
    }

    public static void inputData()
    {
        System.out.println("inputData()");
        int i, t, a, b, I;

        n = scanner.nextInt();//지역의 개수
        m = scanner.nextInt();//수색 범위
        r = scanner.nextInt();//길의 개수

        ground = new int[n + 1][n + 1];
        item = new int[n + 1];

        //ground 배열 초기화
        for(i = 0; i < ground.length; i++)//fill 함수는 1차원 배열만 가능하다
        {
            Arrays.fill(ground[i], 16);//모든 거리를 16으로 초기화
            ground[i][i] = 0;
        }

        //각 구역에 있는 아이템의 수
        for(i = 1; i <= n; i++)
        {
            t = scanner.nextInt();
            item[i] = t;
        }

        //지역 이동 정보
        for(i = 0; i < r; i++)
        {
            a = scanner.nextInt();
            b = scanner.nextInt();
            I = scanner.nextInt();
            ground[a][b] = I;
            ground[b][a] = I;
        }

        System.out.println("\n입력결과");
        System.out.println("ground : ");
        for(i = 0; i < ground.length; i++)
        {
            for(int j = 0; j < ground[i].length; j++)
            {
                System.out.print(ground[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("\nitem : ");
        for(i = 0; i < item.length; i++)
        {
            System.out.print(item[i] + " ");
        }
        System.out.println("\n");
    }

    public static void findAnswer()
    {
        System.out.println("findAnswer()");

        floydWarshall();//어느 점에서 어느 점까지 도착 할 수 있는 최소 거리 찾기
        System.out.println(maxItemsValue());
    }

    public static void floydWarshall()
    {
        System.out.println("floydWarshall()");
        int i, j, k;

        for(k = 1; k <= n; k++)
        {
            for(i = 1; i <= n; i++)
            {
                for(j = 1; j <= n; j++)
                {
                    ground[i][j] = Math.min(ground[i][j], ground[i][k] + ground[k][j]);
                }
            }
        }

        System.out.println("플로이드 워셜 결과");
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j <= n; j++)
            {
                System.out.print(ground[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static int maxItemsValue()
    {
        System.out.println("maxItemsValue()");
        int answer = 0;
        int i, j, temp;

        System.out.println("tempSum : ");
        for(i = 1; i < ground.length; i++)
        {
            temp = 0;
            for(j = 1; j < ground[i].length; j++)
            {
                if(ground[i][j] <= m)
                {
                    temp += item[j];
                }
            }
            System.out.print(temp + " ");
            if(temp > answer)
            {
                answer = temp;
            }
        }
        System.out.println("\n");

        return answer;
    }
}

0개의 댓글