Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions 13_최단 경로/필수/1238.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#define MAX 1001
#define INF 1e9
using namespace std;

int N, M, X;
vector<pair<int, int>> graph[MAX];
int dist[MAX];

void dijkstra(int start) {
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, start));
dist[start] = 0;
while(!pq.empty()) {
int cost = -pq.top().first;
int here = pq.top().second;
pq.pop();
if(dist[here] < cost) continue;
for(int i=0; i<graph[here].size(); i++) {
int there = graph[here][i].first;
int nextDist = cost + graph[here][i].second;
if(dist[there] > nextDist) {
dist[there] = nextDist;
pq.push(make_pair(-nextDist, there));
}
}
}
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

cin >> N >> M >> X;
for(int i=0; i<M; i++) {
int a, b, t;
cin >> a >> b >> t;
graph[a].push_back(make_pair(b, t));
}
int result = 0;
for(int i=1; i<=N; i++) {
fill(dist, dist+MAX, INF);
dijkstra(i);
int temp = dist[X];
fill(dist, dist+MAX, INF);
dijkstra(X);
Copy link
Contributor

Choose a reason for hiding this comment

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

P3. dijkstra(X)는 한 번만 해도 되기 때문에 for문 전에 실행한 후 결과를 저장해서 사용하는 게 좋습니다! 😄

temp += dist[i];
result = max(result, temp);
}
cout << result;
return 0;
}