Skip to content

Commit 02fc5b4

Browse files
authored
[BOJ] 1238 파티 (G3)
1 parent 3720c48 commit 02fc5b4

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

박예진/7주차/260211.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <vector>
4+
#include <queue>
5+
using namespace std;
6+
7+
/*
8+
i -> X -> i 왕복하는 최단시간 중 최댓값
9+
X -> 모든 노드 (정방향)
10+
모든 노드 -> X (역방향)
11+
*/
12+
13+
struct Node {
14+
int node, cost;
15+
};
16+
17+
struct cmp {
18+
bool operator()(const Node &n1, const Node &n2) {
19+
return n1.cost > n2.cost; // 오름차순
20+
}
21+
};
22+
23+
int N, X, M;
24+
vector<vector<Node>> graph;
25+
vector<vector<Node>> rgraph;
26+
27+
vector<int> dijkstra(int start, vector<vector<Node>> &graph){
28+
priority_queue<Node, vector<Node>, cmp> pq;
29+
vector<int> dist(N + 1, 1e9);
30+
dist[start] = 0;
31+
pq.push({start, dist[start]});
32+
33+
while(!pq.empty()) {
34+
Node now = pq.top();
35+
pq.pop();
36+
37+
if (dist[now.node] < now.cost) continue;
38+
39+
for(int i = 0; i < graph[now.node].size(); i++){
40+
int nextnode = graph[now.node][i].node;
41+
int nextcost = graph[now.node][i].cost;
42+
43+
if (dist[nextnode] > dist[now.node] + nextcost) {
44+
dist[nextnode] = dist[now.node] + nextcost;
45+
pq.push({nextnode, dist[nextnode]});
46+
}
47+
}
48+
}
49+
return dist;
50+
}
51+
52+
int main() {
53+
ios::sync_with_stdio(false);
54+
cin.tie(NULL); cout.tie(NULL);
55+
56+
cin >> N >> M >> X;
57+
graph.resize(N + 1);
58+
rgraph.resize(N + 1);
59+
for(int i = 0; i < M; i++){
60+
int a, b, c;
61+
cin >> a >> b >> c;
62+
graph[a].push_back({b, c});
63+
rgraph[b].push_back({a, c});
64+
}
65+
66+
vector<int> v1 = dijkstra(X, graph); // 정방향
67+
vector<int> v2 = dijkstra(X, rgraph); // 역방향
68+
69+
int ans = 0;
70+
for(int i = 1; i <= N; i++){
71+
ans = max(ans, v1[i] + v2[i]);
72+
}
73+
cout << ans;
74+
75+
return 0;
76+
}

0 commit comments

Comments
 (0)