Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

이태호 / 10월 1주차 / 목 #259

Merged
merged 3 commits into from
Oct 22, 2023
Merged

Conversation

leetaggg
Copy link
Member

@leetaggg leetaggg commented Oct 5, 2023


🎈boj 2660 - 회장뽑기


🗨 해결방법 :

플로이드 워셜로 최단 경로를 구하고 각 노드 별 최대 거리와 그 중 최솟값을 구해서 비교하였습니다.

📝메모 :


✔코드 :

import java.io.*;
import java.util.*;

public class boj2660 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[][] dp = new int[n + 1][n + 1];

        for (int i = 1; i <= n; i++) {
            Arrays.fill(dp[i], 100000000);
            dp[i][i] = 0;
        }

        while (true) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int p1 = Integer.parseInt(st.nextToken());
            int p2 = Integer.parseInt(st.nextToken());

            if (p1 == -1 && p2 == -1) break;

            dp[p1][p2] = dp[p2][p1] = 1;
        }

        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    if (dp[i][j] > dp[i][k] + dp[k][j]) {
                        dp[i][j] = dp[i][k] + dp[k][j];
                    }
                }
            }
        }

        int[] score = new int[n + 1];
        int minValue = 100000000;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (score[i] < dp[i][j]) {
                    score[i] = dp[i][j];
                }
            }
            if (minValue > score[i]) {
                minValue = score[i];
            }
        }

        StringBuilder sb = new StringBuilder();
        int cnt = 0;
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            if (score[i] == minValue) {
                cnt++;
                list.add(i);
            }
        }

        sb.append(minValue).append(" ").append(cnt).append("\n");
        for (int i : list) {
            sb.append(i).append(" ");
        }

        System.out.println(sb);
    }
}


🎈boj 15817 - 배수 공사


🗨 해결방법 :

갯수가 정해져 있는 냅색 풀이로 구현하였습니다. 2624번 동전 바꿔주기 문제와 매우 유사합니다.

📝메모 :


✔코드 :

import java.io.*;
import java.util.*;

public class boj15817 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int x = Integer.parseInt(st.nextToken());
        int[] pipe = new int[x + 1];
        pipe[0] = 1;
        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int length = Integer.parseInt(st.nextToken());
            int cnt = Integer.parseInt(st.nextToken());
            for (int j = x; j >= length; j--) {
                for (int k = 1; k <= cnt; k++) {
                    if(j - length * k >= 0){
                        pipe[j] += pipe[j - length * k];
                    }
                }
            }
        }

        System.out.println(pipe[x]);
    }
}


🎈boj 18427 - 함께 블록 쌓기


🗨 해결방법 :

냅색 풀이로 구현하였습니다.

📝메모 :


✔코드 :

import java.io.*;
import java.util.*;

public class boj18427 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        st.nextToken();
        int h = Integer.parseInt(st.nextToken());

        int[] dp = new int[h + 1];

        List<Integer>[] block = new ArrayList[n + 1];

        for (int i = 1; i <= n; i++) {
            block[i] = new ArrayList<>();
            st = new StringTokenizer(br.readLine());
            while (st.hasMoreTokens()){
                block[i].add(Integer.parseInt(st.nextToken()));
            }
        }

        dp[0] = 1;

        for (int i = 1; i <= n; i++) {
            for (int j = h; j >= 0; j--) {
                for (int k : block[i]) {
                    if(j - k >= 0){
                        dp[j] += dp[j - k];
                        dp[j] %= 10007;
                    }
                }
            }
        }

        System.out.println(dp[h]);
    }
}

@leetaggg leetaggg merged commit 554815f into SSAFY-10th-Seoul17:main Oct 22, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants