Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[9주차] 이지영 #119

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

Merged
baexxbin merged 9 commits into GreatAlgorithm-Study:main from yeongleej:main
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
9 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions BOJ/1000-5000번/JY_2660.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package day1105;

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

public class JY_2660 {

static int INF = Integer.MAX_VALUE;
static int N;
static List<Node>[] g;
static int[] distance;
static class Node implements Comparable<Node> {
int w, dist;
public Node(int w, int dist) {
this.w = w;
this.dist = dist;
}
@Override
public int compareTo(Node other) {
return this.dist - other.dist;
}
}

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());

g = new ArrayList[N+1];
for(int i=1; i<N+1; i++) {
g[i] = new ArrayList<>();
}

while(true) {
st = new StringTokenizer(br.readLine());
int n1 = Integer.parseInt(st.nextToken());
int n2 = Integer.parseInt(st.nextToken());
if(n1 == -1 && n2 == -1) break;
g[n1].add(new Node(n2, 1));
g[n2].add(new Node(n1, 1));
}

int minLevel = Integer.MAX_VALUE;
List<Integer> aList = new ArrayList<>();
for(int i=1; i<N+1; i++) {
distance = new int[N+1];
Arrays.fill(distance, INF);

dijkstra(i);
// System.out.println(i+": "+Arrays.toString(distance));

int level = findLevel();

if(level < minLevel) {
aList = new ArrayList<>();
aList.add(i);
minLevel = level;

} else if (level == minLevel) {
aList.add(i);
}
}

System.out.println(minLevel+" "+aList.size());
for(int i: aList) {
System.out.print(i+" ");
}
System.out.println();

}
public static void dijkstra(int n) {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(n, 0));

distance[n] = 0;

while(!pq.isEmpty()) {
Node now = pq.poll();

if(now.dist > distance[now.w]) continue;

for(Node next: g[now.w]) {
int cost = now.dist + next.dist;
if(cost < distance[next.w]) {
distance[next.w] = cost;
pq.add(new Node(next.w, cost));
}
}
}
}
public static int findLevel() {
int maxL = Integer.MIN_VALUE;
for(int i=1; i<N+1; i++) {
maxL = Math.max(maxL, distance[i]);
}

return maxL;
}

}
66 changes: 66 additions & 0 deletions BOJ/1000-5000번/JY_2908.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package day1106;

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

public class JY_2908 {

static int INF = 15_000_000;
static int FullBit;
static int N;
static int[][] w;
static int[][] dp;

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());

w = new int[N][N];
for(int i=0; i<N; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<N; j++) {
w[i][j] = Integer.parseInt(st.nextToken());
}
}

// 모든 노드를 방문한 비트마스킹 값
FullBit = (1 << N);

dp = new int[N][FullBit];
for(int i=0; i<N; i++) {
Arrays.fill(dp[i], -1);
}

// 0번째 노드부터 시작
System.out.println(dfs(0, 1));
}
public static int dfs(int now, int visited) {
// 모든 정점 방문
if(visited == FullBit-1) {
if(w[now][0] == 0) return INF;
return w[now][0];
}

// 이미 방문한 노드
if(dp[now][visited] != -1) return dp[now][visited];

// 아직 방문 X
dp[now][visited] = INF;

for(int i=0; i<N; i++) {
// i로 가는 것 방문처리
int next = visited | (1 << i);

// i로 가는 경로가 없거나, 이미 방문했다면 continue
if(w[now][i] == 0) continue;
if((visited & (1 << i)) != 0) continue;

dp[now][visited] = Math.min(dp[now][visited], dfs(i, next) + w[now][i]);
}

return dp[now][visited];
}

}
36 changes: 36 additions & 0 deletions BOJ/10001-15000번/JY_13164.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package day1108;

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

public class JY_13164 {

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 K = Integer.parseInt(st.nextToken());

List<Integer> dList = new ArrayList<>();
st = new StringTokenizer(br.readLine());
int n1 = Integer.parseInt(st.nextToken());
for(int i=1; i<N; i++) {
int n2 = Integer.parseInt(st.nextToken());
dList.add(n2 - n1);
n1 = n2;
}

Collections.sort(dList);
// System.out.println(dList);

// 뒤의 (K-1)개의 차이를 없애야 함
int ans = 0;
for(int i=0; i<N-K; i++) {
ans += dList.get(i);
}

System.out.println(ans);
}

}
84 changes: 84 additions & 0 deletions BOJ/10001-15000번/JY_14620.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package day1108;

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

public class JY_14620 {

static int N;
static int[][] g;
static int[] dx = {0, 0, -1, 1};
static int[] dy = {-1, 1, 0, 0};
static boolean[][] visited;
static int ans;

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());

g = new int[N][N];

for(int i=0; i<N; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<N; j++) {
g[i][j] = Integer.parseInt(st.nextToken());
}
}

ans = Integer.MAX_VALUE;
visited = new boolean[N][N];
findSpot(0, 0, 0, 0);

System.out.println(ans);

}
public static boolean inRange(int x, int y) {
return x>=0 && x<N && y>=0 && y<N;
}
public static boolean isOk(int x, int y) {
if(visited[x][y]) return false;
for(int i=0; i<4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(visited[nx][ny]) return false;
}

return true;
}
public static void findSpot(int x, int y, int depth, int cost) {
if(depth == 3) {
ans = Math.min(ans, cost);
return;
}

for(int i=1; i<N-1; i++) {
for(int j=1; j<N-1; j++) {
if(x==i && y==j) continue;
if(!isOk(i, j)) continue;

int c = g[i][j];
visited[i][j] = true;
for(int d=0; d<4; d++) {
int nx = i + dx[d];
int ny = j + dy[d];
visited[nx][ny] = true;
c += g[nx][ny];
}

findSpot(i, j, depth+1, cost+c);

visited[i][j] = false;
for(int d=0; d<4; d++) {
int nx = i + dx[d];
int ny = j + dy[d];
visited[nx][ny] = false;
}

}
}

}

}
Loading

AltStyle によって変換されたページ (->オリジナル) /