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
82 changes: 82 additions & 0 deletions kimsuhyeok/BOJ_11437_LCA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
* 세그먼트 트리
* 연속된 구간의 데이터의 합을 가장 빠르고 간단하게 구할 수 있는 트리
* 루트 노드에 모든 원소를 더한 값이 들어가는 방식
*/
public class Main {

static List<Integer>[] list;
static int[] parent, depth;
static int N,M;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

N = Integer.parseInt(br.readLine());

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

for(int i=0;i<N-1;i++){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

list[a].add(b);
list[b].add(a);
}

init(1,1,0);

M = Integer.parseInt(br.readLine());
for(int i=0;i<M;i++){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
System.out.println(LCA(a,b));
}

}

public static void init(int cur, int h, int pa){
depth[cur] = h;
parent[cur] = pa;
for(int next: list[cur]){
if(next != pa){
init(next, h+1, cur);
}
}
}

public 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;
}

}
110 changes: 110 additions & 0 deletions kimsuhyeok/BOJ_11779_최소비용구하기2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

/**
* 도시의 개수 : N
* 버스의 개수 : M
*
*
*/
public class Main {

static class Node implements Comparable<Node>{
int to;
int weight;
public Node(int to, int weight){
this.to = to;
this.weight = weight;
}

@Override
public int compareTo(Node o) {
return this.weight-o.weight;
}
}
static int N,M;
static int[] dist; //거리 배열
static int[] prev; //직전 도시 저장할 배열
static boolean[] visited;
static List<Node>[] list;
static int start, end;
static final int INF = 1000000001;

public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

N = Integer.parseInt(br.readLine());
M = Integer.parseInt(br.readLine());

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

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());
list[from].add(new Node(to, weight));
}

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

dist = new int[N+1];
prev = new int[N+1];
visited = new boolean[N+1];
Arrays.fill(dist, INF);

dijkstra();

System.out.println(dist[end]);

List<Integer> routes = new ArrayList<>();

//역추적
int trace = end;

while(trace!=0){
routes.add(trace);
trace = prev[trace];
}

System.out.println(routes.size());

for(int i= routes.size()-1;i>=0;i--){
System.out.print(routes.get(i)+" ");
}

}

public static void dijkstra(){
PriorityQueue<Node> queue = new PriorityQueue<>();
queue.offer(new Node(start,0));
dist[start] = 0;
prev[start] = 0;

while(!queue.isEmpty()){
Node cur = queue.poll();

if(!visited[cur.to]){
visited[cur.to] = true;
}
else continue;

for(int i=0;i<list[cur.to].size();i++){
Node next = list[cur.to].get(i);
if(dist[next.to]>dist[cur.to]+next.weight){
dist[next.to] = dist[cur.to]+next.weight;
queue.offer(new Node(next.to, dist[next.to]));
prev[next.to] = cur.to;
}
}
}
}

}
62 changes: 62 additions & 0 deletions kimsuhyeok/BOJ_1507_궁금한민호.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
* 1. 이미 최단거리임
* 그렇다면 우리는 플로이드 워셜 알고리즘을 수행한 배열을 이용해서 플로이드 워셜 알고리즘 수행 전 배열을 찾아야 함
*
*/
public class Main {

static int N;
static int[][] map;
static int[][] copy;

public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

N = Integer.parseInt(br.readLine());
map = new int[N][N];
copy = new int[N][N];

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

for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
for(int k=0;k<N;k++){
if(i==j||i==k||j==k)continue;

if(map[j][k] == map[j][i]+map[i][k]){
copy[j][k]=0;
}

//말이 안되는 경우
if(map[j][k] > map[j][i] + map[i][k]){
System.out.println(-1);
System.exit(0);
}
}
}
}

int answer = 0;

for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
answer += copy[i][j];
}
}

System.out.println(answer/2);

}

}