티스토리 뷰
https://www.acmicpc.net/problem/11779
11779번: 최소비용 구하기 2
첫째 줄에 도시의 개수 n(1≤n≤1,000)이 주어지고 둘째 줄에는 버스의 개수 m(1≤m≤100,000)이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스�
www.acmicpc.net
백준 소스코드 [C++] 11779 최소비용 구하기 2
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
using namespace std;
int V,E,u,v,e,w,s,d;
vector <pair<int, int>> adj[1000];
vector<int> dijkstra(int src) {
priority_queue <pair<int, int> > pq;
vector<int> dist(V, INT_MAX);
vector <int> path[1000];
dist[src] = 0;
path[src].push_back(src);
pq.push({ 0,src });
while (pq.empty() != true) {
int cost = -pq.top().first;
int here = pq.top().second;
pq.pop();
if (dist[here] < cost) continue;
for (int i = 0;i < (int)adj[here].size();i++) {
int there = adj[here][i].first;
int nextdist = cost + adj[here][i].second;
if (dist[there] > nextdist) {
dist[there] = nextdist;
pq.push({ -nextdist,there });
path[there] = path[here];
path[there].push_back(there);
}
}
}
cout << dist[d - 1] << '\n';
cout << path[d - 1].size() << '\n';
for(int i=0;i< (int)path[d - 1].size();i++) cout<<path[d-1][i]+1<<" ";
return dist;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> V;
cin >> E;
for (int i = 0;i < E;i++) {
cin >> u >> v >> w;;
adj[u - 1].push_back(make_pair(v - 1, w));
}
cin >> s >> d;
vector<int> res = dijkstra(s - 1);
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 18223 민준이와 마산 그리고 건우 (0) | 2020.09.01 |
---|---|
백준 소스코드 [C++] 4485 녹색 옷 입은 애가 젤다지 (0) | 2020.09.01 |
백준 소스코드 [C++] 2665 미로만들기 (0) | 2020.09.01 |
백준 소스코드 [C++] 6497 전력난 (0) | 2020.08.30 |
백준 소스코드 [C++] 1504 특정한 최단 경로 (0) | 2020.08.30 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- observeOn
- CompositionalLayout
- WWDC16
- WWDC19
- 최단경로문제
- rxswift
- WWDC17
- 최단경로 알고리즘
- IOS
- State Restoration
- 강한 순환 참조
- 다익스트라 시간복잡도
- mach-o
- 네트워크 플로우
- 에드몬드 카프 알고리즘
- test coverage
- 벨만포드 알고리즘
- MeTal
- 최단경로 문제
- Testable
- WWDC21
- 최대 매칭
- HIG
- 네트워크 유량
- CPU와 Memory
- 코딩대회
- 부스트캠프 6기
- 벨만포드 시간복잡도
- 컴퓨터 추상화
- 포드 풀커슨 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함