Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
59 changes: 59 additions & 0 deletions kwakmirae/BOJ_10868_최소값.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package kwakmirae;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ_10868_최소값 {

static int N, M, LEAF;
static long tree[];

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());
int height = (int) Math.ceil(Math.log(N)/Math.log(2)) +1;
int size = (int) Math.pow(2, height);

tree = new long[size];
LEAF = (int)Math.pow(2, height-1);
for(int i=LEAF; i<LEAF+N; i++) {
tree[i] = Long.parseLong(br.readLine());
}
init(1);

StringBuilder sb = new StringBuilder();
for(int i=0; i<M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
sb.append(search(1, 1, LEAF, a, b)).append("\n");
}
System.out.println(sb);
}

private static long search(int node, int start, int end, int left, int right) {
if(end < left || start > right) return Long.MAX_VALUE;

if(left <= start && end <= right) {
return tree[node];
}

int mid = (start + end)/2;
return Math.min(search(node*2, start, mid, left, right), search(node*2+1, mid+1, end, left, right));
}

public static long init(int idx) {
if(idx >= LEAF) {
return tree[idx];
}

return tree[idx] = Math.min(init(idx*2), init(idx*2+1));
}


}
76 changes: 76 additions & 0 deletions kwakmirae/BOJ_11437_LCA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package kwakmirae;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class BOJ_11437_LCA {

private static List<Integer>[] list;
private static int[] parent;
private static int[] depth;

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
list = new ArrayList[N+1];
StringTokenizer st;
for(int i=0; i<=N; i++) {
list[i] = new ArrayList<Integer>();
}
for (int i = 0; i < N-1; i++) {
st = new StringTokenizer(br.readLine());
int v1 = Integer.parseInt(st.nextToken());
int v2 = Integer.parseInt(st.nextToken());
list[v1].add(v2);
list[v2].add(v1);
}

parent = new int[N+1];
depth = new int[N+1];
init(1, 1, 0);

int M = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for(int i=0; i<M; i++) {
st = new StringTokenizer(br.readLine());
int v1 = Integer.parseInt(st.nextToken());
int v2 = Integer.parseInt(st.nextToken());
sb.append(lca(v1, v2)).append("\n");
}
System.out.println(sb);
}

private static void init(int v, int d, int pa) {
depth[v] = d;
parent[v] = pa;

for(int child: list[v]) {
if(child == pa) continue;
init(child, d+1, v);
}
}

private static int lca(int a, int b) {
int ah = depth[a];
int bh = depth[b];

while(ah > bh) {
a = parent[a];
ah--;
}
while(bh > ah) {
b = parent[b];
bh--;
}
while(a!=b) {
a = parent[a];
b = parent[b];
}
return a;
}
}
81 changes: 81 additions & 0 deletions kwakmirae/BOJ_11779_최소비용구하기2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package kwakmirae;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class BOJ_11779_최소비용구하기2 {

static class Node implements Comparable<Node>{
int vertex, weight;
Node next;
public Node(int vertex, int weight, Node next) {
this.vertex = vertex;
this.weight = weight;
this.next = next;
}
@Override
public int compareTo(Node o) {
return weight - o.weight;
}
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int M = Integer.parseInt(br.readLine());

StringTokenizer st;
Node[] adjList = new Node[N+1];
for(int i=0; i<M; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
adjList[from] = new Node(to, weight, adjList[from]);
}

st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());

int[] distance = new int[N+1];
int[] selected = new int[N+1];
Arrays.fill(distance, Integer.MAX_VALUE);
distance[start] = 0;
selected[start] = 0;
PriorityQueue<Node> queue = new PriorityQueue<Node>();
queue.add(new Node(start, 0, null));
while(true) {
// 1. 최소 비용 경로를 가지는 vertex 찾기
Node curr = queue.poll();

// 2. 방문 처리하기
if(distance[curr.vertex] < curr.weight) continue;
if(curr.vertex == end) break;

// 3. 해당 vertex와 연결된 edge를 보면서 최소 비용 갱신하기
for(Node temp=adjList[curr.vertex]; temp != null; temp = temp.next) {
if(distance[temp.vertex] > distance[curr.vertex]+temp.weight) {
distance[temp.vertex] = distance[curr.vertex]+temp.weight;
selected[temp.vertex] = curr.vertex;
queue.add(new Node(temp.vertex, distance[temp.vertex], null));
}
}
}

StringBuilder sb = new StringBuilder();
int idx = end, cnt = 0;
while(idx != 0) {
sb.insert(0, idx+" ");
idx = selected[idx];
cnt++;
}
System.out.println(distance[end]);
System.out.println(cnt);
System.out.println(sb);
}
}
51 changes: 51 additions & 0 deletions kwakmirae/BOJ_1507_궁금한민호.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package kwakmirae;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ_1507_궁금한민호 {

private static int N, distance[][], result[][];

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
StringTokenizer st;

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

for(int k=0; k<N; k++) {
for(int i=0; i<N; i++) {
if(k==i) continue;
for(int j=0; j<N; j++) {
if(j==k || i==j) continue;
if(distance[i][j] == distance[i][k] + distance[k][j]) {
result[i][j] = 0;
}
if(distance[i][j] > distance[i][k] + distance[k][j]) {
System.out.println(-1);
return;
}
}
}
}

int sum = 0;
for(int i=0; i<N; i++) {
for(int j=i+1; j<N; j++) {
sum += result[i][j];
}
}
System.out.println(sum);
}
}