Notice
Recent Posts
Recent Comments
tony9402
[백준 1916] 최소비용 구하기 본문
반응형
백준 1916 최소비용 구하기
소스 코드 메모(해설은 아직) - 우선순위 큐를 이용한 다익스트라 공부중
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 76 | #include<cstdio> #include<queue> #include<vector> #include<iostream> #include<algorithm> const int INF = 0x3f3f3f3f; using namespace std; vector<vector<pair<int, int>>> map; vector<bool> visit; priority_queue<pair<int, int>> pq; vector<int> money; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; map.resize(n + 1); visit.resize(n + 1); money.resize(n + 1); int m; cin >> m; int from, to, cost; int start, end; fill(money.begin(), money.end(), INF); for (int i = 0; i < m; i++) { cin >> from >> to >> cost; int vcheck = 0; for (; vcheck < map[from].size(); vcheck++) { if (map[from][vcheck].first == to && map[from][vcheck].second > cost) { map[from][vcheck].second = cost; break; } } if (vcheck == map[from].size()) { map[from].push_back(make_pair(to, cost)); } } cin >> start >> end; pq.push(make_pair(0, start)); money[start] = 0; while (!pq.empty()) { pair<int, int> now = pq.top(); pq.pop(); for (int i = 0; i < map[now.second].size(); i++) { if (money[map[now.second][i].first] > money[now.second] + map[now.second][i].second) { money[map[now.second][i].first] = money[now.second] + map[now.second][i].second; pq.push(make_pair(-map[now.second][i].second, map[now.second][i].first)); } } } printf("%d", money[end]); return 0; } | cs |
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 | #include<cstdio> #include<queue> #include<vector> #include<iostream> #include<algorithm> const int INF = 0x3f3f3f3f; using namespace std; vector<vector<pair<int, int>>> map; vector<bool> visit; priority_queue<pair<int, int>> pq; vector<int> money; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; map.resize(n + 1); visit.resize(n + 1); money.resize(n + 1); int m; cin >> m; int from, to, cost; int start, end; fill(money.begin(), money.end(), INF); for (int i = 0; i < m; i++) { cin >> from >> to >> cost; map[from].push_back(make_pair(to, cost)); } cin >> start >> end; pq.push(make_pair(0, start)); money[start] = 0; while (!pq.empty()) { pair<int, int> now = pq.top(); pq.pop(); now.first *= -1; for (int i = 0; i < map[now.second].size(); i++) { if (money[map[now.second][i].first] > money[now.second] + map[now.second][i].second) { money[map[now.second][i].first] = money[now.second] + map[now.second][i].second; pq.push(make_pair(-map[now.second][i].second, map[now.second][i].first)); } } } printf("%d", money[end]); return 0; } | cs |
반응형
'알고리즘 > Baekjoon Online Judge' 카테고리의 다른 글
[백준 1504] 특정한 최단 경로 (0) | 2018.08.31 |
---|---|
[백준 1753] 최단경로 (0) | 2018.08.31 |
[백준 7569] 토마토(3차원) (0) | 2018.08.23 |
[백준 7576] 토마토(2차원) (0) | 2018.08.23 |
[백준 4179] 불! (0) | 2018.08.22 |
Comments