반응형
문제
https://www.acmicpc.net/problem/1916
접근방법
1) 접근 사고
2) 시간 복잡도
3) 배운 점
4) PS
정답 코드
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include<bits/stdc++.h>
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pii pair<int,int>
#define mp(X,Y) make_pair(X,Y)
#define mt(X,Y) make_tuple(X,Y)
#define mtt(X,Y,Z) make_tuple(X,Y,Z)
#define ll long long
#define sz(v) (int)(v).size()
using namespace std;
const int INF = 987654321;
const int MAX = 1001;
vector<pair<int,int>> edge[MAX];
ll dist[MAX];
int n, m;
int main(void)
{
fastio;
cin >> n >> m;
for(int i = 0; i < m; i++)
{
int st, ed, value;
cin >> st >> ed >>value;
edge[st].push_back({value, ed});
}
fill(dist, dist + MAX, INF);
int start, finish;
cin >> start >> finish;
priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
pq.push({0,start});
dist[start] = 0;
while(!pq.empty())
{
int cur = pq.top().second;
ll cost = pq.top().first;
pq.pop();
if(dist[cur] < cost)
continue;
for(auto idx : edge[cur])
{
int neighbor = idx.second;
ll neighborCost = idx.first + cost;
if(dist[neighbor] > neighborCost)
{
dist[neighbor] = neighborCost;
pq.push({neighborCost, neighbor});
}
}
}
cout << dist[finish] << "\n";
}
|
cs |
반응형
'백준문제풀이 > Dijkstrea' 카테고리의 다른 글
2211번-네트워크 복구 (0) | 2021.08.19 |
---|---|
1922번-네트워크 연결 (0) | 2021.08.19 |
1854번-K번째 최단경로 찾기 (0) | 2021.08.19 |
1753번-최단경로 (0) | 2021.08.19 |
16681번-등산 (0) | 2021.08.19 |