반응형
05-14 05:47
Today
Total
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
관리 메뉴

개발하는 고라니

[백준] 13424번 : 비밀 모임 본문

Programming/백준

[백준] 13424번 : 비밀 모임

조용한고라니 2021. 3. 17. 23:24
반응형
 

13424번: 비밀 모임

입력 데이터는 표준 입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에 테스트 케이스의 개수를 나타내는 자연수 T가 주어진다. 각 테스트 케이스의 첫째 줄에는 방

www.acmicpc.net


[Floyd-Warshall or 다익스트라]

나는 플로이드-와샬로 풀었으나, 다익스트라로 푸는 것이 어쩌면 더 적은 시간이 들지도 모르겠다. 다익스트라를 사용한다면 k번의 다익스트라를 수행하게 되니까 말이다.

 

일반적인 플로이드 와샬 문제처럼 거리의 대한 배열 dist를 i == j일때는 0으로, 아닐때는 INF로 초기화 해놓고, 입력에 따라 dist에 값을 대입한다.

            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++)
                    dist[i][j] = (i != j)? INF:0;

            for(int i=0; i<m; i++){
                st = new StringTokenizer(br.readLine());

                int h = Integer.parseInt(st.nextToken());
                int tg = Integer.parseInt(st.nextToken());
                int d = Integer.parseInt(st.nextToken());

                dist[h][tg] = d;
                dist[tg][h] = d;
            }

이후 바로 플로이드-와샬

            for(int z=1; z<=n; z++)
                for(int i=1; i<=n; i++)
                    for(int j=1; j<=n; j++)
                    
                        if(i == j)
                            continue;
                        else
                            dist[i][j] = Math.min(dist[i][j], dist[i][z] + dist[z][j]);

나는 친구들이 있는 방을 arr[ ] 배열에 저장해두었다. 즉 arr[0], arr[1], arr[2], ..., arr[k-1]에는 친구들이 있는 방 번호가 저장되어있다. 이 방들을 시작점으로, 1번 방 ~ n번 방으로의 거리를 더해 그 중 가장 작은 값을 갖는 방에서 모이기로 하면 되겠다.

# Code </>

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

public class Main {

    static final int INF = 1000000000;
    static int[][] dist = new int[101][101];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());

        for(int a=0; a<t; a++){
            StringTokenizer st = new StringTokenizer(br.readLine());

            int n = Integer.parseInt(st.nextToken());
            int m = Integer.parseInt(st.nextToken());

            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++)
                    dist[i][j] = (i != j)? INF:0;

            for(int i=0; i<m; i++){
                st = new StringTokenizer(br.readLine());

                int h = Integer.parseInt(st.nextToken());
                int tg = Integer.parseInt(st.nextToken());
                int d = Integer.parseInt(st.nextToken());

                dist[h][tg] = d;
                dist[tg][h] = d;
            }
            int k = Integer.parseInt(br.readLine());

            int[] arr = new int[n];
            st = new StringTokenizer(br.readLine());
            for(int i=0; i<k; i++)
                arr[i] = Integer.parseInt(st.nextToken());

            for(int z=1; z<=n; z++)
                for(int i=1; i<=n; i++)
                    for(int j=1; j<=n; j++)
                        if(i == j)
                            continue;
                        else
                            dist[i][j] = Math.min(dist[i][j], dist[i][z] + dist[z][j]);
            //출력
            int min = INF, idx = -1;
            for(int i=1; i<=n; i++){
            
                int sum = 0;
                
                for(int j=0; j<k; j++)
                    sum += dist[arr[j]][i];

                if(sum < min){
                    idx = i;
                    min = sum;
                }
            }
            System.out.println(idx);
        }
    }
}
반응형

'Programming > 백준' 카테고리의 다른 글

[백준] 10473번 : 인간 대포  (0) 2021.03.19
[백준] 6087번 : 레이저 통신  (0) 2021.03.18
[백준] 10423번 : 전기가 부족해  (0) 2021.03.17
[백준] 16397번 : 탈출  (0) 2021.03.17
[백준] 16118번 : 달빛 여우  (0) 2021.03.16
Comments