-
Notifications
You must be signed in to change notification settings - Fork 0
/
최소비용구하기.cpp
54 lines (46 loc) · 1.13 KB
/
최소비용구하기.cpp
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
#include <string.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
struct node {
int x, cost;
};
int n, m, s, e;
bool visit[1001];
int memo[1001];
map<int, vector<node>> bus;
bool cmp(node a, node b) {
return a.cost < b.cost;
}
void dfs(int num, int cost) {
if (num == e)
return;
for (auto& elem : bus[num]) {
int nextCost = cost + elem.cost;
if (!visit[elem.x] && nextCost < memo[elem.x]) {
visit[elem.x] = true;
memo[elem.x] = nextCost;
dfs(elem.x, nextCost);
visit[elem.x] = false;
}
}
}
int main() {
int a, b, c;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &a, &b, &c);
bus[a].push_back({b, c});
}
scanf("%d %d", &s, &e);
for (int i = 0; i < 1001; i++) {
visit[i] = false;
memo[i] = 2000000000;
}
for (auto it = bus.begin(); it != bus.end(); it++)
sort(bus[it->first].begin(), bus[it->first].end(), cmp);
dfs(s, 0);
printf("%d", memo[e]);
}