[백준] 1238 파티
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX_COST = 987654321;
const int MAX_V = 1000;
int V;
vector <pair<int, int>> adj[MAX_V];
vector <pair<int, int>> adj_reverse[MAX_V];
int x;
vector<int> dist(MAX_V, MAX_COST);
vector<int> dist_reverse(MAX_V, MAX_COST);
void priority_queue_dijkstra() {
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, x));
dist[x] = 0;
while (!pq.empty()) {
int curNode = pq.top().second;
int curW = -pq.top().first;
pq.pop();
for (int i = 0; i < adj[curNode].size(); i++) {
int nextNode = adj[curNode][i].first;
int& nextW = adj[curNode][i].second;
if (dist[nextNode] > curW + nextW) {
dist[nextNode] = curW + nextW;
pq.push({ -dist[nextNode], nextNode });
}
}
}
}
void priority_queue_dijkstra_reverse() {
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, x));
dist_reverse[x] = 0;
while (!pq.empty()) {
int curNode = pq.top().second;
int curW = -pq.top().first;
pq.pop();
for (int i = 0; i < adj_reverse[curNode].size(); i++) {
int nextNode = adj_reverse[curNode][i].first;
int& nextW = adj_reverse[curNode][i].second;
if (dist_reverse[nextNode] > curW + nextW) {
dist_reverse[nextNode] = curW + nextW;
pq.push({ -dist_reverse[nextNode], nextNode });
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
for (int i = 0; i < MAX_V; ++i) {
adj[i].clear();
adj_reverse[i].clear();
dist[i] = MAX_COST;
dist_reverse[i] = MAX_COST;
}
int n, m;
cin >> n >> m >> x;
x--;
V = n;
for (int i = 0; i < m; ++i) {
int u, v, t;
cin >> u >> v >> t;
u--;
v--;
adj[u].push_back({ v, t });
adj_reverse[v].push_back({ u, t });
}
priority_queue_dijkstra();
priority_queue_dijkstra_reverse();
int maxT = -1;
for (int i = 0; i < n; ++i) {
maxT = max(maxT, dist[i] + dist_reverse[i]);
}
cout << maxT;
return 0;
}