diff --git "a/\353\260\260\353\213\254.cpp" "b/\353\260\260\353\213\254.cpp" new file mode 100644 index 0000000..c072422 --- /dev/null +++ "b/\353\260\260\353\213\254.cpp" @@ -0,0 +1,73 @@ +#include +using namespace std; + +// 인접 리스트: Graph[u] = { {v, cost}, ... } +vector> Graph[55]; + +// 각 마을까지의 최소 거리 저장 +vector Dist; + +// 다익스트라 알고리즘 +void Dijkstra(int N) +{ + // 우선순위 큐 (최소 비용이 우선되도록 음수로 저장) + priority_queue> PQ; + + // 시작점 push (비용 0, 노드 1) + PQ.push({0, 1}); + Dist[1] = 0; + + while (!PQ.empty()) + { + int currentCost = -PQ.top().first; // 원래 비용 복원 + int currentNode = PQ.top().second; + PQ.pop(); + + // 현재 노드에서 이동 가능한 모든 인접 노드 탐색 + for (int i = 0; i < Graph[currentNode].size(); i++) + { + int nextNode = Graph[currentNode][i].first; + int edgeCost = Graph[currentNode][i].second; + + // 현재까지 비용 + 다음 간선 비용 + int newCost = currentCost + edgeCost; + + // 더 짧은 경로 발견 시 갱신 + if (Dist[nextNode] > newCost) + { + Dist[nextNode] = newCost; + // 최소 비용 정렬을 위해 음수로 push + PQ.push({-newCost, nextNode}); + } + } + } +} + +int solution(int N, vector> road, int K) +{ + // 거리 배열 초기화 + Dist.assign(N + 1, 2e9); + + // 그래프 구성 + for (int i = 0; i < road.size(); i++) + { + int nodeA = road[i][0]; + int nodeB = road[i][1]; + int cost = road[i][2]; + + Graph[nodeA].push_back({nodeB, cost}); + Graph[nodeB].push_back({nodeA, cost}); + } + + // 다익스트라 실행 (1번 마을 시작) + Dijkstra(N); + + // K 이하로 도달 가능한 마을 수 계산 + int answer = 0; + for (int i = 1; i <= N; i++) + { + if (Dist[i] <= K) answer++; + } + + return answer; +} diff --git "a/\355\230\270\355\205\224 \353\214\200\354\213\244.cpp" "b/\355\230\270\355\205\224 \353\214\200\354\213\244.cpp" new file mode 100644 index 0000000..f2b99b9 --- /dev/null +++ "b/\355\230\270\355\205\224 \353\214\200\354\213\244.cpp" @@ -0,0 +1,50 @@ +#include +#include +#include +#include +using namespace std; + +int solution(vector> s_book_time) { + int answer(0), hour(0), min(0); + + // book_time[i][0] = 시작시간(분) + // book_time[i][1] = 종료시간(분) + 청소시간(10분) + vector> book_time(s_book_time.size(), vector(2, 0)); + + // 문자열 시각을 분 단위 정수로 변환 + for (int idx = 0; idx < s_book_time.size(); ++idx) { + + // 시작 시간 변환 + hour = stoi(s_book_time[idx][0].substr(0, 2)); + min = stoi(s_book_time[idx][0].substr(3, 2)); + book_time[idx][0] = hour * 60 + min; + + // 종료 시간 변환 + hour = stoi(s_book_time[idx][1].substr(0, 2)); + min = stoi(s_book_time[idx][1].substr(3, 2)); + book_time[idx][1] = hour * 60 + min + 10; // 퇴실 후 청소 + } + + //시작 시간이 빠른 순서대로 정렬 + sort(book_time.begin(), book_time.end()); + + priority_queue, greater> room; + + // 예약들을 순회하며 방 배정 + for (int idx = 0; idx < book_time.size(); ++idx) { + + int start = book_time[idx][0]; // 현재 예약 시작 시간 + int end = book_time[idx][1]; // 현재 예약 종료+청소 시간 + + while (!room.empty() && room.top() <= start) { + room.pop(); + } + + room.push(end); + + int room_size = room.size(); + answer = max(answer, room_size); + } + + return answer; +}