회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
메모리 초과가 납니다..
11779번 - 최소비용 구하기 2
qudwjd4296
3년 전
0
인접리스트 다익스트라로 구현을 했는데도 메모리 초과가 납니다.. 뭐가 문제일까요 ㅠㅠ
#include <iostream> #include <vector> #include <stack> using namespace std; vector<pair<int,int>> dijkstra(int v, vector<vector<pair<int,int>>>& weight, int n) { vector<bool> visited(n, false); vector<pair<int,int>> dist(n, pair<int,int>(-1, INT32_MAX)); dist[v].second = 0; for (int i = 0; i < n - 1; i++) { int nextnode = -1; int nextdist = INT32_MAX; for (int j = 0; j < n; j++) { if (visited[j] == false && dist[j].second < nextdist) { nextnode = j; nextdist = dist[j].second; } } if (nextnode == -1) { break; } visited[nextnode] = true; for (auto t : weight[nextnode]) { if (visited[t.first] == false && dist[nextnode].second + t.second < dist[t.first].second) { dist[t.first].first = nextnode; dist[t.first].second = dist[nextnode].second + t.second; } } } return dist; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<vector<pair<int,int>>> weight(n); for (int i = 0; i < m; i++) { int src, dest, cost; cin >> src >> dest >> cost; weight[src - 1].push_back(pair<int,int>(dest - 1, cost)); } int src, dest; cin >> src >> dest; vector<pair<int,int>> answer = dijkstra(src-1, weight, n); cout << answer[dest - 1].second << "\n"; stack<int> s; int curnode = dest; while (answer[curnode - 1].first != -1) { s.push(curnode); curnode = answer[curnode - 1].first; if (curnode == 0) { break; } } s.push(src); cout << s.size() << "\n"; while (!s.empty()) { cout << s.top() << " "; s.pop(); } return 0; }
댓글을 작성하려면
로그인
해야 합니다.
qudwjd4296 3년 전
인접리스트 다익스트라로 구현을 했는데도 메모리 초과가 납니다.. 뭐가 문제일까요 ㅠㅠ