๋ฐ์ํ
๋ฌธ์
https://www.acmicpc.net/problem/16118
16118๋ฒ: ๋ฌ๋น ์ฌ์ฐ
์ฒซ ์ค์ ๋๋ฌด ๊ทธ๋ฃจํฐ๊ธฐ์ ๊ฐ์์ ์ค์๊ธธ์ ๊ฐ์๋ฅผ ์๋ฏธํ๋ ์ ์ N, M(2 ≤ N ≤ 4,000, 1 ≤ M ≤ 100,000)์ด ์ฃผ์ด์ง๋ค. ๋ ๋ฒ์งธ ์ค๋ถํฐ M๊ฐ์ ์ค์ ๊ฑธ์ณ ๊ฐ ์ค์ ์ธ ๊ฐ์ ์ ์ a, b, d(1 ≤ a, b ≤ N, a ≠ b
www.acmicpc.net
ํ์ด
๋ชจ๋ ์ ์ ์ ๋ํ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ํ์ํ๋ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์
๊ฐ์ฅ ์ค์ํ ๊ณ ๋ ค์ฌํญ์ ๋๋์ ๊ฒฝ์ฐ ํ์/์ง์๋ฒ์งธ๋ก ๊ฑด๋์ ๊ฒฝ์ฐ๋ฅผ ๊ฐ๊ฐ ์ ์ฅํด ์ค ์ ์์ด์ผ ํจ!
์๋๊ฐ /2๊ฐ ๋ ์ ์์ผ๋ฏ๋ก ์์์ ๋ฐฉ์ง๋ฅผ ์ํด ์ฒ์ ์ ๋ ฅ ๋ฐ์ ์ weight์ *2 ๋ฅผ ๊ณฑํ์ฌ ์ ์ฅ!
๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ PriorityQueue๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํ
์ฝ๋
๋๋ณด๊ธฐ
import java.util.*;
import java.io.*;
public class Main {
static class Edge implements Comparable<Edge>{
int end, weight, dir;
public Edge(int end, int weight) {
this.end = end;
this.weight = weight;
}
public Edge(int end, int weight, int dir) {
this.end = end;
this.weight = weight;
this.dir = dir;
}
@Override
public int compareTo(Edge o) {
return Integer.compare(this.weight, o.weight);
}
}
static int N, M;
static List<Edge>[] graph;
static int[] disFox;
static int[][] disWolf; // 1: ํ,์ง / 2: ๊ฑฐ๋ฆฌ
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());
graph = new ArrayList[N];
for(int i =0 ; i < N ; i++){
graph[i] = new ArrayList<>();
}
disFox = new int[N];
disWolf = new int[2][N];
for(int i =0 ; i < M ; i++){
st = new StringTokenizer(br.readLine());
int to = Integer.parseInt(st.nextToken())-1;
int from = Integer.parseInt(st.nextToken())-1;
int dis = Integer.parseInt(st.nextToken());
graph[to].add(new Edge(from, dis*2)); // ์ ์์ ๋๋์
๊ฒฐ๊ณผ๋ฅผ ์ํด *2
graph[from].add(new Edge(to, dis*2));
}
Arrays.fill(disFox, Integer.MAX_VALUE);
Arrays.fill(disWolf[0], Integer.MAX_VALUE);
Arrays.fill(disWolf[1], Integer.MAX_VALUE);
findFoxPath();
findWolfPath();
int cnt = 0;
for(int i =0 ; i < N ; i++){
if(disFox[i] < Integer.min(disWolf[0][i], disWolf[1][i])) cnt++;
}
System.out.println(cnt);
}
private static void findWolfPath() {
PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.add(new Edge(0, 0, 0));
disWolf[0][0] = 0;
while(!pq.isEmpty()){
Edge cur = pq.poll();
if(disWolf[cur.dir][cur.end] < cur.weight) continue; // ์ด๋ฏธ ๋น๊ต ๊ฐ๋ณด๋ค ์์ ๊ฒฝ์ฐ
for(Edge nEdge : graph[cur.end]){
int nNode = nEdge.end;
int nWeight = cur.weight;
int nDir = 0;
if(cur.dir == 0){ // ํ์ฌ ํ์๋ฒ์งธ๋ก ๊ฑด๋๋ค๋ฉด
nWeight += nEdge.weight / 2; // ์๋ ๋๋ฐฐ
nDir = 1; // ๋ค์ ์ง์
} else{ // ํ์ฌ ์ง์๋ฒ์งธ๋ก ๊ฑด๋๋ค๋ฉด
nWeight += nEdge.weight * 2; // ์๋ 1/2๋ฐฐ
nDir = 0; // ๋ค์ ํ์
}
if(disWolf[nDir][nNode] > nWeight){
disWolf[nDir][nNode] = nWeight;
pq.add(new Edge(nNode, nWeight, nDir));
}
}
}
}
// ๊ธฐ๋ณธ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ
private static void findFoxPath() {
PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.add(new Edge(0, 0));
disFox[0] = 0;
while(!pq.isEmpty()){
Edge cur = pq.poll();
if(disFox[cur.end] < cur.weight) continue;
for(Edge nEdge : graph[cur.end]){
int nNode = nEdge.end;
int nWeight = cur.weight + nEdge.weight;
if(disFox[nNode] > nWeight){
disFox[nNode] = nWeight;
pq.add(new Edge(nNode, nWeight));
}
}
}
}
}
๋ฐ์ํ
'๐ ์๊ณ ๋ฆฌ์ฆ > ShortestPath' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 1091. Shortest Path in Binary Matrix (ํด์, Kotlin ํ์ด) (0) | 2023.06.19 |
---|---|
[๋ฐฑ์ค] 13549๋ฒ: ์จ๋ฐ๊ผญ์ง3 (0) | 2022.04.03 |
[๋ฐฑ์ค] 1238๋ฒ: ํํฐ (JAVA) (0) | 2022.04.03 |
[๋ฐฑ์ค]11780๋ฒ ํ๋ก์ด๋2 (Java) (0) | 2022.02.21 |
[๋ฐฑ์ค] 1753๋ฒ ์ต๋จ๊ฒฝ๋ก (Java) (0) | 2021.08.24 |