CodeForce707B - Bakery (배열 그래프)
이 문제는 storage들 중에서 가장 가까운 도시를 찾는 문제이다.
그래프의 정보를 배열로 저장하여 storage와의 엣지가 가장 작은 node를 찾아낸다.
코드는 다음과 같다.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import java.util.Scanner; /** * Created by kjm81 on 2017-04-07. */ public class CodeForce707B { static int[] u = new int[100005]; static int[] v = new int[100005]; static int[] l = new int[100005]; static int[] storages = new int[100005]; public static void main(String[] args) { int n, m, k, x, y, z; Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); k = sc.nextInt(); for (int i = 0; i < m; i++) { u[i] = sc.nextInt(); v[i] = sc.nextInt(); l[i] = sc.nextInt(); } for (int i = 0; i < k; i++) { z = sc.nextInt(); storages[z] = 1; } int min = 2000000000; for (int i = 0; i < m; i++) { if (storages[u[i]] == 1 && storages[v[i]] == 0) { if (min > l[i]) { min = l[i]; } } if (storages[v[i]] == 1 && storages[u[i]] == 0) { if (min > l[i]) { min = l[i]; } } } if (min == 2000000000) { System.out.print(-1); }else { System.out.print(min); } } } |