일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준
- 자바
- Paging
- HTTP
- 스프링부트
- 코드리뷰
- 의존성
- JUnit5
- 세션
- 트랜잭션
- Spring Batch
- 서블릿
- yml
- AWS
- 우테코
- 프로그래머스
- CircuitBreaker
- Docker
- REDIS
- 프리코스
- JPA
- 미션
- 우아한테크코스
- Level2
- mock
- AOP
- 우아한세미나
- MSA
- 스프링 부트
- 레벨2
Archives
- Today
- Total
늘
acmicpc_1753(최단경로) 본문
728x90
다익스트라 알고리즘을 처음 배우고 풀었던 문제였다. 다익스트라만 적용하면 금방 풀리는 쉬운 문제였다..!
#include <iostream>
#include <queue>
#include <vector>
#include <utility>
using namespace std;
int answer[20001];//최소 비용
vector<pair<int, int>> line[300001];//<거리, 연결상대><자신과 연결> 간선
int INF = 2000010;
void dijstra(int start) {
answer[start] = 0;
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, start));
while (!pq.empty()) {
int current = pq.top().second;
int distance = -pq.top().first;//최소 힙으로 변환
pq.pop();
if (answer[current] < distance) {
continue;
}
for (int i = 0; i < line[current].size(); ++i) {
//선택된 노드의 인접노드
int next = line[current][i].second;
int NextD = answer[current] + line[current][i].first;
if (NextD < answer[next]) {
answer[next] = NextD;
pq.push(make_pair(-NextD, next));
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int x, e;
cin >> x >> e;//x 정점의 갯수 e 간선의 갯수
int a, b, c;
int k;
cin >> k;//시작 점
for (int i = 1; i <= x; ++i) {
answer[i] = INF;
}
for (int i = 1; i <= e; ++i) {
cin >> a >> b >> c;
line[a].push_back(make_pair(c, b));
}
dijstra(k);
for (int i = 1; i <= x; ++i) {
if (answer[i] != INF) {
cout << answer[i] << endl;
}
else {
cout << "INF" << endl;
}
}
}
728x90
Comments