Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

14-wnsmir #56

wants to merge 2 commits into from

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Jan 9, 2025

πŸ”— 문제 링크

μ΄μ „λ¬Έμ œμ˜ 흐름을 이어받아 μ΅œλ‹¨κ±°λ¦¬λ¬Έμ œ ν•˜λ‚˜ 더 κ°€μ Έμ™€λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

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둜 μ§€μ •ν•˜λ©΄ 자릿수 ν‹€λ¦¬μ§€μ•Šκ³  νŽΈμ•ˆν•˜κ²Œ 진행할 수 μžˆμŠ΅λ‹ˆλ‹€!

@wnsmir wnsmir self-assigned this Jan 9, 2025
@wnsmir wnsmir changed the title Create 전보.py 14-wnsmir Jan 9, 2025
Copy link
Collaborator

@kangrae-jo kangrae-jo left a 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;
}

Comment on lines +15 to +27
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]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κ°€μ€‘μΉ˜ κ·Έλž˜ν”„μ—μ„œ λ‹€μ΅μŠ€νŠΈλΌλ₯Ό κ΅¬ν˜„ν•˜λŠ” κΈ°λ³Έ 둜직인 것 κ°™λ„€μš”!
직접 κ΅¬ν˜„ν•΄λ³Έ 것은 처음인데, λ‚˜λ¦„ μ§κ΄€μ μœΌλ‘œ 이해가 잘 λμŠ΅λ‹ˆλ‹€.
μ•žμœΌλ‘œλŠ” μ’€ 더 μ–΄λ €μš΄ λ‹€μ΅μŠ€νŠΈλΌλ„ λ§ˆμ£Όν•΄λ³΄κ³  μ‹ΆμŠ΅λ‹ˆλ‹·. πŸ‘

Copy link
Collaborator

@g0rnn g0rnn left a 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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants