Notice
Recent Posts
Recent Comments
Link
«   2025/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
Archives
Today
Total
관리 메뉴

전공공부

[BOJ_10159] 저울 본문

Study/Problem Solving

[BOJ_10159] 저울

monitor 2024. 2. 4. 22:02

설명


특이한 알고리즘 기법이 사용된 문제는 아니고 일반적인 플로이드 와셜 문제인데요. 다만, 수의 비교를 통해서 갈 수 있는 곳을 찾아내는 문제다 보니 나 보다 큰 수는 어디까지 탐색 할 수 있는지 파악 할 수 있는 곳을 찾고 나 보다 작은 수는 어디까지 탐색 할 수 있는 지 탐색이 필요합니다. 따라서, 아래와 같이 코드 구현을 하였습니다.

코드


 

 

package BOJ.shortest_path;

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

public class BOJ_10159 {
    static int N,M;
    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine()," ");
        N = Integer.parseInt(tk.nextToken());
        tk = new StringTokenizer(in.readLine()," ");
        M = Integer.parseInt(tk.nextToken());
        int map[][] = new int[N][N];
        int reverseMap[][] = new int[N][N];
        for(int tmp[] : map) {
            Arrays.fill(tmp, 1000_000_000);
        }
        for(int tmp[] : reverseMap) {
            Arrays.fill(tmp, 1000_000_000);
        }

        for (int i = 0; i < M; i++){
            tk = new StringTokenizer(in.readLine()," ");
            int from = Integer.parseInt(tk.nextToken()) - 1;
            int to = Integer.parseInt(tk.nextToken()) - 1;
            reverseMap[to][from] = 0;
            map[from][to] = 0;
        }
        for (int k = 0; k < N; k++){
            for (int i = 0; i < N; i++){
                for (int j = 0; j < N; j++){
                    if(i == j || (map[i][j] == 0 && reverseMap[i][j] == 0)){
                        continue;
                    }
                    if(map[i][k] + map[k][j] < map[i][j]){
                        map[i][j] = map[i][k] + map[k][j];
                    }
                    if(reverseMap[i][k] + reverseMap[k][j] < reverseMap[i][j]){
                        reverseMap[i][j] = reverseMap[i][k] + reverseMap[k][j];
                    }
                }
            }
        }
        int cnt = 0;
        for (int i = 0; i < N; i++){
            cnt = 0;
            for (int j = 0; j < N; j++) {
                if ((reverseMap[i][j] > 0 && map[i][j] > 0) && i != j){
                    cnt++;
                }
            }
            System.out.println(cnt);
        }
    }
}

 

10159번: 저울 (acmicpc.net)

 

10159번: 저울

첫 줄에는 물건의 개수 N 이 주어지고, 둘째 줄에는 미리 측정된 물건 쌍의 개수 M이 주어진다. 단, 5 ≤ N ≤ 100 이고, 0 ≤ M ≤ 2,000이다. 다음 M개의 줄에 미리 측정된 비교 결과가 한 줄에 하나씩

www.acmicpc.net

 

 

 

'Study > Problem Solving' 카테고리의 다른 글

[BOJ_22865] 가장 먼 곳  (1) 2024.02.11
[BOJ_11657] 타임머신  (1) 2024.02.07
[BOJ_2224] 명제증명  (0) 2024.02.02
[BOJ_1227] 발전소 설치  (1) 2024.01.28
[BOJ_14938] 서강그라운드  (1) 2024.01.27