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

알고리즘 공부방

백준 14938 서강그라운드(JAVA) 본문

알고리즘

백준 14938 서강그라운드(JAVA)

head89 2022. 7. 3. 23:00

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

 

14938번: 서강그라운드

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

www.acmicpc.net

문제풀이 방법

먼저 다익스트라 알고리즘을 공부하고 싶다면 아래의 문제를 한번 풀어보는 것을 추천한다.

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

 

1753번: 최단경로

첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1 ≤ V ≤ 20,000, 1 ≤ E ≤ 300,000) 모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. 둘째 줄에는 시작 정점의 번호 K(1 ≤ K ≤ V)가

www.acmicpc.net

다익스트라 알고리즘을 알고있다는 가정하에 설명을 하자면, 시작정점이 있지 않기 때문에 모든 정점에서 시작했을 때의 아이템의 최대 개수를 알아야한다.

for(int i =1;i<=n;i++) {
			visited = new boolean[n+1];
			Arrays.fill(w, INF);
			int k = Dekestr(i, w);
			if(max<k) max = k;
		}

위의 코드가 모든 정점을 시작정점으로 하여 최대 값을 찾는 부분이다.

그리고 다익스트라 알고리즘에서 다익스트라 알고리즘이 끝나고 각 정점의 가중치를 수색범위 M과 비교하여 M이하인 정점들의 아이템을 더하는 방법을 사용했다.

for(int i =1;i<=n;i++) {
			if(w[i] <= m) {
				item_value += value[i];
			}
		}

 

 

전체 코드

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {	
	static List<Node>[] list;
	static boolean visited[];
	static int n;
	static int m;
	static int r;
	static int value[];
	static int INF = Integer.MAX_VALUE;
	static class Node implements Comparable<Node>{
		int next = 0;
		int distance= 0;
		public Node(int next, int distance) {
			this.next = next;
			this.distance = distance;
		}
		@Override
		public int compareTo(Node o) {
			return this.distance - o.distance;
		}
		
	}
	static int Dekestr(int start, int[]w){
		PriorityQueue<Node> queue = new PriorityQueue<>();
		queue.add(new Node(start,0));
		w[start] = 0;
		int item_value = 0;
		while(!queue.isEmpty()) {
			Node node = queue.poll();
			int v = node.next;
			if(visited[v]) continue;
			visited[v] = true;
			for(Node next: list[v]) {
				if(w[v] + next.distance > m) continue;
				if(w[next.next]> w[v] + next.distance) {
					w[next.next] = w[v] + next.distance;
					queue.add(new Node(next.next, w[next.next]));
				}
			}
		}
		for(int i =1;i<=n;i++) {
			if(w[i] <= m) {
				item_value += value[i];
			}
		}
		return item_value;
	}
	
	public static void main(String[] args) throws NumberFormatException, 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());
		list = new ArrayList[n+1];
		visited = new boolean[n+1];
		int[] w = new int[n+1];
		int[] w1 = new int[n+1];
		for(int i =1; i<=n;i++) {
			list[i] = new ArrayList<>();
		}
		value = new int[n + 1];
		StringTokenizer st1 = new StringTokenizer(br.readLine() + " ");
		for(int i=1;i<=n;i++) {
			value[i] = Integer.parseInt(st1.nextToken());
		}
		for(int i =0; i<r;i++) {
			StringTokenizer st2 = new StringTokenizer(br.readLine() + " ");
			int a = Integer.parseInt(st2.nextToken());
			int b = Integer.parseInt(st2.nextToken());
			int l = Integer.parseInt(st2.nextToken());
			list[a].add(new Node(b,l));
			list[b].add(new Node(a,l));
		}
		int max = 0;
		for(int i =1;i<=n;i++) {
			visited = new boolean[n+1];
			Arrays.fill(w, INF);
			int k = Dekestr(i, w);
			if(max<k) max = k;
		}
		System.out.print(max);
	}
}

'알고리즘' 카테고리의 다른 글

백준 26153 로하의 농사  (0) 2022.12.15
백준 6087 레이저 통신(JAVA)  (0) 2022.12.14
백준 1744 수 묶기(JAVA)  (0) 2022.12.12
백준 1719 택배(JAVA)  (0) 2022.12.10
백준 2206 벽 부수고 이동하기(JAVA)  (0) 2022.07.01