2024. 1. 16. 16:58γCoding Test/Programmers
πλ¬Έμ 보λ¬κ°κΈ°
π§π»π» νμ΄ κ³Όμ
[1μ°¨ μλ] λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦ (μ€ν¨)
μΆλ°μ§(s)λ‘λΆν° κ° λ Έλμ λν μ΅λ¨ κ²½λ‘ λ°°μ΄μ ꡬν΄λκ³ , κ° λ Έλ(i)λ§λ€ λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ λλ €μ sππ»i + iππ»a + iππ»b κ°λ€ μ€ μ΅μκ°μ ꡬνλ λ°©μμΌλ‘ νμμ΅λλ€. λ Έλμ κ°μ(N)κ° μ΅λ 200κ°λΌ λ μ€ μμλλ° ν¨μ¨μ± ν μ€νΈμμ μκ° μ΄κ³Όκ° λλκ΅°μ.
[2μ°¨ μλ] νλ‘μ΄λ μμ μκ³ λ¦¬μ¦ (μ±κ³΅)
맀 λ Έλλ§λ€ λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ λλ¦° κ²μ΄ μμΈμ΄λΌ μκ°λμ΄, λͺ¨λ λ Έλ κ°μ μ΅λ¨ κ²½λ‘λ₯Ό ꡬν μ μλ νλ‘μ΄λ μμ μκ³ λ¦¬μ¦μ μ¬μ©νμ΅λλ€. μ¬μ€ μλ μ μ¨ λ³΄κ³ μ μ¨μ κΈ°μ΅μ΄ μ λ¬λλ°, κ°λ¨ν μκ³ λ¦¬μ¦μ΄λΌ 빨리 곡λΆνκ³ ν μ μμλ€μ.
βοΈμμ€ μ½λ λ° κ²°κ³Ό
#include <vector>
using namespace std;
using uint32 = unsigned int;
#define INF 3000000000
void FloydWarshall(vector<vector<uint32>>& graph, int n)
{
for (int mid = 1; mid <= n; mid++)
for (int from = 1; from <= n; from++)
for (int to = 1; to <= n; to++)
graph[from][to] = min(graph[from][to], graph[from][mid] + graph[mid][to]);
}
int solution(int n, int s, int a, int b, vector<vector<int>> fares) {
// 1. Input Initialize
vector<vector<uint32>> graph(n + 1, vector<uint32>(n + 1, INF));
for (int i = 1; i <= n; i++)
graph[i][i] = 0;
for (const vector<int>& fare : fares)
{
uint32 from = fare[0], to = fare[1], cost = fare[2];
graph[from][to] = graph[to][from] = cost;
}
// 2. Floyd-Warshall Algorithm
FloydWarshall(graph, n);
uint32 answer = INF;
for (int i = 1; i <= n; i++)
answer = min(answer, graph[s][i] + graph[i][a] + graph[i][b]);
return answer;
}
'Coding Test > Programmers' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Programmers] Lv3. λΆλλ³΅κ· | C++ (0) | 2024.01.13 |
---|---|
[Programmers] Lv3. [1μ°¨] μ νλ²μ€ | C++ (0) | 2023.11.18 |
[Programmers] Lv3. μ¬ μ°κ²°νκΈ° | C++ (1) | 2023.10.22 |
[Programmers] Lv3. κ°μ₯ λ¨Ό λ Έλ | C++ (0) | 2023.10.22 |
[Programmers] Lv3. [μΉ΄μΉ΄μ€ μΈν΄] 보μ μΌν | C++ (0) | 2023.10.22 |