Skip to content
Open
Changes from 1 commit
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
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;

}
```