본문 바로가기

Problem Solving/백준BOJ

[백준BOJ] 14221번 편의점.java

728x90
반응형
SMALL

 

백준 저지에서 편의점을 자바를 통해 풀어 보았다. 

 

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

 

14221번: 편의점

처음 줄에는 정점의 개수 n, 간선의 개수 m이 주어진다.(2 ≤ n ≤ 5,000, 1 ≤ m ≤ 100,000) 다음 m줄에는 a,b,c가 주어지는데 이는 a, b를 잇는 간선의 거리가 c라는 것이다.(1 ≤ a, b ≤ n, 1 ≤ c ≤ 10,000)

www.acmicpc.net

 

14221번 편의점.java

 

GitHub - tomy9729/Algorithm: 🐗 내가 직접 작성한 내 코드 🐗

🐗 내가 직접 작성한 내 코드 🐗. Contribute to tomy9729/Algorithm development by creating an account on GitHub.

github.com

 

14221번 편의점

문제

영선이는 이사할 일이 생겨 집을 알아보고 있다. 영선이는 혼자 살기 때문에, 편의점에서 대충 때울 때가 많아, 집을 고르는 기준을 편의점과의 거리가 가장 가까운 곳으로 하려한다.
영선이가 이사할 도시는 정점과 간선으로 표현할 수 있는데, 이사가려 하는 집의 후보들과 편의점은 정점들 위에 있다.
영선이는 캠프 강사 준비로 바쁘므로, 대신하여 집을 골라주자. 만약 거리가 같은 지점이 여러 개라면 정점 번호가 가장 낮은 곳으로 골라주자.

설명

다익스트라 알고리즘을 활용해서 푼다.

 

집 후보들을 출발점으로 하여 다익스트라를 통해 각 편의점에 대한 거리를 구한다. 각 거리들과 집이 있는 정점번호들을 비교하여 문제의 조건에 맞는 답을 도출한다. 

코드

//14221번 편의점.java
package boj20211213;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class 편의점_14221 {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static StringBuilder sb = new StringBuilder();
    static final int maxNum = Integer.MAX_VALUE;
    
    public static void main(String[] args) throws IOException {
    	st = new StringTokenizer(br.readLine()," ");
		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());
		
		ArrayList<ArrayList<edge>> graph = new ArrayList();
		for(int i=0;i<=N;i++) {
			graph.add(new ArrayList<edge>());
		}
		
		for(int i=0;i<M;i++) {
			st = new StringTokenizer(br.readLine()," ");
			int from = Integer.parseInt(st.nextToken());
			int to = Integer.parseInt(st.nextToken());
			int value = Integer.parseInt(st.nextToken());
			graph.get(from).add(new edge(to, value));
			graph.get(to).add(new edge(from, value));
		}
		
		st = new StringTokenizer(br.readLine()," ");
		int houseCount = Integer.parseInt(st.nextToken());
		int[] house = new int[houseCount];
		int cuCount = Integer.parseInt(st.nextToken());
		int[] cu = new int[cuCount];
		
		st = new StringTokenizer(br.readLine()," ");
		for(int i=0;i<houseCount;i++) {
			house[i] = Integer.parseInt(st.nextToken());
		}
		
		st = new StringTokenizer(br.readLine()," ");
		for(int i=0;i<cuCount;i++) {
			cu[i] = Integer.parseInt(st.nextToken());
		}
		
		int from=0;
		int dis=Integer.MAX_VALUE;
		
		for(int i=0;i<houseCount;i++) {
			int[] root = new int[N+1];
			Arrays.fill(root,Integer.MAX_VALUE);
			dijkstra(graph, root, house[i]);
			for(int j=0;j<cuCount;j++) {
				//도착 편의점 cu[j]
				//출발 집 house[i]
				if(dis > root[cu[j]] || (dis==root[cu[j]] && from > house[i])) {
					from = house[i];
					dis = root[cu[j]];
				}
			}
		}
		System.out.println(from);
	}
    
    static void dijkstra(ArrayList<ArrayList<edge>> graph,int[] root, int start) {
		root[start]=0;
		PriorityQueue<edge> pq = new PriorityQueue<>();
		pq.add(new edge(start,0));
		
		while(!pq.isEmpty()) {
			edge now = pq.poll();
			if(root[now.to] < now.value)continue;
			
			for(int i=0;i<graph.get(now.to).size();i++) {
				edge next = graph.get(now.to).get(i);
				if(now.value+next.value < root[next.to]) {
					root[next.to] = now.value+next.value;
					pq.add(new edge(next.to,root[next.to]));
				}
			}
		}
	}
    static class edge implements Comparable<edge>{
    	int to;
    	int value;
    	edge(int to, int value){
    		this.to=to;
    		this.value=value;
    	}
		@Override
		public int compareTo(edge o) {
			return Integer.compare(this.value, o.value);
		}
    }
}
728x90
반응형
SMALL