-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
14-wnsmir #56
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ€μ΅μ€νΈλΌ
ꡬνμ μ²μ μΈ κ² κ°λ€μ.
( μμ μ ν κ² κ°κΈ°λ νλ° κΈ°μ΅μ΄ νλλ μλλ€μ... )
κ·Έλμ κ°λ μ μ‘°κΈ μ°Ύμλ΄€κ³ μμ£Όμμ£Ό κ°λ¨νκ² μ 리λ ν΄λ΄€μ΅λλ€.
λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ κ·Έλνμμ νλμ μ μ (μΆλ°μ )μΌλ‘λΆν°
λ€λ₯Έ λͺ¨λ μ μ κΉμ§μ μ΅λ¨ κ²½λ‘λ₯Ό μ°Ύλ μκ³ λ¦¬μ¦μ
λλ€.
( One to All )
κ°μ€μΉκ° μλ κ·Έλνμμ μ¬μ©λλ©°, κ°μ μ κ°μ€μΉκ° μμκ° μλ κ²½μ°μλ§ μ¬λ°λ₯΄κ² λμν©λλ€.
( μμ κ°μ€μΉκ° μλ€λ©΄ μ΅μλ₯Ό μ°Ύμκ°κΈ° λλ¬Έμ 무ν 루νκ° μκΉ )
μ²μμλ μ°μ μμ νλ₯Ό μ¬μ©νμ§ μκ³ κ·Έλ₯ νλ₯Ό μΌμ΅λλ€.
κ·Έλλ λ¬Έμ λ ν리λλΌκ΅¬μ...
κ·Έλ¬κ³ 리뷰λ₯Ό λ¬κΈ°μν΄μ μ€μ©λ μ€λͺ
μ μ½μ΄λ΄€μ΅λλ€.
κ·Έλ°λ°... μ€μ©λμ μ°μ μμ νλ₯Ό μ¬μ©νμ¬μ μ΅μ κ°μ€μΉλΆν° κ³μ°νλ λ‘μ§μΌλ‘ ꡬννμ κ²μ λ΄€μ΅λλ€.
μ κ·Έλ΄κΉ μκ°μ ν΄λ΄€λλ° κ·Έλ₯ νλ₯Ό μ¬μ©νλ€λ©΄ κ°μ€μΉ λ°°μ΄μ΄ κ³μν΄μ μ
λ°μ΄νΈλλ λ¬Έμ κ° μκΈΈ μ λ μμ κ² κ°μ΅λλ€.
λ°λΌμ μ΄ λ¬Έμ κ°μ΄ μ
λ ₯μ΄ λ§μ λ¬Έμ μμλ μκ° λ³΅μ‘λμ ν° μν₯μ λΌμΉ κ²μ΄λΌκ³ μκ°νμ΅λλ€.
κ·Έλμ μ λ μ°μ μμ νλ‘ κ³ μΉκ³ λ¬Έμ λ₯Ό λ€μ νμ΄λ΄€μ΅λλ€...
λ κ· νΈλμ΄ μκ°ν΄μ£Όμ ¨λ c++ λ¬Έλ²μ€μ
auto [a, b] = c;
μ΄ λ‘μ§ μ μ¬μ©νμ΅λλ€. κ°μ¬ν©λλ€~
CPP CODE
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <limits>
using namespace std;
int N, M, C;
vector<vector<pair<int, int>>> graph;
vector<int> distances;
void solution() {
distances = vector<int>(N + 1, INT_MAX);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
pq.push({0,C});
distances[C] = 0;
while (!pq.empty()) {
auto [curDist, cur] = pq.top();
pq.pop();
if (curDist > distances[cur]) continue;
for (auto [v, w] : graph[cur]) {
int nextDist = curDist + w;
if (nextDist < distances[v]){
distances[v] = nextDist;
pq.push({nextDist, v});
}
}
}
}
int main() {
cin >> N >> M >> C;
graph = vector<vector<pair<int, int>>>(N + 1);
for (int i = 0; i < M; i++) {
int in, out, w;
cin >> in >> out >> w;
graph[in].push_back({out, w});
}
solution();
int connected = 0;
int maxTime = 0;
for (int i : distances) {
if (i != INT_MAX) {
connected++;
maxTime = max(i, maxTime);
}
}
cout << connected - 1 << ' ' << maxTime << '\n';
return 0;
}
def dijkstra(start): | ||
q = [] | ||
heapq.heappush(q, (0,start)) | ||
distance[start] = 0 | ||
while q: | ||
dist, now = heapq.heappop(q) | ||
if distance[now] < dist: | ||
continue | ||
for i in graph[now]: | ||
cost = dist + i[1] | ||
if cost < distance[i[0]]: | ||
distance[i[0]] = cost | ||
heapq.heappush(q, (cost,i[0])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ°μ€μΉ κ·Έλνμμ λ€μ΅μ€νΈλΌ
λ₯Ό ꡬννλ κΈ°λ³Έ λ‘μ§μΈ κ² κ°λ€μ!
μ§μ ꡬνν΄λ³Έ κ²μ μ²μμΈλ°, λλ¦ μ§κ΄μ μΌλ‘ μ΄ν΄κ° μ λμ΅λλ€.
μμΌλ‘λ μ’ λ μ΄λ €μ΄ λ€μ΅μ€νΈλΌλ λ§μ£Όν΄λ³΄κ³ μΆμ΅λλ·. π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
οΏ½λ€μ΅μ€νΈλΌ μ λ§ μ€λλ§μ΄λ€μ. λ무 μ€λμ μ΄λΌ κΈ°μ΅λ μλμ λ¬Έμ λ₯Ό 보μλ§μ μ°Ύμ보μμ΅λλ€. μ΅λ¨κ²½λ‘λ₯Ό νΈλ λ¬Έμ μμλ κ±°μ μ
μΈκ² κ°λ€μ.. λΉμ·ν μ νμ λ§μ΄ νμ΄μ λΉ λ₯΄κ² λ§μ€ν° ν΄μΌκ² μ΅λλ€.
μ΄μ©λ©΄ μ μμ μΈ λ¬Έμ λΌμ λ€μ΅μ€νΈλΌ λ‘μ§μ μμ λκ°μ΅λλ€. κ·ΈλΌμλ λ§μ΄ μ€μν μ½λ κ°μΌλ μ 체 μ½λλ₯Ό λ¬μλκ² μ΅λλ€.
μ΄λ² prλ μκ³ νμ ¨μ΅λλ€
code
//
// Created by κΉκ· νΈ on 2025. 1. 13..
//
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
int n, m, c;
int s, e, w;
vector<vector<pair<int, int> > > cities;
vector<int> minDist;
void relax(int next, int nDist, priority_queue<pair<int, int> > &pq) {
if (nDist < minDist[next]) {
minDist[next] = nDist;
pq.emplace(-nDist, next);
}
}
void dijkstra() {
minDist = vector<int>(n + 1, INT_MAX);
priority_queue<pair<int, int> > pq;
minDist[c] = 0;
pq.emplace(0, c);
while (!pq.empty()) {
int dist = -pq.top().first;
int cur = pq.top().second;
pq.pop();
if (minDist[cur] < dist) continue;
for (auto &city: cities[cur]) {
int next = city.first;
int nDist = dist + city.second;
relax(next, nDist, pq);
}
}
}
int main() {
cin >> n >> m >> c;
cities.resize(n + 1);
for (int i = 0; i < m; i++) {
cin >> s >> e >> w;
cities[s].emplace_back(e, w);
}
dijkstra();
int cnt = 0;
int dist = 0;
for (int &d: minDist) {
if (d != INT_MAX) {
cnt++;
dist = max(dist, d);
}
}
cout << cnt - 1 << ' ' << dist;
return 0;
}
π λ¬Έμ λ§ν¬
μ΄μ λ¬Έμ μ νλ¦μ μ΄μ΄λ°μ μ΅λ¨κ±°λ¦¬λ¬Έμ νλ λ κ°μ Έμ보μμ΅λλ€.
Nκ°μ λμκ° μκ³ , Mκ°μ ν΅λ‘κ° μμ΅λλ€. Nκ°μ λμμ€ CλΌλ λμκ° μνμ μ³ν΄ μν©μ μλ¦¬κ³ μ μ΅λν λ§μ λμλ‘ λ©μμ§λ₯Ό 보λ΄λ €ν©λλ€.
λ©μμ§λ Cμμ μΆλ°νμ¬ κ° λμ μ¬μμ μ€μΉλ ν΅λ‘λ₯Ό κ±°μ³ μ΅λν λ§μ΄ νΌμ Έλκ°λλ€.
ν΅λ‘λ λ°©ν₯μ±μ κ°μ§λ©°, ν΅λ‘λ§λ€ κ°μ€μΉκ° μ£Όμ΄μ§λλ€.
κ° λμμ λ²νΈμ ν΅λ‘κ° μ€μΉλμ΄μλ μ λ³΄κ° μ£Όμ΄μ‘μλ λμ Cμμ λ³΄λΈ λ©μμ§λ₯Ό λ°κ²λλ λμμ κ°μλ μ΄ λͺκ°μ΄λ©° λμλ€μ΄ λͺ¨λ λ©μμ§λ₯Ό λ°λ λ° κΉμ§ 걸리λ μκ°μ μΌλ§μΈμ§ κ³μ°νλ νλ‘κ·Έλ¨μ μμ±νμμ€.
첫째μ€μ λμμκ°μ (1 <= N <= 30,000), ν΅λ‘μ κ°μ (1 <= M <= 200,000), 1<=C<=N)μ΄ μ£Όμ΄μ§λ€.
λμ§Έμ€λΆν° Xμμ Yλ‘ μ΄μ΄μ§λ ν΅λ‘μ κ°μ€μΉ μ¦ (X, Y, X)κ° μ£Όμ΄μ§λ€.
μ λ ₯ μμ
3 2 1
1 2 4
1 3 2
μΈ¨λ ₯μμ
2 4
βοΈ μμλ μκ°
1h 30min
β¨ μλ μ½λ
μ΄μ λ¬Έμ λ λ Έλμ κ°μ μ κ°μκ° μ μ΄ ν루μ΄λμμ λ¬Έμ λ‘λ κ°λ₯νμ§λ§, μ΄λ¬Έμ λ λ Έλμ κ°μ μ κ°μκ° κ΅μ₯ν λ§μ μΌλ° λ€μ΅μ€νΈλΌλ‘λ νμ΄κ° λΆκ°λ₯ν©λλ€. λ°λΌμ κ°μ λ μ°μ μμνλ₯Ό μ¬μ©ν λ€μ΅μ€νΈλΌλ₯Ό νμ©ν΄μΌν©λλ€.
λ°λΌμ μκ°μ΄ λ§μ΄ μμλμμ΅λλ€
μ°Έκ³ μ© λΈλ‘κ·Έ : https://kimig.tistory.com/30
μ΄λ¬Έμ κ° μ΅λ¨κ±°λ¦¬λ‘ μΉνλ μ μμλ μ΄μ λ κ°μ₯ λ¨Ό λμκΉμ§ λλ¬νλ μκ°μ΄ λͺ¨λ λμμ μ λ³΄κ° μ λ¬λλ μκ°μ΄κ³ λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ΄ μ§κ³ λμ΄κ° λ Έλμ κ°μκ° μ λ³΄κ° μ λ¬λ λμμ μμ΄κΈ° λλ¬Έμ λλ€.
π μλ‘κ² μκ²λ λ΄μ©
λ°©λ¬Ένμ§μμ λμ, λΏμ§μλ λμλ₯Ό λνλΌλ INFλ₯Ό μ§μ ν΄μ£Όλλ°
987654321λ‘ μ§μ νλ©΄ μλ¦Ώμ ν리μ§μκ³ νΈμνκ² μ§νν μ μμ΅λλ€!