티스토리 뷰
https://www.acmicpc.net/problem/1753
1753번: 최단경로
첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1≤V≤20,000, 1≤E≤300,000) 모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. 둘째 줄에는 시작 정점의 번호 K(1≤K≤V)가 주어진다.
www.acmicpc.net
백준 소스코드 [C++] 1753 최단경로
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
using namespace std;
int v, e, start, s, d, weight;
vector <pair<int, int>> adj[20000];
vector<int> dijkstra(int src) {
priority_queue <pair<int, int> > pq;
vector<int> dist(v, INT_MAX);
dist[src] = 0;
pq.push({ 0,src });
while (pq.empty() != true) {
int cost = -pq.top().first;
int here = pq.top().second;
pq.pop();
if (dist[here] < cost) continue;
for (int i = 0;i < (int)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({ -nextdist,there });
}
}
}
return dist;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> v >> e;
cin >> start;
for (int i = 0;i < e;i++) {
cin >> s >> d >> weight;
adj[s-1].push_back(make_pair(d-1, weight));
}
vector<int> res=dijkstra(start-1);
for(int i=0;i<res.size();i++)
if (res[i]==INT_MAX) cout<<"INF"<<'\n';
else cout << res[i] << '\n';
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 1238 파티 (0) | 2020.08.26 |
---|---|
백준 소스코드 [C++] 1916 최소비용 구하기 (0) | 2020.08.26 |
백준 소스코드 [C++] 16390 Sheba’s Amoebas (0) | 2020.08.26 |
백준 소스코드 [C++] 15240 Paint bucket (0) | 2020.08.25 |
백준 소스코드 [C++] 3184 양 (0) | 2020.08.25 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- mach-o
- WWDC19
- 최단경로 문제
- CompositionalLayout
- CPU와 Memory
- WWDC21
- IOS
- 포드 풀커슨 알고리즘
- State Restoration
- 최대 매칭
- test coverage
- 최단경로문제
- 컴퓨터 추상화
- 에드몬드 카프 알고리즘
- 다익스트라 시간복잡도
- WWDC16
- MeTal
- Testable
- HIG
- rxswift
- 벨만포드 시간복잡도
- 강한 순환 참조
- 코딩대회
- observeOn
- 네트워크 유량
- 부스트캠프 6기
- 네트워크 플로우
- 벨만포드 알고리즘
- WWDC17
- 최단경로 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
글 보관함