티스토리 뷰
14284번: 간선 이어가기 2
정점 n개, 0개의 간선으로 이루어진 무방향 그래프가 주어진다. 그리고 m개의 가중치 간선의 정보가 있는 간선리스트가 주어진다. 간선리스트에 있는 간선 하나씩 그래프에 추가해 나갈 것이다.
www.acmicpc.net
백준 소스코드 [C++] 14284 간선 이어가기 2
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
typedef long long ll;
using namespace std;
int n, m,u,v,w;
vector<pair<int, int>> arr[5001];
vector<int> dijkstra(int a) {
priority_queue<pair<int, int>> pq;
vector<int>res(n,INT_MAX);
res[a] = 0;
pq.push({ 0,a });
while (pq.empty() != true) {
int cost = -pq.top().first;
int here = pq.top().second;
pq.pop();
for (int i = 0;i < arr[here].size();i++) {
int there = arr[here][i].first;
int nextcost = cost + arr[here][i].second;
if (res[there] > nextcost) {
res[there] = nextcost;
pq.push({ -nextcost,there });
}
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0;i < m;i++) {
cin >> u >> v >> w;
arr[u - 1].push_back({ v - 1,w });
arr[v - 1].push_back({ u - 1,w });
}
int a, b;
cin >> a >> b;
cout << dijkstra(a-1)[b-1];
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 11869 님블 (0) | 2020.11.26 |
---|---|
백준 소스코드 [C++] 11868 님 게임 2 (0) | 2020.11.25 |
백준 소스코드 [C++] 11659 구간 합 구하기 4 (0) | 2020.11.22 |
백준 소스코드 [C++] 1629 곱셈 (0) | 2020.11.22 |
백준 소스코드 [C++] 11724 연결 요소의 개수 (0) | 2020.11.16 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 최단경로문제
- 포드 풀커슨 알고리즘
- rxswift
- 네트워크 유량
- 네트워크 플로우
- CompositionalLayout
- Testable
- test coverage
- 최대 매칭
- 벨만포드 알고리즘
- WWDC21
- 최단경로 문제
- WWDC16
- CPU와 Memory
- 다익스트라 시간복잡도
- 컴퓨터 추상화
- IOS
- mach-o
- MeTal
- 코딩대회
- 강한 순환 참조
- 부스트캠프 6기
- HIG
- WWDC17
- WWDC19
- 에드몬드 카프 알고리즘
- 벨만포드 시간복잡도
- observeOn
- State Restoration
- 최단경로 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함