반응형
05-15 00:00
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
관리 메뉴

개발하는 고라니

[백준] 14938번 : 서강그라운드 본문

Programming/백준

[백준] 14938번 : 서강그라운드

조용한고라니 2021. 3. 6. 19:45
반응형
 

14938번: 서강그라운드

예은이는 요즘 가장 인기가 있는 게임 서강그라운드를 즐기고 있다. 서강그라운드는 여러 지역중 하나의 지역에 낙하산을 타고 낙하하여, 그 지역에 떨어져 있는 아이템들을 이용해 서바이벌을

www.acmicpc.net


모든 정점에 대하여 다른 정점으로의 최단 경로를 구하는 문제이므로 n * Djikstra / Floyd Warshall을 사용할 수 있겠다.

 

[Dijkstra]

# Code </>

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

public class Main {

    static class Edge implements Comparable<Edge> {
        int tg, dist;
        public Edge(int t, int d){
            tg=t;
            dist=d;
        }

        @Override
        public int compareTo(Edge o) {
            return dist - o.dist;
        }
    }

    static final int INF = 1000000000;
    static List<Edge>[] list = new ArrayList[101];
    static int[] item = new int[101], result = new int[101];
    static int[][] d = new int[101][101];
    static int n, m, r;

    //1. N * Dijkstra
    static void Dijkstra(int s){

        Queue<Edge> Q = new PriorityQueue<>();
        d[s][s] = 0;

        Q.add(new Edge(s, 0));

        while(!Q.isEmpty()){
            Edge e = Q.poll();
            int cur = e.tg;
            int dist = e.dist;

            if(d[cur][s] < dist) continue;

            result[s] += item[cur];

            for(Edge next:list[cur]){
                int nDist = dist + next.dist;

                if(d[next.tg][s] > nDist && nDist <= m){
                    d[next.tg][s] = nDist;
                    Q.add(new Edge(next.tg, nDist));
                }
            }
        }
    }

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

        n = Integer.parseInt(st.nextToken()); //지역개수
        m = Integer.parseInt(st.nextToken()); //수색범위
        r = Integer.parseInt(st.nextToken()); //길의개수

        st = new StringTokenizer(br.readLine());
        for(int i=1; i<=n; i++) {
            Arrays.fill(d[i], INF);
            list[i] = new ArrayList<>();
            item[i] = Integer.parseInt(st.nextToken());
        }

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

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

            list[h].add(new Edge(tg, d));
            list[tg].add(new Edge(h, d));
        }

        int max = 0;
        for(int i=1; i<=n; i++) {
            Dijkstra(i);
            max = Math.max(max, result[i]);
        }
        System.out.println(max);
    }
}

 

반응형

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

[백준] 9328번 : 열쇠  (0) 2021.03.09
[백준] 1525번 : 퍼즐  (0) 2021.03.07
[백준] 1944번 : 복제 로봇  (0) 2021.03.06
[백준] 1507번 : 궁금한 민호  (0) 2021.03.06
[백준] 1486번 : 등산  (0) 2021.03.06
Comments