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
90 changes: 90 additions & 0 deletions 11-Kruskal&Prim/yongtaek/네트워크연결.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

class Edge implements Comparable<Edge> {
int start;
int end;
int weight;

Edge(int start, int end, int weight) {
this.start = start;
this.end = end;
this.weight = weight;
}

@Override
public int compareTo(Edge o) {
return weight - o.weight;
}

}

public class 네트워크연결 {
static int[] parent;
static ArrayList<Edge> edgeList;

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;

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

edgeList = new ArrayList<>();
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());

edgeList.add(new Edge(start, end, weight));
}

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

Collections.sort(edgeList);

int ans = 0;
for (int i = 0; i < edgeList.size(); i++) {
Edge edge = edgeList.get(i);

if (find(edge.start) != find(edge.end)) {
ans += edge.weight;
union(edge.start, edge.end);
}
}

bw.write(ans + "\n");
bw.flush();
bw.close();
br.close();
}

public static int find(int x) {
if (x == parent[x]) {
return x;
}

return parent[x] = find(parent[x]);
}

public static void union(int x, int y) {
x = find(x);
y = find(y);

if (x != y) {
parent[y] = x;
}
}

}
67 changes: 67 additions & 0 deletions 11-Kruskal&Prim/yongtaek/운동.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.io.*;
import java.util.*;

public class 운동 {
static long[][] graph;
static int v;
static int e;

public static class Pair {
int b,c;

Pair(int b, int c) {
this.b = b;
this.c = c;
}
}
public static void floydWarshall() {
for(int i = 1; i < v+1; i++) {
for(int j = 1; j < v+1; j++) {
for(int k = 1; k < v+1; k++) {
graph[j][k] = Math.min(graph[j][k], graph[j][i] + graph[i][k]);
}
}
}
}
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());

v = Integer.parseInt(st.nextToken());
e = Integer.parseInt(st.nextToken());

graph = new long[v+1][v+1];

for(int i = 1; i < v+1; i++) {
for(int j = 1; j < v+1; j++) {
graph[i][j] = Integer.MAX_VALUE;
}
}

for(int i = 0 ; i < e; i++) {
st = new StringTokenizer(br.readLine());

int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());

graph[a][b] = c;
}

floydWarshall();

long min = Integer.MAX_VALUE;

for(int i = 1; i < v+1; i++) {
if(graph[i][i] >= Integer.MAX_VALUE) continue;

if(graph[i][i] < min) {
min = graph[i][i];
}
}

if(min == Integer.MAX_VALUE) System.out.println(-1);
else System.out.println(min);
}
}
96 changes: 96 additions & 0 deletions 11-Kruskal&Prim/yongtaek/최소스패닝트리.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import java.util.*;
import java.io.*;

public class 최소스패닝트리 {

static class Node implements Comparable<Node> {
int from;
int to;
int cost;



public Node(int from, int to, int cost) {
super();
this.from = from;
this.to = to;
this.cost = cost;
}



@Override
public int compareTo(Node o) {
return this.cost - o.cost;
}

}
static int V, E;
static int[] parents;
static ArrayList<Node> nodeList;
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];
nodeList = new ArrayList<>();


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 cost = Integer.parseInt(st.nextToken());

//from번 정점과 to번 정점이 가중치 cost인 간선으로 연결되어 있다
nodeList.add(new Node(from,to, cost));
}

Collections.sort(nodeList);

make();

int sum = 0;
int cnt = 0;

for(Node n : nodeList) {
if(union(n.from, n.to)){
sum += n.cost;
cnt++;

if(cnt==E-1) break;
}
}

System.out.println(sum);
}

private static boolean union(int from, int to) {

int fromRoot = findSet(from);
int toRoot = findSet(to);

if(fromRoot==toRoot) return false;
else parents[toRoot] = fromRoot;
return true;
}

private static int findSet(int v) {

if(parents[v]==v) return v;
else return parents[v] = findSet(parents[v]);
}

private static void make() {
for(int i = 1 ; i <= V ; i++) {
parents[i] = i;
}
}


}
88 changes: 88 additions & 0 deletions 11-Kruskal&Prim/yongtaek/파티.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.io.*;
import java.util.*;

public class 파티 {
static ArrayList<Pair>[] graph;
static long[] distance;
static ArrayList<Long> result;

public static class Pair implements Comparable<Pair>{
int end;
int time;

Pair(int end, int time) {
this.end = end;
this.time = time;
}

@Override
public int compareTo(Pair p) {
if(this.time == p.time) {
return this.end - p.end;
}

return this.time - p.time;
}
}

public static long dijkstra(int start, int end) {
Arrays.fill(distance, Integer.MAX_VALUE);
PriorityQueue<Pair> pQueue = new PriorityQueue<>();

pQueue.offer(new Pair(start, 0));
distance[start] = 0;

while(!pQueue.isEmpty()) {
Pair poll = pQueue.poll();
int pollEnd = poll.end;
int pollTime = poll.time;

if(distance[pollEnd] < pollTime) {
continue;
}

for(Pair p: graph[pollEnd]) {
int cost = p.time + pollTime;
if(cost < distance[p.end]) {
distance[p.end] = cost;
pQueue.offer(new Pair(p.end, cost));
}
}
}
return distance[end];
}
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

st = new StringTokenizer(br.readLine());

int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());

graph = new ArrayList[n+1];
distance = new long[n+1];
result = new ArrayList<>();

for(int i = 1; i < n+1; i++) {
distance[i] = Integer.MAX_VALUE;
graph[i] = new ArrayList<>();
}

for(int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
int time = Integer.parseInt(st.nextToken());

graph[start].add(new Pair(end, time));
}

for(int i = 1; i < n+1; i++) {
result.add(dijkstra(i,x) + dijkstra(x,i));
}

System.out.println(Collections.max(result));
}
}