Notice
Recent Posts
Recent Comments
Today
Total
01-10 18:14
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[๋ฐฑ์ค€ 11403] ๊ฒฝ๋กœ ์ฐพ๊ธฐ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[๋ฐฑ์ค€ 11403] ๊ฒฝ๋กœ ์ฐพ๊ธฐ

WonderJay 2023. 1. 19. 14:05
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://www.acmicpc.net/problem/11403

 

11403๋ฒˆ: ๊ฒฝ๋กœ ์ฐพ๊ธฐ

๊ฐ€์ค‘์น˜ ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ G๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ชจ๋“  ์ •์  (i, j)์— ๋Œ€ํ•ด์„œ, i์—์„œ j๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

www.acmicpc.net

๋”๋ณด๊ธฐ

 

 

๋ฌธ์ œ

๊ฐ€์ค‘์น˜ ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ G๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ชจ๋“  ์ •์  (i, j)์— ๋Œ€ํ•ด์„œ, i์—์„œ j๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ ์ค„์—๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ธ์ ‘ ํ–‰๋ ฌ์ด ์ฃผ์–ด์ง„๋‹ค. i๋ฒˆ์งธ ์ค„์˜ j๋ฒˆ์งธ ์ˆซ์ž๊ฐ€ 1์ธ ๊ฒฝ์šฐ์—๋Š” i์—์„œ j๋กœ ๊ฐ€๋Š” ๊ฐ„์„ ์ด ์กด์žฌํ•œ๋‹ค๋Š” ๋œป์ด๊ณ , 0์ธ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค๋Š” ๋œป์ด๋‹ค. i๋ฒˆ์งธ ์ค„์˜ i๋ฒˆ์งธ ์ˆซ์ž๋Š” ํ•ญ์ƒ 0์ด๋‹ค.

์ถœ๋ ฅ

์ด N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ์ธ์ ‘ํ–‰๋ ฌ ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ์ •์  i์—์„œ j๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ์œผ๋ฉด i๋ฒˆ์งธ ์ค„์˜ j๋ฒˆ์งธ ์ˆซ์ž๋ฅผ 1๋กœ, ์—†์œผ๋ฉด 0์œผ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1 

3
0 1 0
0 0 1
1 0 0

์˜ˆ์ œ ์ถœ๋ ฅ 1 

1 1 1
1 1 1
1 1 1

์˜ˆ์ œ ์ž…๋ ฅ 2 

7
0 0 0 1 0 0 0
0 0 0 0 0 0 1
0 0 0 0 0 0 0
0 0 0 0 1 1 0
1 0 0 0 0 0 0
0 0 0 0 0 0 1
0 0 1 0 0 0 0

์˜ˆ์ œ ์ถœ๋ ฅ 2 

1 0 1 1 1 1 1
0 0 1 0 0 0 1
0 0 0 0 0 0 0
1 0 1 1 1 1 1
1 0 1 1 1 1 1
0 0 1 0 0 0 1
0 0 1 0 0 0 0
 

๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 100 ์œผ๋กœ ์ž‘์€ ํŽธ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜์—ฌ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด ํŽธํ•˜๋‹ค.

 

๊ธฐ์กด์˜ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ,

 

A-B ๊นŒ์ง€ ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜๊ณ , ๊ทธ ์‚ฌ์ด์— ๋…ธ๋“œ K ๊ฐ€ ์žˆ๋‹ค๋ฉด A-K, K-B ๋˜ํ•œ ๋ถ€๋ถ„ ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ ๋ชจ๋“  ๋…ธ๋“œ ๊ฐ„ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฅผ ์กฐ๊ธˆ๋งŒ ์ˆ˜์ •ํ•˜์ž.

 

A-B ์‚ฌ์ด์— ์ค‘๊ฐ„ ๋…ธ๋“œ K ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋ฉด, A-B ๋˜ํ•œ ์—ฐ๊ฒฐ๋œ ๊ฒƒ์ด๋‹ค.

 

์ฆ‰, graph[a][k] == 1 ์ด๊ณ  graph[k][b] == 1 ์ด๋ฉด graph[a][b] == 1 ์ผ ๊ฒƒ์ด๋‹ค.

 

์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜์ž.

 

import java.io.*;
import java.util.StringTokenizer;

public class Main {
    static int graph[][];

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;

        int n = Integer.parseInt(br.readLine()); // ๋…ธ๋“œ ๊ฐœ์ˆ˜

        graph = new int[n + 1][n + 1];

        // ์ดˆ๊ธฐํ™”
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i == j)
                    graph[i][j] = 0;
                else
                    graph[i][j] = 10000000;
            }
        }

        for (int i = 1; i <= n; i++) {
            stk = new StringTokenizer(br.readLine());
            for(int j = 1 ; j <= n ; j ++){
                int cost = Integer.parseInt(stk.nextToken());
                if(graph[i][j] > cost && cost != 0){
                    graph[i][j] = cost;
                }
            }
        }
        // ํ”Œ๋กœ์ด๋“œ - ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ O(v^3)
        for (int k = 1; k <= n; k++) {
            for (int s = 1; s <= n; s++) {
                for (int e = 1; e <= n; e++) {
                    if (graph[s][k] == 1 && graph[k][e] == 1)
                        graph[s][e] = 1;
                }
            }
        }

        // ์ •๋‹ต ์ถœ๋ ฅ
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (graph[i][j] == 10000000)
                    bw.write("0 ");
                else
                    bw.write(graph[i][j] + " ");
            }
            bw.write("\n");
        }
        bw.flush();
        bw.close();
    }
}

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments