๐Ÿ‘ฉ๐Ÿปโ€๐ŸŒพ

๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ ์ •๋ฆฌ ๋ณธ๋ฌธ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ/๋ฐฑ์ค€(BOJ)

๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ ์ •๋ฆฌ

์ฅฌ์Šค์ด 2024. 1. 21. 21:15
728x90

1๋ฒˆ) 1753๋ฒˆ ์ตœ๋‹จ๊ฒฝ๋กœ

 

1753๋ฒˆ: ์ตœ๋‹จ๊ฒฝ๋กœ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ V์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค V โ‰ค 20,000, 1 โ‰ค E โ‰ค 300,000) ๋ชจ๋“  ์ •์ ์—๋Š” 1๋ถ€ํ„ฐ V๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์‹œ์ž‘ ์ •์ ์˜ ๋ฒˆํ˜ธ K(1 โ‰ค K โ‰ค V)๊ฐ€

www.acmicpc.net

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

public class BOJ_1753_1 {
    static int V, E, K;
    static ArrayList<Edge>[] graph;
    static boolean[] visited;
    static int[] dist;
    static PriorityQueue<Edge> pq;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        V = Integer.parseInt(st.nextToken());
        E = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(br.readLine());

        graph = new ArrayList[V + 1];
        visited = new boolean[V + 1];
        dist = new int[V + 1];
        pq = new PriorityQueue<>();

        for (int i = 0; i <= V; i++)
            graph[i] = new ArrayList<>();

        for (int i = 0; i <= V; i++)
            dist[i] = Integer.MAX_VALUE;

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

            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());
            int w = Integer.parseInt(st.nextToken());

            graph[u].add(new Edge(v, w));
        }
        pq.add(new Edge(K, 0));
        dist[K] = 0;

        while (!pq.isEmpty()) {
            Edge cur = pq.poll();
            int curVertex = cur.vertex;

            if (!visited[curVertex]) {
                visited[curVertex] = true;
                for (int i = 0; i < graph[curVertex].size(); i++) {
                    Edge temp = graph[curVertex].get(i);

                    int nextV = temp.vertex;
                    int value = temp.value;

                    if (dist[nextV] > dist[curVertex] + value) {
                        dist[nextV] = dist[curVertex] + value;
                        pq.add(new Edge(nextV, dist[nextV]));
                    }
                }
            }
        }

        for (int i = 1; i <= V; i++) {
            if (visited[i])
                System.out.println(dist[i]);
            else
                System.out.println("INF");
        }
    }

    static class Edge implements Comparable<Edge> {
        int vertex, value;  // ์—ฐ๊ฒฐ๋œ ์ •์ , ๊ฐ€์ค‘์น˜ ์ €์žฅ

        public Edge(int vertex, int value) {
            this.vertex = vertex;
            this.value = value;
        }

        @Override
        public int compareTo(Edge e) {
            return this.value - e.value;
        }
    }
}

 

2๋ฒˆ) 2211๋ฒˆ ๋„คํŠธ์›Œํฌ ๋ณต๊ตฌ

 

2211๋ฒˆ: ๋„คํŠธ์›Œํฌ ๋ณต๊ตฌ

์ฒซ์งธ ์ค„์— ๋‘ ์ •์ˆ˜ N, M์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ํšŒ์„ ์˜ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ ์ •์ˆ˜ A, B, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” A๋ฒˆ ์ปดํ“จํ„ฐ์™€ B๋ฒˆ ์ปดํ“จํ„ฐ๊ฐ€ ํ†ต์‹  ์‹œ๊ฐ„์ด C (1 โ‰ค C โ‰ค 10)์ธ ํšŒ์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค

www.acmicpc.net

1๋ฒˆ ๋ฌธ์ œ๋Š” ๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„์˜€๋‹ค๋ฉด, 2๋ฒˆ ๋ฌธ์ œ๋Š” ๋ฌด๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์ด๊ธฐ์— Node ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์—ฐ๊ฒฐ๋œ ์ •์ ์˜ ๋ฒˆํ˜ธ, ๊ฐ€์ค‘์น˜ + ๋ณธ์ธ์˜ ๋ฒˆํ˜ธ๋ฅผ ์„ ์–ธํ•ด์ค˜์•ผ ํ•œ๋‹ค.

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

public class Main {
    static ArrayList<Node>[] graph;
    static ArrayList<Node> ans;
    static boolean[] visited;
    static int[] dist;
    static PriorityQueue<Node> pq;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        graph = new ArrayList[N + 1];
        visited = new boolean[N + 1];
        dist = new int[N + 1];
        pq = new PriorityQueue<>();
        ans = new ArrayList<>();

        for (int i = 0; i <= N; i++)
            graph[i] = new ArrayList<>();

        for (int i = 0; i <= N; i++)
            dist[i] = Integer.MAX_VALUE;

        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            int s = Integer.parseInt(st.nextToken());
            int e = Integer.parseInt(st.nextToken());
            int t = Integer.parseInt(st.nextToken());

            graph[s].add(new Node(s, e, t));
            graph[e].add(new Node(e, s, t));
        }
        pq.add(new Node(1, 1, 0));
        visited[1] = true;
        dist[1] = 0;

        while (!pq.isEmpty()) {
            Node cur = pq.poll();

            if (!visited[cur.to]) {
                visited[cur.to] = true;
                ans.add(cur);
            }

            for (int i = 0; i < graph[cur.to].size(); i++) {
                Node next = graph[cur.to].get(i);

                if (dist[next.to] > dist[cur.to] + next.cost) {
                    dist[next.to] = dist[cur.to] + next.cost;
                    pq.add(new Node(next.from, next.to, dist[next.to]));
                }
            }
        }
        System.out.println(ans.size());
        for (Node node : ans)
            System.out.println(node.from + " " + node.to);
    }

    static class Node implements Comparable<Node> {
        int from, to, cost;
        
        Node(int start, int end, int cost) {
            this.from = start;
            this.to = end;
            this.cost = cost;
        }
        
        @Override
        public int compareTo(Node n) {
            return this.cost - n.cost;
        }
    }
}
728x90
Comments