백준알고리즘
최소비용 구하기
먼지의삶
2019. 9. 29. 01:12
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include<iostream>
#include<vector>
#include<queue>
#include<utility>
using namespace std;
class PAIR {
public:
int x, y;
PAIR(int x, int y) {
this->x = x;
this->y = y;
}
};
int tc,n, d, c;
vector<PAIR> map[20000];
int visit[20000];
//bool dist[20000];
int node, edge;
int INF = 987654321;
int dist[20000];
void dijkstra(int start) {
visit[start] = 0;
priority_queue<pair<int, int> > pq;
pq.pop();
//if (visit[current] < distance) continue;
for(int i = 0; i< map[current].size(); i++){
int next = visit[current] + map[current][i].y;
int before = visit[map[current][i].x];
if (next < before) {
visit[map[current][i].x] = next;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> node >> edge;
for (int i = 0; i < node; i++) {
visit[i] = INF;
}
for (int i = 0; i < edge; i++) {
int a, b, c;
cin >> a >> b >> c;
map[a - 1].push_back(PAIR(b-1,c));
//map[b - 1].push_back({ a - 1,c });
}
int start, ending;
cin >> start >> ending;
dijkstra(start-1);
cout << visit[ending - 1]<<'\n';
/*for (int i = 0; i < node; i++) {
if (visit[i] != INF)
cout << visit[i] << '\n';
else
cout << "INF" << '\n';
}*/
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
똑같은 다익스트라 문제