DFS를 통해 풀었으며 DFS로 완전탐색을 하고 이문제를 풀려면
boolean 배열을 2개 써야 했다
이외에도 다익스트라, 플루이드와샬, BFS등 모든 탐색기법이 가능했다.
package Boj;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
class Node{
int end;
int val;
public Node(int end, int val) {
this.end = end;
this.val = val;
}
}
public class b14938 {
static int N, M, R, ans, answerMax=Integer.MIN_VALUE;
static int[] item;
static ArrayList<Node>[] list;
static boolean[] isVisited;
static boolean[] checked;
public static void DFS(int sum,int now){
for(Node next : list[now]){
if (!isVisited[next.end] && sum+next.val<=M){
isVisited[next.end] = true;
if(!checked[next.end]) {
checked[next.end]=true;
ans += item[next.end];
}
DFS(sum+next.val, next.end);
isVisited[next.end] = false;
}
}
return;
}
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());
item = new int[N+1];
list = new ArrayList[N+1];
st= new StringTokenizer(br.readLine());
for(int i = 1; i<=N; i++) {
item[i] = Integer.parseInt(st.nextToken());
}
for(int i = 0; i<=N; i++) {
list[i] = new ArrayList<Node>();
}
for(int i = 0 ; i < R ; i++){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int l = Integer.parseInt(st.nextToken());
list[a].add(new Node(b,l));
list[b].add(new Node(a,l));
}
for(int i = 1; i <= N; i++){
isVisited = new boolean[N+1];
checked = new boolean[N+1];
ans = item[i];
checked[i]=true;
DFS(0,i);
if(answerMax<ans) answerMax = ans;
}
System.out.println(answerMax);
}
}
'Algorithm & Data Structures > BOJ' 카테고리의 다른 글
2096. 내려가기 (Java) (0) | 2023.12.18 |
---|---|
2448. 별찍기 - 11 (Java) (0) | 2023.12.13 |
12851. 숨바꼭질2 (Java) (2) | 2023.11.28 |
1043. 거짓말 (Java) (0) | 2023.11.15 |
2638. 치즈 (Java) (0) | 2023.11.14 |