Skip to content

[이준희-11주차 알고리즘 스터디] #14

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

Open
wants to merge 16 commits into
base: junhee325/week11
Choose a base branch
from
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
90 changes: 75 additions & 15 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,75 @@
- closed #000

### Week 00 (00.00.00.)
| Category | Title | Link |
| :------: | :---: | :--: |
| | <a href="https://www.acmicpc.net/problem/문제번호"></a> | <a href="">🔗</a> |
| | <a href="https://www.acmicpc.net/problem/문제번호"></a> | <a href="">🔗</a> |
| | <a href="https://www.acmicpc.net/problem/문제번호"></a> | <a href="">🔗</a> |
| | <a href="https://www.acmicpc.net/problem/문제번호"></a> | <a href="">🔗</a> |
| | <a href="https://www.acmicpc.net/problem/문제번호"></a> | <a href="">🔗</a> |
<br>

```
To. Reviewers
```
# 🚀 싸피 15반 알고리즘 스터디 5주차 [이준희]

## 📌 문제 풀이 개요
- 이번 PR에서는 다음 5문제의 풀이를 포함합니다.
- 각 문제에 대한 풀이 과정과 접근 방식을 설명합니다.
---

## ✅ 문제 해결 여부

- [x] **문제 1**
- [x] **문제 2**
- [ ] **문제 3**
- [x] **문제 4**
- [ ] **문제 5**

---

## 💡 풀이 방법
### 문제 1: 문제 이름
(문제 이름은 현재 문제에 맞게 바꿔주세요! 바꾸시고 이 문장을 지워주세요.)

**문제 난이도**



**문제 유형**



**접근 방식 및 풀이**


---



### 문제 2: 문제 이름
**문제 유형**



**접근 방식 및 풀이**



---
### 문제 3: 문제 이름
**문제 유형**






**접근 방식 및 풀이**


---
### 문제 4: 문제 이름
**문제 유형**



**접근 방식 및 풀이**


---
### 문제 5: 문제 이름
**문제 유형**



**접근 방식 및 풀이**


Empty file added pullrequesttest.java
Empty file.
51 changes: 51 additions & 0 deletions week10/BOJ11403.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package week10;

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

public class BOJ11403 {

static int[][] arr;
static int n;

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

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
n = Integer.parseInt(br.readLine());
arr = new int[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 1; j <= n; j++) {
int a = Integer.parseInt(st.nextToken());
if (a == 1)
arr[i][j] = 1;
else
arr[i][j] = 101;
}
}

for (int k = 1; k <= n; k++) { // 플로이드-워셜 알고리즘으로 모든 점에서 다른 모든 점으로의 거리 계산
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
arr[i][j] = Math.min(arr[i][j], arr[i][k] + arr[k][j]);
}
}
}

for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (arr[i][j] > 100 || arr[i][j] == 0) {
System.out.print("0 ");
} else {
System.out.print("1 ");
}
}
System.out.println();
}

}

}

87 changes: 87 additions & 0 deletions week10/BOJ1197.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package week10;


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

public class BOJ1197 {

static class Edge implements Comparable<Edge> {
int from, to, weight;

public Edge(int from, int to, int weight) {
super();
this.from = from;
this.to = to;
this.weight = weight;
}

@Override
public int compareTo(Edge o) {
return Integer.compare(this.weight, o.weight);
}

}

static int V, E;
static int[] parents;
static Edge[] edgeList;

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

V = Integer.parseInt(st.nextToken());
E = Integer.parseInt(st.nextToken());
parents = new int[V + 1];
edgeList = new Edge[2*E];
for (int i = 1; i <= V; i++) {
parents[i] = i;
}

for (int i = 0; i < E; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());

edgeList[i] = new Edge(from, to, w); // 무향 그래프이므로 from to 번갈아 저장
edgeList[E+i] = new Edge(to, from, w);
}

Arrays.sort(edgeList);
long result = 0, count = 0;

for (Edge edge : edgeList) { // edge 마다 탐색
if (union(edge.from, edge.to)) {
result += edge.weight;
if (++count == V - 1) {
break;
}
}
}

System.out.println(result);
}

static int find(int a) {
if (a == parents[a])
return a;
return parents[a] = find(parents[a]);
}

static boolean union(int a, int b) {
int aRoot = find(a);
int bRoot = find(b);

if (aRoot == bRoot)
return false;

parents[bRoot] = aRoot;
return true;
}

}

124 changes: 124 additions & 0 deletions week10/BOJ1238.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package week10;

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

class Node implements Comparable<Node> {
int index;
int weight;

public Node(int index, int cost) {
this.index = index;
this.weight = cost;
}

@Override
public int compareTo(Node o) {
return Integer.compare(this.weight, o.weight);
}
}

public class BOJ1238 {

static int n, m, x;
static ArrayList<Node>[] graph;
static ArrayList<Node>[] graph2;

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

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
x = Integer.parseInt(st.nextToken());

graph = new ArrayList[n + 1]; // 목적지 도착용
graph2 = new ArrayList[n + 1]; // 목적지에서 귀가용

for (int i = 0; i <= n; i++) {
graph[i] = new ArrayList<>();
graph2[i] = new ArrayList<>();
}

for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());

graph[v].add(new Node(u, w));
graph2[u].add(new Node(v, w));
}

int[] dist = Dijkstra(x);
int[] dist2 = Dijkstra2(x);
int max = 0;
for (int i = 1; i <= n; i++) {
max = Math.max(max, dist[i] + dist2[i]);
}
System.out.println(max);
}

public static int[] Dijkstra(int start) { // 목적지 도착용
boolean[] check = new boolean[n + 1];
int[] dist = new int[n + 1];
int INF = Integer.MAX_VALUE;

Arrays.fill(dist, INF);
dist[start] = 0;

PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(start, 0));

while (!pq.isEmpty()) {
int nowVertex = pq.poll().index;

if (check[nowVertex])
continue;
check[nowVertex] = true;

for (Node next : graph[nowVertex]) {
if (dist[next.index] > dist[nowVertex] + next.weight) {
dist[next.index] = dist[nowVertex] + next.weight;

pq.offer(new Node(next.index, dist[next.index]));
}
}
}

return dist;
}

public static int[] Dijkstra2(int start) { // 목적지 출발용
boolean[] check = new boolean[n + 1];
int[] dist = new int[n + 1];
int INF = Integer.MAX_VALUE;

Arrays.fill(dist, INF);
dist[start] = 0;

PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(start, 0));

while (!pq.isEmpty()) {
int nowVertex = pq.poll().index;

if (check[nowVertex])
continue;
check[nowVertex] = true;

for (Node next : graph2[nowVertex]) {
if (dist[next.index] > dist[nowVertex] + next.weight) {
dist[next.index] = dist[nowVertex] + next.weight;

pq.offer(new Node(next.index, dist[next.index]));
}
}
}
return dist;
}
}
Loading