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

Partially Committed

[๋ฐฑ์ค€ 9251/1520/9370] ์ž๋ฐ” ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 9251/1520/9370] ์ž๋ฐ”

WonderJay 2023. 2. 20. 12:16
728x90
๋ฐ˜์‘ํ˜•
SMALL

(title: [๋ฐฑ์ค€ 9251/1520/9370] ์ž๋ฐ”)

A. LCS BOJ 9251

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

 

9251๋ฒˆ: LCS

LCS(Longest Common Subsequence, ์ตœ์žฅ ๊ณตํ†ต ๋ถ€๋ถ„ ์ˆ˜์—ด)๋ฌธ์ œ๋Š” ๋‘ ์ˆ˜์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ชจ๋‘์˜ ๋ถ€๋ถ„ ์ˆ˜์—ด์ด ๋˜๋Š” ์ˆ˜์—ด ์ค‘ ๊ฐ€์žฅ ๊ธด ๊ฒƒ์„ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ACAYKP์™€ CAPCAK์˜ LCS๋Š” ACAK๊ฐ€ ๋œ๋‹ค.

www.acmicpc.net

๋‚œ์ด๋„ Gold 5
ํ’€์ด ์‹œ๊ฐ„ 10๋ถ„
๋ถ„๋ฅ˜ DP
์‹œ๊ฐ„๋ณต์žก๋„ O(NM)
๊ณต๊ฐ„๋ณต์žก๋„  
import java.io.*;
import java.util.*;

public class Main {
    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;

        String str1 = br.readLine();
        String str2 = br.readLine();
        int n = str1.length();
        int m = str2.length();
        int[][] dp = new int[n + 1][m + 1];

        int max_length = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if(str1.charAt(i-1)==str2.charAt(j-1)){
                    // ๊ฐ™๋‹ค๋ฉด
                     dp[i][j] = dp[i-1][j-1] + 1;
                } else {
                    // ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
                max_length = Math.max(dp[i][j], max_length);
            }
        }

        bw.write(max_length+"\n");
        bw.flush();
        bw.close();
    }

}

(์ถ”๊ฐ€)

์œ„ ๋ฌธ์ œ๋Š” LCS ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด์ง€๋งŒ ๋งŒ์•ฝ LCS ์ž์ฒด๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์•„๋ž˜์˜ ์—ญ์ถ”์  ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

Stack<Character> st = new Stack<>();
int now = 0;
int i = n;
int j = m;
while (i > 0 && j > 0) {
    if (i == 0 || j == 0)
        continue;
    now = dp[i][j];
    if (now == dp[i - 1][j]) {
        i--;
    } else if (now == dp[i][j - 1]) {
        j--;
    } else {
        st.add(str1.charAt(i - 1));
        i--;
        j--;
    }
}

bw.write(max_length + "\n");

while (!st.empty()){
    bw.append(st.pop());
} bw.newLine();

OUTPUT:

4
ACAK

 

B. ๋‚ด๋ฆฌ๋ง‰๊ธธ BOJ 1520

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

 

1520๋ฒˆ: ๋‚ด๋ฆฌ๋ง‰ ๊ธธ

์—ฌํ–‰์„ ๋– ๋‚œ ์„ธ์ค€์ด๋Š” ์ง€๋„๋ฅผ ํ•˜๋‚˜ ๊ตฌํ•˜์˜€๋‹ค. ์ด ์ง€๋„๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋ฉฐ ์—ฌ๋Ÿฌ ์นธ์œผ๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ๋‹ค. ํ•œ ์นธ์€ ํ•œ ์ง€์ ์„ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ ๊ฐ ์นธ์—๋Š” ๊ทธ ์ง€์ ์˜ ๋†’์ด๊ฐ€ ์“ฐ์—ฌ ์žˆ์œผ

www.acmicpc.net

๋‚œ์ด๋„ Gold 3
ํ’€์ด ์‹œ๊ฐ„ 50๋ถ„
๋ถ„๋ฅ˜ DFS + DP, BFS with Priority Queue
์‹œ๊ฐ„๋ณต์žก๋„ dfs : O(4V) ~= O(V)
bfs : O(VlogV)
๊ณต๊ฐ„๋ณต์žก๋„ O(V)

[1. DFS + DP ๋ฅผ ์ด์šฉํ•œ ํ’€์ด]

static int dfs(int x, int y) {
    if (y == row - 1 && x == col - 1) {
        return 1;
    }
    if (dp[y][x] != -1) {
        return dp[y][x]; // ์ด๋ฏธ ์ €์žฅ๋œ ๊ฐ’์ด ์žˆ์„ ์‹œ ๊ทธ๊ฒƒ์„ ๋ฐ˜ํ™˜
    }
    dp[y][x] = 0;
    for (int dir = 0; dir < 4; dir++) {
        int nx = x + dx[dir];
        int ny = y + dy[dir]; // ์ƒˆ๋กœ ์ด๋™ํ•  ์ขŒํ‘œ
        if (nx < 0 || nx >= col || ny < 0 || ny >= row) {
            continue; // out of bounds
        }
        if (graph[y][x] > graph[ny][nx]) {
            // ๋‚ด๋ฆฌ๋ง‰ ๊ธธ์ด๋ฉด
            dp[y][x] += dfs(nx, ny);
        }
    }
    return dp[y][x];
}

[2. BFS + Priority Queue ๋ฅผ ์‚ฌ์šฉํ•œ ํ’€์ด]

static int bfs(int x, int y, int heigh) {
    dp[y][x] = 1;
    PriorityQueue<Node> pq = new PriorityQueue<>();
    pq.add(new Node(x, y, heigh));
    while (!pq.isEmpty()) {
        Node now = pq.poll();
        for (int dir = 0; dir < 4; dir++) {
            int nx = now.x + dx[dir];
            int ny = now.y + dy[dir];
            if (nx < 0 || nx >= col || ny < 0 || ny >= row) {
                continue; // out of bounds
            }
            if (graph[now.y][now.x] > graph[ny][nx]) {
                // ๋‚ด๋ฆฌ๋ง‰ ๊ธธ์ด๋ฉด
                if (dp[ny][nx]==0) {
                    pq.add(new Node(nx, ny, graph[ny][nx]));
                }
                dp[ny][nx] += dp[now.y][now.x];
            }

        }
    }
    return dp[row - 1][col - 1];
}

 

 

C. ๋ฏธํ™•์ธ ๋„์ฐฉ์ง€ BOJ 9370

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

 

9370๋ฒˆ: ๋ฏธํ™•์ธ ๋„์ฐฉ์ง€

(์ทจ์ต)B100 ์š”์›, ์š”๋ž€ํ•œ ์˜ท์ฐจ๋ฆผ์„ ํ•œ ์„œ์ปค์Šค ์˜ˆ์ˆ ๊ฐ€ ํ•œ ์Œ์ด ํ•œ ๋„์‹œ์˜ ๊ฑฐ๋ฆฌ๋“ค์„ ์ด๋™ํ•˜๊ณ  ์žˆ๋‹ค. ๋„ˆ์˜ ์ž„๋ฌด๋Š” ๊ทธ๋“ค์ด ์–ด๋””๋กœ ๊ฐ€๊ณ  ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์•Œ์•„๋‚ธ ๊ฒƒ์€ ๊ทธ๋“ค์ด s์ง€์ ์—์„œ

www.acmicpc.net

๋‚œ์ด๋„ Gold 2
ํ’€์ด ์‹œ๊ฐ„ 40 ๋ถ„
๋ถ„๋ฅ˜ ๋‹ค์ต์ŠคํŠธ๋ผ
์‹œ๊ฐ„๋ณต์žก๋„ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O((V+E)logV) ์ด๋‹ค.
๋…ธ๋“œ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 2000 ๊ฐœ์ด๊ณ 
์—ฃ์ง€์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 50000 ๊ฐœ
๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 100 ๊ฐœ์ด๋‹ค
๊ทธ๋Ÿฌ๋ฉด ์ตœ์•…์˜ ๊ฒฝ์šฐ์— ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ๋Œ€๋žต
(2000+50000)log2000 * 100 ~= 17,165,355 = ์ฒœ์น ๋ฐฑ๋งŒ๋ฒˆ์˜ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
์ผ๋ฐ˜์ ์ธ ํ™˜๊ฒฝ์—์„œ 2์ฒœ๋งŒ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ 1์ดˆ ์ด๋‚ด์— ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ,
์œ„ ์ฝ”๋“œ๋Š” ์‹œ๊ฐ„์ œํ•œ 3 ์ดˆ ์•ˆ์— ์•ˆ์ „ํ•˜๊ฒŒ ์ˆ˜ํ–‰๋  ๊ฒƒ์ด๋ผ๊ณ  ํ•ฉ๋ฆฌ์ ์œผ๋กœ ์˜ˆ์ƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
๊ณต๊ฐ„๋ณต์žก๋„ O(n+m) 

๋ฌธ์ œ ์„ค๋ช…์ด ์กฐ๊ธˆ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต๊ฒŒ ๋˜์–ด์žˆ๋Š”๋ฐ, ๊ฐ„๋‹จํ•˜๊ฒŒ ์š”์•ฝํ•˜๋ฉด ์ด๋ ‡๋‹ค.

 

์ฃผ์–ด์ง€๋Š” ์ •๋ณด๋Š” s(์‹œ์ž‘์ ), t0,t1,t2...(ํ›„๋ณด ๋ชฉ์ ์ง€), g-h (๋ชฉ์ ์ง€๋กœ ํ–ฅํ•  ๋•Œ ๋ฐ˜๋“œ์‹œ ๊ฑฐ์ณ์•ผ ํ•˜๋Š” ๊ฒฝ๋กœ) ์ธ๋ฐ,

 

์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ s ๋ถ€ํ„ฐ g-h , h-g ๋ฅผ ๊ฑฐ์ณ์„œ ํ›„๋ณด ๋ชฉ์ ์ง€์ธ t0, ... tn ์— ๋„๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ์ตœ๋‹จ ๊ฒฝ๋กœ์ธ ์ง€ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

์ฆ‰, s ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ํ›„๋ณด ๋ชฉ์ ์ง€์— ๋„๋‹ฌํ•˜๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ(1)์™€ s ์—์„œ ์ถœ๋ฐœํ•˜๊ณ  g-h ํ˜น์€ h-g ๋ฅผ ๊ฑฐ์ณ์„œ ํ›„๋ณด ๋ชฉ์ ์ง€์— ๋„๋‹ฌํ•˜๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ(2)๋ฅผ ๊ตฌํ•œ ๋‹ค์Œ ์ด๋ฅผ ๋น„๊ตํ•˜์—ฌ ํ›„๋ณด ๋ชฉ์ ์ง€ ์ค‘์—์„œ ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์€ ๊ฒƒ์„ ์ œ์™ธ์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค.

 

์ด๋Ÿฌํ•œ ํŒ๋‹จ์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” ๋ฌธ์ œ ์ƒํ™ฉ์—์„œ ์„œ์ปค์Šค ์˜ˆ์ˆ ๊ฐ€ ํ•œ ์Œ์€ ๋ฐ˜๋“œ์‹œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋กœ๋งŒ ์ด๋™ํ•œ๋‹ค๋Š” ๊ฐ€์ • ๋•Œ๋ฌธ์ด๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ด๋“ค์ด g ์™€ h ์‚ฌ์ด์˜ ๋„๋กœ๋ฅผ ํ†ต๊ณผํ–ˆ๋‹ค๋Š” ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง€๋ฏ€๋กœ, 

 

g ์™€ h ์‚ฌ์ด์˜ ๋„๋กœ๋ฅผ ํ†ต๊ณผํ•˜์ง€ ์•Š๊ณ  ํ›„๋ณด ๋ชฉ์ ์ง€๋กœ ๋„๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ผ๋ฉด

 

ํ›„๋ณด ๋ชฉ์ ์ง€์—์„œ ์ œ์™ธํ•ด๋„ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋Ÿฌ๋ฉด ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ๊นŒ?

 

์šฐ๋ฆฌ๊ฐ€ ์–ป์–ด์•ผ ํ•˜๋Š” ์ •๋ณด๋Š” ์•„๋ž˜ ๋‘ ๊ฐ€์ง€ ์ด๋‹ค. 

 

1. s ๋ถ€ํ„ฐ ํ›„๋ณด ๋ชฉ์ ์ง€๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ

 

2. s ๋ถ€ํ„ฐ g์™€ h ์‚ฌ์ด์˜ ๋„๋กœ๋ฅผ ๊ฑฐ์ณ์„œ ํ›„๋ณด ๋ชฉ์ ์ง€๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ

 

 

(2) ๋ถ€ํ„ฐ ์ƒ๊ฐํ•ด๋ณด์ž.s ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ g ์™€ h ์‚ฌ์ด์˜ ๋„๋กœ๋ฅผ ๊ฑฐ์ณ์„œ ํ›„๋ณด ๋ชฉ์ ์ง€(t) ๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์•„๋ž˜ ๋‘ ๊ฐ€์ง€ ์ผ€์ด์Šค๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.s->g->h->ts->h->g->t๊ทธ๋Ÿฌ๋ฏ€๋กœ (2) = Math.min(s->g->h->t, s->h->g->t) ์œผ๋กœ ๊ตฌํ•˜๋ฉด ๋ ํ…๋ฐ, 

 

์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” s->g ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ, g->h ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ, h->t ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ, s->h ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ, g->t ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

(1) ์˜ ๊ฒฝ์šฐ์—๋Š” s->t ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

์ฆ‰, 3 ๊ฐ€์ง€ ์‹œ์ž‘ ๋…ธ๋“œ(s, g, h)๋กœ ๋ถ€ํ„ฐ ๋ชจ๋“  ๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์–ป๊ณ ์ž ํ•˜๋Š” ์ •๋ณด๋ฅผ ๋ชจ๋‘ ์–ป์„์ˆ˜ ์žˆ๋‹ค.

 

3๊ฐ€์ง€ ๋‹จ์ผ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ „์ฒด ๋…ธ๋“œ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด ๋˜๋Š”๋ฐ, ์ด๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ์ž‘์ ์„ ๋ฐ”๊พธ์–ด ๊ฐ€๋ฉด์„œ 3๋ฒˆ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋  ๊ฒƒ์ž„์„ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋งˆ์นจ, ์—ฃ์ง€์˜ ๊ฐ€์ค‘์น˜๋“ค์€ ๋ชจ๋‘ ์–‘์ˆ˜์ด๋ฏ€๋กœ  ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ,

 

์‹œ๊ฐ„ ๋ณต์žก๋„ ๋˜ํ•œ ๋„๋„ํ•˜๋‹ค.

 

๋”๋ณด๊ธฐ

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O((V+E)logV) ์ด๋‹ค.
๋…ธ๋“œ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 2000 ๊ฐœ์ด๊ณ 
์—ฃ์ง€์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 50000 ๊ฐœ
๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 100 ๊ฐœ์ด๋‹ค
๊ทธ๋Ÿฌ๋ฉด ์ตœ์•…์˜ ๊ฒฝ์šฐ์— ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ๋Œ€๋žต
(2000+50000)log2000 * 100 ~= 17,165,355 = ์ฒœ์น ๋ฐฑ๋งŒ๋ฒˆ์˜ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
์ผ๋ฐ˜์ ์ธ ํ™˜๊ฒฝ์—์„œ 2์ฒœ๋งŒ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ 1์ดˆ ์ด๋‚ด์— ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ,
์œ„ ์ฝ”๋“œ๋Š” ์‹œ๊ฐ„์ œํ•œ 3์ดˆ ์•ˆ์— ์•ˆ์ „ํ•˜๊ฒŒ ์ˆ˜ํ–‰๋  ๊ฒƒ์ด๋ผ๊ณ  ํ•ฉ๋ฆฌ์ ์œผ๋กœ ์˜ˆ์ƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ์˜ ์‹œ์ž‘์ ์„ s, g, h ๋กœ ๋ฐ”๊พธ์–ด ๊ฐ€๋ฉด์„œ 3 ๋ฒˆ ์ˆ˜ํ–‰ํ•˜๋ฉฐ

 

 s->g ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ, g->h ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ, h->t ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ, s->h ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ, g->t ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ์–ป์–ด๋‚ด๊ณ ,

 

์ด๋กœ๋ถ€ํ„ฐ 

 

1. s ๋ถ€ํ„ฐ ํ›„๋ณด ๋ชฉ์ ์ง€๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ

 

2. s ๋ถ€ํ„ฐ g์™€ h ์‚ฌ์ด์˜ ๋„๋กœ๋ฅผ ๊ฑฐ์ณ์„œ ํ›„๋ณด ๋ชฉ์ ์ง€๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ

 

๋ฅผ ๋„์ถœํ•œ ๋‹ค์Œ

 

g ์™€ h ์‚ฌ์ด์˜ ๋„๋กœ๋ฅผ ํ†ต๊ณผํ•˜์ง€ ์•Š๊ณ  ํ›„๋ณด ๋ชฉ์ ์ง€๋กœ ๋„๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•(1)์ด

g ์™€ h ์‚ฌ์ด์˜ ๋„๋กœ๋ฅผ ํ†ต๊ณผํ•˜๊ณ  ํ›„๋ณด ๋ชฉ์ ์ง€๋กœ ๋„๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•(2) ๋ณด๋‹ค ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ผ๋ฉด ํ›„๋ณด ๋ชฉ์ ์ง€์—์„œ ์ œ์™ธ์‹œ์ผœ์ค€๋‹ค.

 

(์™œ๋ƒ๋ฉด ๋ฌด์กฐ๊ฑด g-h ๋ฅผ ํ†ต๊ณผํ•ด์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋กœ ์ง„ํ–‰ํ•œ๋‹ค๋Š” ์ „์ œ๊ฐ€ ์ฃผ์–ด์กŒ๋Š”๋ฐ, ๊ณ„์‚ฐ ํ•ด๋ณด๋‹ˆ g-h ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ํ›„๋ณด ๋ชฉ์ ์ง€๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.)

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด AC ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

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

public class Main {
    static int n;
    static int m;
    static int t;
    static int s;
    static int g;
    static int h;
    static ArrayList<Node>[] graph;
    static int[] dist;

    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 tc = Integer.parseInt(br.readLine());
        while (tc > 0) {
            tc--;
            ArrayList<Integer> anslist = new ArrayList<>();
            stk = new StringTokenizer(br.readLine());
            n = Integer.parseInt(stk.nextToken()); // ๋…ธ๋“œ ๊ฐœ์ˆ˜
            m = Integer.parseInt(stk.nextToken()); // ์—ฃ์ง€ ๊ฐœ์ˆ˜
            t = Integer.parseInt(stk.nextToken()); // ํ›„๋ณด ๊ฐœ์ˆ˜

            stk = new StringTokenizer(br.readLine());
            s = Integer.parseInt(stk.nextToken()); // ์‹œ์ž‘์ 
            g = Integer.parseInt(stk.nextToken());
            h = Integer.parseInt(stk.nextToken());

            init();

            for (int i = 0; i < m; i++) {
                stk = new StringTokenizer(br.readLine());
                int a = Integer.parseInt(stk.nextToken());
                int b = Integer.parseInt(stk.nextToken());
                int d = Integer.parseInt(stk.nextToken());
                graph[a].add(new Node(b, d));
                graph[b].add(new Node(a, d)); // ์–‘๋ฐฉํ–ฅ
            }
            int minimum_path = Integer.MAX_VALUE;
            for (int i = 0; i < t; i++) {
                int dest = Integer.parseInt(br.readLine());
                dijkstra(s);
                int s2dest = dist[dest];
                int s2g = dist[g];
                int s2h = dist[h];
                dijkstra(g);
                int g2dest = dist[dest];
                int g2h = dist[h];
                dijkstra(h);
                int h2dest = dist[dest];

                int inf = Integer.MAX_VALUE;
                if (s2dest == inf || s2g == inf || s2h == inf || g2h == inf)
                    continue;
                int s2g2h2dest = s2g + g2h + h2dest;
                int s2h2g2dest = s2h + g2h + g2dest;
                minimum_path = Math.min(s2g2h2dest, s2h2g2dest);

                if(s2dest < minimum_path){
                    continue;
                } else {
                    anslist.add(dest);
                }
            }

            Collections.sort(anslist);
            for (Integer iter : anslist) {
                bw.write(iter + " ");
            }
            bw.newLine();
        }

        bw.flush();
        bw.close();
    }

    static class Node implements Comparable<Node> {
        int to;
        int w;

        public Node(int to, int w) {
            this.to = to;
            this.w = w;
        }

        @Override
        public int compareTo(Node o) {
            if (this.w > o.w)
                return 1;
            else return -1;
        }
    }

    static void dijkstra(int start) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        Arrays.fill(dist, Integer.MAX_VALUE);
        dist[start] = 0;
        pq.add(new Node(start, 0));
        while (!pq.isEmpty()) {
            Node now = pq.poll();
            for (Node cur : graph[now.to]) {
                // ์—ฐ๊ฒฐ ๋…ธ๋“œ ํƒ์ƒ‰
                if (dist[cur.to] > dist[now.to] + cur.w) {
                    dist[cur.to] = dist[now.to] + cur.w;
                    pq.add(new Node(cur.to, dist[cur.to]));
                }
            }
        }
    }

    static void init() {
        graph = new ArrayList[n + 1];
        for (int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }
        dist = new int[n + 1];
    }
}

 

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