[BOJ] 보μ μ€κΈ°(2001)
[λ°±μ€(BOJ)] 2001_보μ μ€κΈ° JAVA
λ¬Έμ : BOJ_2001λ² λ³΄μ μ€κΈ°
λ¬Έμ μ€λͺ
μ΅λ 100κ°μ μ¬μ΄ μκ³ κ° μ¬λ§λ€ μ΅λ ν κ°μ§μ μλ°©ν₯ λ€λ¦¬λ‘ μ°κ²°λμ΄ μμ΅λλ€.
κ° μ¬λ§λ€ 보μμ΄ μλ μ¬κ³Ό μλ μ¬μ΄ μκ³ , λ€λ¦¬λ§λ€ 보μμ 견λ μ μλ μκ° μ ν΄μ Έμμ΅λλ€.
1λ² μ¬μμλΆν° μμνμ¬ 1λ² μ¬μΌλ‘ λ€μ λμμμΌ νλλ°, μ΄λ μ΅λν λͺ¨μ μ μλ 보μμ μλ₯Ό ꡬν΄μΌ ν©λλ€.
λ°©λ¬Ένλ λ€λ¦¬μ μ¬μ λ€μ λ°©λ¬Έν μ μκ³ μ¬μ μλ 보μμ κ°μ Έκ°κ±°λ κ°μ Έκ°μ§ μμ μλ μμ΅λλ€.
Solution
λΉνΈ λ§μ€νΉ & bfs
κ° μ¬μ μ§λκ° λ λͺ κ°μ 보μμ λ€κ³ μμ΄μΌ νλμ§, λ ν΄λΉ μ¬μμ 보μμ κ°μ Έκ°λ κ² μ 리νμ§ μλμ§λ₯Ό μ μ μκΈ° λλ¬Έμ λͺ¨λ νμμ΄ νμν©λλ€.
μ΄λ₯Ό μν΄ λΉνΈ λ§μ€νΉμ΄ νμνλ°, κ° μ¬μ μ΅λ 100κ°μ΄κ³ 보μμ μλ μ΅λ 14κ°μ΄λ―λ‘ 100 * 16384(1 << 14)μ 2μ€ λ°°μ΄μ λ§λ€μ΄ ν μ¬μμ λ€κ³ μ§λκ° μ μλ λͺ¨λ 보μμ κ²½μ°μ μλ₯Ό νμΈν μ μμ΅λλ€.
μ μ κ²½μ°μ 1λ² μ¬μμ 보μμ κ°μ§ μκ³ μμνλ κ²½μ°
μ 1λ² μ¬μ 보μμ΄ μλ€λ©΄ 보μμ 1κ° κ°κ³ μμνλ κ²½μ°
λ κ°μ§μ κ²½μ°λ₯Ό Queueμ λ£κ³ bfsλ₯Ό μ¬μ©νμ΅λλ€.
λͺ¨λ bfsκ° λλ ν μ¬μ΄ 1μΌ λ λͺ¨λ 보μμ κ²½μ°μ μ μ€ κ°μ₯ λ§μ 보μμ μκ° μ λ΅μ΄ λ©λλ€.
Description
μ¬μ μΈλ±μ€, 보μ μ, 보μμ λ°λ₯Έ λΉνΈ λ§μ€νΉ μ
μΈ κ°μ§λ₯Ό μ μ₯ν PairλΌλ μλ£νμ λ§λ€μ΄ bfsμ νμ©νμ΅λλ€.- λ€λ¦¬μ κ²½μ°μ
ArrayList
λ₯Ό μ¬μ©νλ©΄ λ°λ³΅λ¬Έμ μ€μΌ μ μμ§λ§ μ¬μ μ΅λ κ°μκ° μκΈ° λλ¬Έμ 볡μ‘μ±μ μ€μ΄κΈ° μν΄ 2μ€ λ°°μ΄μ μ¬μ©νμ΅λλ€. - 보μμ κ²½μ°μ 0~μ΅λ 보μμ μκΉμ§ λλ²λ₯Ό λΆμ¬ν΄μ λΉνΈ λ§μ€νΉμ νμ©νμ΅λλ€.
μμ€ μ½λ
import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
class Pair{
int idx;
int cnt;
int bit;
public Pair(int idx, int cnt, int bit) {
this.idx = idx;
this.cnt = cnt;
this.bit = bit;
}
}
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int n, m, k;
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
int last = (1 << k);
int[][] map = new int[n + 1][1 << k];
int[][] bridge = new int[n+1][n+1];
int[] jewel = new int[n+1];
Arrays.fill(jewel, -1);
for (int i = 0; i < k; i++) {
int idx = Integer.parseInt(br.readLine());
jewel[idx] = i;
}
for (int i = 0; i < m; i++) {
int a, b, c;
st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
bridge[a][b] = c;
bridge[b][a] = c;
}
int tempBitNum = 0;
int tempCnt = 0;
if (jewel[1]!=-1){
tempBitNum |= (1 << jewel[1]);
tempCnt++;
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <last ; j++) {
map[i][j] = -1;
}
}
map[1][tempBitNum] = tempCnt;
Queue<Pair> q = new LinkedList<>();
q.add(new Pair(1, tempCnt, tempBitNum));
q.add(new Pair(1, 0, 0));
while (!q.isEmpty()) {
Pair poll = q.poll();
int idx = poll.idx;
int cnt = poll.cnt;
int bitNum = poll.bit;
for (int i = 1; i <= n ; i++) {
if(i==idx || bridge[idx][i]==0) continue;
if (bridge[idx][i] < cnt) continue;
if (map[i][bitNum] ==-1){
map[i][bitNum] = cnt;
q.add(new Pair(i, cnt, bitNum));
}
int nextCnt = cnt;
int nextBitNum = bitNum;
if (jewel[i]!=-1){
if ((nextBitNum & (1 << jewel[i])) == 0) {
nextCnt++;
nextBitNum |= (1 << jewel[i]);
}
}
if (map[i][nextBitNum]!=-1) continue;
map[i][nextBitNum] = nextCnt;
q.add(new Pair(i, nextCnt, nextBitNum));
}
}
int res = 0;
for (int i = 0; i < last; i++) {
res = Math.max(res, map[1][i]);
}
bw.write(res+"\n");
bw.flush();
br.close();
bw.close();
}
}