카테고리 없음
백준 1753 최단경로
레옹
2021. 7. 13. 11:34
분명 알고리즘은 맞는데 왜 틀린건지 모르겠고 몇시간을 고민하다가 드디어 해결방법을 찾았는데
입력 부분에서 반복 횟수를 E번이 아니라 V번으로 해서 틀렸었다
#include<cstdio>
#include<iostream>
#include<queue>
#include <string>
using namespace std;
const int INF = 987654321;
int V, E, K;
vector<pair<int, int>> adj[400000];
vector<int> dijkstra(int src) {
vector<int> dist(V + 1, INF);
dist[src] = 0;
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, src));
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 < adj[here].size(); ++i) {
int there = adj[here][i].first;
int nextDist = cost + adj[here][i].second;
if (dist[there] > nextDist) {
dist[there] = nextDist;
pq.push(make_pair(-nextDist, there));
}
}
}
for (int i = 1; i <= V; i++) {
int dis = dist[i];
if (dis == INF)
cout << "INF" << '\n';
else
cout << dis << '\n';
}
return dist;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> V >> E;
cin >> K;
for (int i = 0; i < E; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back(make_pair(v, w));
}
dijkstra(K);
return 0;
}