Skip to content

[권혁준-16주차 알고리즘 스터디] #73

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 5 commits into
base: main
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
38 changes: 38 additions & 0 deletions 권혁준_16주차/[BOJ-13141] Ignition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# CPP
```cpp
#include <iostream>
#include <tuple>
#include <algorithm>
using namespace std;

int main() {
cin.tie(0)->sync_with_stdio(0);

int N, M, D[201][201]{}, INF = 1e9 + 7;
cin >> N >> M;
tuple<int, int, int> E[20000]{};
for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) if(i != j) D[i][j] = INF;
for (int i = 0, a, b, c; i < M; i++) {
cin >> a >> b >> c;
D[b][a] = D[a][b] = min(D[a][b], c);
E[i] = { a,b,c };
}

for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) for (int k = 1; k <= N; k++) {
int res = D[j][i] + D[i][k];
D[j][k] = min(D[j][k], D[j][i] + D[i][k]);
}

int ans = INF;
for (int i = 1; i <= N; i++) {
int res = 0;
for (int j = 0; j < M; j++) {
auto[s, e, l] = E[j];
res = max(res, D[i][s] + l + D[i][e]);
}
ans = min(ans, res);
}
cout << ans / 2 << "." << ((ans & 1) ? "5" : "0");

}
```
103 changes: 103 additions & 0 deletions 권혁준_16주차/[BOJ-13905] 세부.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# JAVA
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N, M, S, E;
static int[] r;
static int[][] edges;

static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));}

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
M = nextInt();
S = nextInt();
E = nextInt();
r = new int[N+1];
for(int i=1;i<=N;i++) r[i] = i;
edges = new int[M][];
for(int i=0;i<M;i++) edges[i] = new int[] {nextInt(), nextInt(), nextInt()};

}

static void solve() throws Exception {

Arrays.sort(edges, (a,b) -> b[2]-a[2]);
for(int[] e:edges) {
int a = e[0], b = e[1], c = e[2];
int x = f(a), y = f(b);
if(x == y) continue;
r[x] = y;
if(f(S) == f(E)) {
bw.write(c + "\n");
return;
}
}
bw.write("0");

}
Comment on lines +53 to +68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확실히 이렇게 보니까 제가 프림을 쓴 거 같네요. 처음에 봤을 때, s에서 출발한다는 사고를 해서 다익스트라라고 생각했나봐요.


}
```

# CPP
```cpp
#include <iostream>
#include <numeric>
#include <queue>
#include <functional>
using namespace std;

int main() {
cin.tie(0)->sync_with_stdio(0);

int N, M, S, E, r[100001]{};
cin >> N >> M >> S >> E;

iota(r, r + N + 1, 0);
function<int(int)> f = [&](int x) -> int {return x == r[x] ? x : r[x] = f(r[x]); };

priority_queue<tuple<int, int, int>> Q;
for (int a, b, c; M--; Q.emplace(c, a, b)) cin >> a >> b >> c;

while (!Q.empty()) {
auto[c, a, b] = Q.top(); Q.pop();
int x = f(a), y = f(b);
if (x == y) continue;
r[x] = y;
if (f(S) == f(E)) return cout << c, 0;
}
cout << 0;

}
```
60 changes: 60 additions & 0 deletions 권혁준_16주차/[BOJ-15653] 구슬 탈출 4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# CPP
```cpp
#include <iostream>
#include <tuple>
#include <queue>
using namespace std;

int main() {
cin.tie(0)->sync_with_stdio(0);

int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };

int N, M;
cin >> N >> M;
char A[10][10]{};
int px, py, qx, qy;


for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) {
cin >> A[i][j];
if (A[i][j] == 'R') px = i, py = j, A[i][j] = '.';
if (A[i][j] == 'B') qx = i, qy = j, A[i][j] = '.';
}

int vis[10][10][10][10]{};
vis[px][py][qx][qy]++;
queue<tuple<int, int, int, int, int>> Q;
Q.emplace(px, py, qx, qy, 0);
while (!Q.empty()) {
auto[ax, ay, bx, by, t] = Q.front();
Q.pop();
if (A[ax][ay] == 'O') return cout << t, 0;

for (int i = 0; i < 4; i++) {
int fx = ax, fy = ay, gx, gy;
while (!(fx == bx && fy == by) && A[fx][fy] == '.') fx += dx[i], fy += dy[i];
if (fx == bx && fy == by) {
gx = bx, gy = by;
while (A[gx][gy] == '.') gx += dx[i], gy += dy[i];
if (A[gx][gy] == 'O') continue;
gx -= dx[i], gy -= dy[i];
fx = gx - dx[i], fy = gy - dy[i];
}
else {
if (A[fx][fy] == '#') fx -= dx[i], fy -= dy[i];
gx = bx, gy = by;
while (!(gx == fx && gy == fy) && A[gx][gy] == '.') gx += dx[i], gy += dy[i];
if (A[gx][gy] == 'O') continue;
gx -= dx[i], gy -= dy[i];
}
if (vis[fx][fy][gx][gy]) continue;
vis[fx][fy][gx][gy]++;
Q.emplace(fx, fy, gx, gy, t + 1);
}
}
cout << -1;

}
```
43 changes: 43 additions & 0 deletions 권혁준_16주차/[BOJ-1727] 커플 만들기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# CPP
```cpp
#include <iostream>
#include <algorithm>
using namespace std;

int N, M, A[1001]{}, B[1001]{};
int dp[1001]{}, mn[1001]{};

int main() {
cin.tie(0)->sync_with_stdio(0);

cin >> N >> M;
for (int i = 1; i <= N; i++) cin >> A[i];
for (int i = 1; i <= M; i++) cin >> B[i];
sort(A + 1, A + N + 1);
sort(B + 1, B + M + 1);

if (N > M) {
swap(N, M);
swap(A, B);
}

int ans = 2e9;
for (int j = 1; j <= M; j++) {
dp[j] = abs(A[1] - B[j]);
mn[j] = j == 1 ? dp[j] : min(mn[j - 1], dp[j]);
if (N == 1) ans = min(ans, dp[j]);
}

for (int i = 2; i <= N; i++) {
int ndp[1001]{}, nmn[1001]{};
for (int j = i; j <= M; j++) {
ndp[j] = mn[j - 1] + abs(A[i] - B[j]);
nmn[j] = j == i ? ndp[j] : min(nmn[j - 1], ndp[j]);
if (i == N) ans = min(ans, ndp[j]);
}
for (int j = 1; j <= M; j++) dp[j] = ndp[j], mn[j] = nmn[j];
}
cout << ans;

}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# JAVA

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

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N, K;
static int[] A;

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

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
K = nextInt();
A = new int[N];
for(int i=0;i<N;i++) A[i] = nextInt();

}

static void solve() throws Exception {

int s = 0, e = 2000000, m = (s+e+1)>>1;
while(s<e) {
int cnt = 1, sum = 0;
for(int i:A) {
sum += i;
if(sum >= m) {
sum = 0;
cnt++;
}
}
if(cnt > K) s = m;
else e = m-1;
m = (s+e+1)>>1;
}
bw.write(m + "\n");

}

}
```

# CPP

```cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
cin.tie(0)->sync_with_stdio(0);

int N, K;
cin >> N >> K;
vector<int> A(N);
for (int &i : A) cin >> i;

int s = 0, e = 2000000, m = (s + e + 1) >> 1;
while (s < e) {
int cnt = 1, sum = 0;
for (int i : A) {
sum += i;
if (sum >= m) cnt++, sum = 0;
}
if (cnt > K) s = m;
else e = m - 1;
m = (s + e + 1) >> 1;
}
cout << m;

}
```