14938. 서강그라운드 (Java)

2023. 11. 29. 19:44·Algorithm & Data Structures/BOJ

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)  (1) 2023.11.14
'Algorithm & Data Structures/BOJ' 카테고리의 다른 글
  • 2096. 내려가기 (Java)
  • 2448. 별찍기 - 11 (Java)
  • 12851. 숨바꼭질2 (Java)
  • 1043. 거짓말 (Java)
Geisha
Geisha
개발 일기
  • Geisha
    Geisha
    Geisha
  • 전체
    오늘
    어제
    • 분류 전체보기 (335)
      • Algorithm & Data Structures (253)
        • BOJ (111)
        • SWEA (1)
        • Programers (137)
        • Data Structures (3)
      • DB (29)
        • SQL (23)
        • RDBMS (2)
      • Java (1)
        • Class (1)
      • Spring (5)
        • Spring MVC (1)
        • Annotations (1)
      • CS (36)
        • 운영체제 (13)
        • 네트워크 (5)
      • Tool (6)
        • Git (5)
        • AWS (1)
      • Project (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    동적계획법
    BFS
    프로그래머스
    Stack
    구현
    dp
    SQL
    다익스트라
    programmers
    경로압축
    골드
    PriorityQueue
    dfs
    DynamicProgramming
    자바
    백트래킹
    백준
    Dijkstra
    알고리즘
    Union-Find
    binarySearch
    이분탐색
    algorithm
    Java
    전위순회
    스택
    baekjoon
    후위순회
    투포인터
    유니온파인드
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
Geisha
14938. 서강그라운드 (Java)
상단으로

티스토리툴바