Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 2075번
- DirectX12
- dx12
- BFS
- 2d 박스충돌
- lis응용
- 백준 c++ 24479번
- 그리기 연산
- 애니메이션
- 백준 1260 c++
- 24779
- 뱀과사다리게임
- 바이토닉 수열
- 백준 24444 c++
- C++
- Unreal
- 드래곤플라이트 모작
- Perforce
- 다음소수
- 11286번
- directx12 그리기 연산2
- 2565번
- unrealengine
- WinAPI
- dx12 정리
- 백준
- 다익스트라
- 랜더링 파이프라인
- 루트서명
- 4134번
Archives
- Today
- Total
game-1 님의 블로그
[백준] 1238번 C++ 파티 본문


풀이)
이 문제는 다익스트라 알고리즘을 이용하여 최단거리를 구하는 문제이다.
이때 학생이 x에 모인 후에 원래있던 곳으로 되돌아가는 거리까지를 추가하여 구해주면된다.
1번 마을(노드)에 있는 학생이 x번 까지 갔다가 1번 노드로 다시 돌아가야된다.
1- > x - > 1 여기서 1->x, x->1까지의 최단 거리를 구해준 후, 이 총 거리가 가장 긴 학생의 소요시간을 출력해주면 답이다.
코드)
#include<iostream>
#include<vector>
#include<queue>
#include<limits>
using namespace std;
int n, m, x;
vector<vector<pair<int, int>>> graph;
vector<int> dist;
const int INF = numeric_limits<int>::max();
int dijkstra(int start) {
dist.assign(n + 1, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({ 0,start }); // 시간, 시작노드
dist[start] = 0;
while (not pq.empty()){
int current = pq.top().second;
int distance = pq.top().first;
pq.pop();
if (dist[current] < distance) continue;
for (const auto& a : graph[current]) {
int nextnode = a.second;
int nextvalue = a.first + distance;
if (dist[nextnode] > nextvalue) {
dist[nextnode] = nextvalue;
pq.push({ nextvalue, nextnode });
}
}
}
return dist[x];
}
int dijkstra(int start, int end) {
dist.assign(n + 1, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({ 0,start }); // 시간, 시작노드
dist[start] = 0;
while (not pq.empty()) {
int current = pq.top().second;
int distance = pq.top().first;
pq.pop();
if (dist[current] < distance) continue;
for (const auto& a : graph[current]) {
int nextnode = a.second;
int nextvalue = a.first + distance;
if (dist[nextnode] > nextvalue) {
dist[nextnode] = nextvalue;
pq.push({ nextvalue, nextnode });
}
}
}
return dist[end];
}
int main()
{
cin >> n >> m >> x;
graph.resize(n + 1);
dist.assign(n + 1, INF);
// n명의 학생, m개의 단방향도로, x마을에서 모이기로함..
while (m--) {
int u, v,value;
cin >> u >> v >> value;
graph[u].push_back({ value,v });
}
int solve{};
for (int i = 1; i <= n; ++i) {
int tmp = dijkstra(i) +dijkstra(x, i);
if (solve < tmp) solve = tmp;
}
cout << solve;
}

'백준 문제풀이' 카테고리의 다른 글
| [백준] 2252번 C++ 줄 세우기 , 위상 정렬 (0) | 2025.02.15 |
|---|---|
| [백준]1916 번 C++ 최소 비용 구하기 컴파일에러 (1) | 2025.02.08 |
| [백준] c++ 13549번 숨바꼭질 3 (bfs풀이) (1) | 2025.02.06 |
| [백준] 1504번 C++ 특정한 최단 경로 (0) | 2025.02.05 |
| [백준] 2075번 N번째 큰 수 C++ (2) | 2025.02.03 |