Skip to content

Commit 3d41d7f

Browse files
committed
Merge branch 'week7'
2 parents 9954f50 + 5e97feb commit 3d41d7f

7 files changed

+362
-0
lines changed

week7/BOJ_11404(플로이드).java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static final int MAX = 10_000_001;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
int n = Integer.parseInt(br.readLine());
11+
int m = Integer.parseInt(br.readLine());
12+
int[][] graph = new int[n+1][n+1];
13+
for(int i=1; i<=n; i++) {
14+
for(int j=1; j<=n; j++) {
15+
if(i == j) {
16+
graph[i][j] = 0;
17+
}else {
18+
graph[i][j] = MAX;
19+
}
20+
}
21+
}
22+
for(int i=0; i<m; i++) {
23+
StringTokenizer st = new StringTokenizer(br.readLine());
24+
int a = Integer.parseInt(st.nextToken());
25+
int b = Integer.parseInt(st.nextToken());
26+
int c = Integer.parseInt(st.nextToken());
27+
graph[a][b] = Math.min(graph[a][b], c);
28+
}
29+
for(int k=1; k<=n; k++) {
30+
for(int i=1; i<=n; i++) {
31+
for(int j=1; j<=n; j++) {
32+
graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
33+
}
34+
}
35+
}
36+
StringBuffer sb = new StringBuffer();
37+
for(int i=1; i<=n; i++) {
38+
for(int j=1; j<=n; j++) {
39+
sb.append((graph[i][j] == MAX) ? 0 : graph[i][j]).append(' ');
40+
}
41+
sb.append('\n');
42+
}
43+
System.out.print(sb);
44+
}
45+
}

week7/BOJ_11657(타임머신).java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static int city;
7+
public static List<List<int[]>> graph;
8+
public static long[] dist;
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
city = Integer.parseInt(st.nextToken());
14+
int bus = Integer.parseInt(st.nextToken());
15+
graph = new ArrayList<>();
16+
dist = new long[city+1];
17+
for(int i=0; i<=city; i++) {
18+
graph.add(new ArrayList<>());
19+
dist[i] = Long.MAX_VALUE;
20+
}
21+
for(int i=0; i<bus; i++) {
22+
st = new StringTokenizer(br.readLine());
23+
int v1 = Integer.parseInt(st.nextToken());
24+
int v2 = Integer.parseInt(st.nextToken());
25+
int cost = Integer.parseInt(st.nextToken());
26+
graph.get(v1).add(new int[]{v2, cost});
27+
}
28+
bellmanFord(1);
29+
}
30+
31+
public static void bellmanFord(int start) {
32+
dist[start] = 0;
33+
for(int i=1; i<=city; i++) {
34+
for(int j=1; j<=city; j++) {
35+
List<int[]> list = graph.get(j);
36+
for(int k=0; k<list.size(); k++) {
37+
int nextV = list.get(k)[0];
38+
int nextCost = list.get(k)[1];
39+
if(dist[j] != Long.MAX_VALUE && dist[nextV] > dist[j] + nextCost) {
40+
dist[nextV] = dist[j] + nextCost;
41+
42+
// city(정점 개수)번째 반복에 갱신되는 값이 있으면 음수 사이클 존재
43+
if(i == city) {
44+
System.out.println("-1");
45+
return;
46+
}
47+
}
48+
}
49+
}
50+
}
51+
for(int i=2; i<=city; i++) {
52+
System.out.println((dist[i] == Long.MAX_VALUE) ? "-1" : dist[i]);
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int N = Integer.parseInt(st.nextToken());
10+
int M = Integer.parseInt(st.nextToken());
11+
int[][] graph = new int[N+1][N+1];
12+
13+
for(int i=1; i<=N; i++) {
14+
for(int j=1; j<=N; j++) {
15+
if(i == j) {
16+
graph[i][j] = 0;
17+
}else {
18+
graph[i][j] = 1000;
19+
}
20+
}
21+
}
22+
23+
for(int i=0; i<M; i++) {
24+
st = new StringTokenizer(br.readLine());
25+
int A = Integer.parseInt(st.nextToken());
26+
int B = Integer.parseInt(st.nextToken());
27+
graph[A][B] = graph[B][A] = 1;
28+
}
29+
30+
for(int k=1; k<=N; k++) {
31+
for(int i=1; i<=N; i++) {
32+
for(int j=1; j<=N; j++) {
33+
graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
34+
}
35+
}
36+
}
37+
38+
int[] count = new int[N+1];
39+
for(int i=1; i<=N; i++) {
40+
for(int j=1; j<=N; j++) {
41+
count[i] += graph[i][j];
42+
}
43+
}
44+
int answer = 1;
45+
for(int i=1; i<=N; i++) {
46+
if(count[answer] > count[i]) {
47+
answer = i;
48+
}
49+
}
50+
System.out.println(answer);
51+
}
52+
}

week7/BOJ_15723(n단 논법).java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static int n;
7+
public static boolean[][] graph;
8+
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
n = Integer.parseInt(br.readLine());
12+
graph = new boolean[27][27];
13+
for(int i=0; i<n; i++) {
14+
StringTokenizer st = new StringTokenizer(br.readLine());
15+
int a = st.nextToken().charAt(0) - 'a';
16+
st.nextToken();
17+
int b = st.nextToken().charAt(0) - 'a';
18+
graph[a][b] = true;
19+
}
20+
int m = Integer.parseInt(br.readLine());
21+
StringBuffer sb = new StringBuffer();
22+
for(int i=0; i<m; i++) {
23+
StringTokenizer st = new StringTokenizer(br.readLine());
24+
int a = st.nextToken().charAt(0) - 'a';
25+
st.nextToken();
26+
int b = st.nextToken().charAt(0) - 'a';
27+
sb.append(bfs(a, b)).append('\n');
28+
}
29+
System.out.print(sb);
30+
}
31+
32+
public static char bfs(int start, int end) {
33+
boolean[] visited = new boolean[27];
34+
Queue<Integer> q = new LinkedList<>();
35+
q.offer(start);
36+
while(!q.isEmpty()) {
37+
int v = q.poll();
38+
if(v == end) {
39+
return 'T';
40+
}
41+
42+
for(int i=0; i<27; i++) {
43+
if(graph[v][i] && !visited[i]) {
44+
visited[i] = true;
45+
q.offer(i);
46+
}
47+
}
48+
}
49+
return 'F';
50+
}
51+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int N = Integer.parseInt(st.nextToken());
10+
int M = Integer.parseInt(st.nextToken());
11+
int[][] graph = new int[N+1][N+1];
12+
for(int i=1; i<=N; i++) {
13+
for(int j=1; j<=N; j++) {
14+
if(i == j) {
15+
graph[i][j] = 0;
16+
}else {
17+
graph[i][j] = 100;
18+
}
19+
}
20+
}
21+
for(int i=0; i<M; i++) {
22+
st = new StringTokenizer(br.readLine());
23+
int A = Integer.parseInt(st.nextToken());
24+
int B = Integer.parseInt(st.nextToken());
25+
graph[A][B] = graph[B][A] = 1;
26+
}
27+
28+
for(int k=1; k<=N; k++) {
29+
for(int i=1; i<=N; i++) {
30+
for(int j=1; j<=N; j++) {
31+
graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
32+
}
33+
}
34+
}
35+
for(int i=1; i<=N; i++) {
36+
for(int j=1; j<=N; j++) {
37+
if(graph[i][j] > 6) {
38+
System.out.println("Big World!");
39+
return;
40+
}
41+
}
42+
}
43+
System.out.println("Small World!");
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static int city;
7+
public static List<List<int[]>> graph;
8+
public static int[] dist;
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
city = Integer.parseInt(br.readLine());
13+
int bus = Integer.parseInt(br.readLine());
14+
graph = new ArrayList<>();
15+
dist = new int[city+1];
16+
for(int i=0; i<=city; i++) {
17+
graph.add(new ArrayList<>());
18+
dist[i] = Integer.MAX_VALUE;
19+
}
20+
for(int i=0; i<bus; i++) {
21+
StringTokenizer st = new StringTokenizer(br.readLine());
22+
int v1 = Integer.parseInt(st.nextToken());
23+
int v2 = Integer.parseInt(st.nextToken());
24+
int cost = Integer.parseInt(st.nextToken());
25+
graph.get(v1).add(new int[]{v2, cost});
26+
}
27+
StringTokenizer st = new StringTokenizer(br.readLine());
28+
int start = Integer.parseInt(st.nextToken());
29+
int end = Integer.parseInt(st.nextToken());
30+
dijkstra(start, end);
31+
}
32+
33+
public static void dijkstra(int start, int end) {
34+
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[1], o2[1]));
35+
pq.offer(new int[]{start, 0});
36+
while(!pq.isEmpty()) {
37+
int v = pq.peek()[0];
38+
int cost = pq.peek()[1];
39+
pq.poll();
40+
41+
if(dist[v] < cost) {
42+
continue;
43+
}
44+
45+
List<int[]> list = graph.get(v);
46+
for(int i=0; i<list.size(); i++) {
47+
int nextV = list.get(i)[0];
48+
int nextCost = list.get(i)[1];
49+
if(dist[nextV] > cost + nextCost) {
50+
dist[nextV] = cost + nextCost;
51+
pq.offer(new int[]{nextV, dist[nextV]});
52+
}
53+
}
54+
}
55+
System.out.println(dist[end]);
56+
}
57+
}

week7/BOJ_5972(택배 배송).java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static List<List<int[]>> graph;
7+
public static int[] dist;
8+
public static int N;
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
N = Integer.parseInt(st.nextToken());
14+
int M = Integer.parseInt(st.nextToken());
15+
graph = new ArrayList<>();
16+
dist = new int[N+1];
17+
for(int i=0; i<=N; i++) {
18+
graph.add(new ArrayList<>());
19+
dist[i] = Integer.MAX_VALUE;
20+
}
21+
for(int i=0; i<M; i++) {
22+
st = new StringTokenizer(br.readLine());
23+
int A = Integer.parseInt(st.nextToken());
24+
int B = Integer.parseInt(st.nextToken());
25+
int C = Integer.parseInt(st.nextToken());
26+
graph.get(A).add(new int[]{B, C});
27+
graph.get(B).add(new int[]{A, C});
28+
}
29+
dijkstra();
30+
}
31+
32+
public static void dijkstra(){
33+
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[1], o2[1]));
34+
pq.offer(new int[]{1, 0});
35+
while(!pq.isEmpty()) {
36+
int v = pq.peek()[0];
37+
int cost = pq.peek()[1];
38+
pq.poll();
39+
40+
// 중복 방문 방지
41+
if(dist[v] < cost) {
42+
continue;
43+
}
44+
45+
List<int[]> list = graph.get(v);
46+
for(int i=0; i<list.size(); i++) {
47+
int nextV = list.get(i)[0];
48+
int nextCost = list.get(i)[1];
49+
if(dist[nextV] > cost + nextCost) {
50+
dist[nextV] = cost + nextCost;
51+
pq.offer(new int[]{nextV, dist[nextV]});
52+
}
53+
}
54+
}
55+
System.out.println(dist[N]);
56+
}
57+
}

0 commit comments

Comments
 (0)