๐ฉ๐ปโ๐พ
๋ค์ต์คํธ๋ผ ๋ฌธ์ ์ ๋ฆฌ ๋ณธ๋ฌธ
1753๋ฒ: ์ต๋จ๊ฒฝ๋ก
์ฒซ์งธ ์ค์ ์ ์ ์ ๊ฐ์ V์ ๊ฐ์ ์ ๊ฐ์ E๊ฐ ์ฃผ์ด์ง๋ค. (1 โค V โค 20,000, 1 โค E โค 300,000) ๋ชจ๋ ์ ์ ์๋ 1๋ถํฐ V๊น์ง ๋ฒํธ๊ฐ ๋งค๊ฒจ์ ธ ์๋ค๊ณ ๊ฐ์ ํ๋ค. ๋์งธ ์ค์๋ ์์ ์ ์ ์ ๋ฒํธ K(1 โค K โค V)๊ฐ
www.acmicpc.net
import java.io.*;
import java.util.*;
public class BOJ_1753_1 {
static int V, E, K;
static ArrayList<Edge>[] graph;
static boolean[] visited;
static int[] dist;
static PriorityQueue<Edge> pq;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
V = Integer.parseInt(st.nextToken());
E = Integer.parseInt(st.nextToken());
K = Integer.parseInt(br.readLine());
graph = new ArrayList[V + 1];
visited = new boolean[V + 1];
dist = new int[V + 1];
pq = new PriorityQueue<>();
for (int i = 0; i <= V; i++)
graph[i] = new ArrayList<>();
for (int i = 0; i <= V; i++)
dist[i] = Integer.MAX_VALUE;
for (int i = 0; i < E; 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[u].add(new Edge(v, w));
}
pq.add(new Edge(K, 0));
dist[K] = 0;
while (!pq.isEmpty()) {
Edge cur = pq.poll();
int curVertex = cur.vertex;
if (!visited[curVertex]) {
visited[curVertex] = true;
for (int i = 0; i < graph[curVertex].size(); i++) {
Edge temp = graph[curVertex].get(i);
int nextV = temp.vertex;
int value = temp.value;
if (dist[nextV] > dist[curVertex] + value) {
dist[nextV] = dist[curVertex] + value;
pq.add(new Edge(nextV, dist[nextV]));
}
}
}
}
for (int i = 1; i <= V; i++) {
if (visited[i])
System.out.println(dist[i]);
else
System.out.println("INF");
}
}
static class Edge implements Comparable<Edge> {
int vertex, value; // ์ฐ๊ฒฐ๋ ์ ์ , ๊ฐ์ค์น ์ ์ฅ
public Edge(int vertex, int value) {
this.vertex = vertex;
this.value = value;
}
@Override
public int compareTo(Edge e) {
return this.value - e.value;
}
}
}
2๋ฒ) 2211๋ฒ ๋คํธ์ํฌ ๋ณต๊ตฌ
2211๋ฒ: ๋คํธ์ํฌ ๋ณต๊ตฌ
์ฒซ์งธ ์ค์ ๋ ์ ์ N, M์ด ์ฃผ์ด์ง๋ค. ๋ค์ M๊ฐ์ ์ค์๋ ํ์ ์ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ์ธ ์ ์ A, B, C๊ฐ ์ฃผ์ด์ง๋ค. ์ด๋ A๋ฒ ์ปดํจํฐ์ B๋ฒ ์ปดํจํฐ๊ฐ ํต์ ์๊ฐ์ด C (1 โค C โค 10)์ธ ํ์ ์ผ๋ก ์ฐ๊ฒฐ๋์ด ์๋ค
www.acmicpc.net
1๋ฒ ๋ฌธ์ ๋ ๋ฐฉํฅ๊ทธ๋ํ์๋ค๋ฉด, 2๋ฒ ๋ฌธ์ ๋ ๋ฌด๋ฐฉํฅ๊ทธ๋ํ ๋ฌธ์ ์ด๊ธฐ์ Node ํด๋์ค ๋ฉค๋ฒ ๋ณ์๋ก ์ฐ๊ฒฐ๋ ์ ์ ์ ๋ฒํธ, ๊ฐ์ค์น + ๋ณธ์ธ์ ๋ฒํธ๋ฅผ ์ ์ธํด์ค์ผ ํ๋ค.
import java.io.*;
import java.util.*;
public class Main {
static ArrayList<Node>[] graph;
static ArrayList<Node> ans;
static boolean[] visited;
static int[] dist;
static PriorityQueue<Node> pq;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
graph = new ArrayList[N + 1];
visited = new boolean[N + 1];
dist = new int[N + 1];
pq = new PriorityQueue<>();
ans = new ArrayList<>();
for (int i = 0; i <= N; i++)
graph[i] = new ArrayList<>();
for (int i = 0; i <= N; i++)
dist[i] = Integer.MAX_VALUE;
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine(), " ");
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
graph[s].add(new Node(s, e, t));
graph[e].add(new Node(e, s, t));
}
pq.add(new Node(1, 1, 0));
visited[1] = true;
dist[1] = 0;
while (!pq.isEmpty()) {
Node cur = pq.poll();
if (!visited[cur.to]) {
visited[cur.to] = true;
ans.add(cur);
}
for (int i = 0; i < graph[cur.to].size(); i++) {
Node next = graph[cur.to].get(i);
if (dist[next.to] > dist[cur.to] + next.cost) {
dist[next.to] = dist[cur.to] + next.cost;
pq.add(new Node(next.from, next.to, dist[next.to]));
}
}
}
System.out.println(ans.size());
for (Node node : ans)
System.out.println(node.from + " " + node.to);
}
static class Node implements Comparable<Node> {
int from, to, cost;
Node(int start, int end, int cost) {
this.from = start;
this.to = end;
this.cost = cost;
}
@Override
public int compareTo(Node n) {
return this.cost - n.cost;
}
}
}
'์ฝ๋ฉํ ์คํธ > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค/Python] 18870๋ฒ ์ขํ ์์ถ (0) | 2023.04.02 |
---|---|
[๋ฐฑ์ค/Python] 18352๋ฒ ํน์ ๊ฑฐ๋ฆฌ์ ๋์ ์ฐพ๊ธฐ (0) | 2023.03.21 |
[๋ฐฑ์ค/Python] 1033๋ฒ ์นตํ ์ผ (0) | 2023.03.16 |
[๋ฐฑ์ค/Python] 1934๋ฒ ์ต์๊ณต๋ฐฐ์ (0) | 2023.03.15 |
[๋ฐฑ์ค/Python] 1456๋ฒ ๊ฑฐ์ ์์ (0) | 2023.03.15 |