반응형
https://www.acmicpc.net/problem/10282
10282번: 해킹
최흉최악의 해커 yum3이 네트워크 시설의 한 컴퓨터를 해킹했다! 이제 서로에 의존하는 컴퓨터들은 점차 하나둘 전염되기 시작한다. 어떤 컴퓨터 a가 다른 컴퓨터 b에 의존한다면, b가 감염되면
www.acmicpc.net
[ 문제풀이 ]
1. vector에 각 정점의 간선을 저장합니다.
2. visited 배열을 초기화해줍니다.
3. dijkstra를 이용하여 dist의 최댓값을 ans에 저장합니다.
4. visited 배열 중 값이 변한 idx가 있다면 cnt++를 해줍니다.
5. cnt와 ans를 출력해줍니다.
[ 소스코드 ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #include<iostream> #include<queue> #include<vector> #include<cstring> using namespace std; int n, d, c; int visited[10001]; struct cmp { bool operator()(pair<int, int> right, pair<int, int> left) { return left.second < right.second; } }; int main() { int T; scanf("%d", &T); for (int t = 0; t < T; t++) { scanf("%d %d %d", &n, &d, &c); vector<pair<int, int>> list[10001]; memset(visited, -1, 4 * (n+1)); for (int i = 0; i < d; i++) { int a, b, s; scanf("%d %d %d", &a, &b, &s); list[b].push_back({ a,s }); } priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq; pq.push({ c,0 }); int ans = 0; while (!pq.empty()) { int cur = pq.top().first; int dist = pq.top().second; pq.pop(); if (visited[cur] != -1 && visited[cur] < dist) continue; visited[cur] = dist; ans = max(ans, dist); for (auto next : list[cur]) { if (visited[next.first] == -1 || visited[next.first] > next.second + dist) { visited[next.first] = next.second + dist; pq.push({ next.first, next.second + dist }); } } } int cnt = 0; for (int i = 1; i <= n; i++) { if (visited[i] != -1) { cnt++; } } printf("%d %d\n", cnt, ans); } } | cs |
반응형
'백준' 카테고리의 다른 글
[ 백준 ] 17386번 - 선분 교차 1 (C++) (0) | 2022.11.21 |
---|---|
[ 백준 ] 1774번 - 우주신과의 교감 (C++) (0) | 2022.11.20 |
[ 백준 ] 3584번 - 가장 가까운 공통 조상 (C++) (0) | 2022.11.18 |
[ 백준 ] 5529번 - 저택 (C++) (0) | 2022.11.17 |
[ 백준 ] 16933번 - 벽 부수고 이동하기 3 (C++) (0) | 2022.11.16 |