Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions 배달.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <queue>
using namespace std;

// 인접 리스트: Graph[u] = { {v, cost}, ... }
vector<pair<int, int>> Graph[55];

// 각 마을까지의 최소 거리 저장
vector<int> Dist;

// 다익스트라 알고리즘
void Dijkstra(int N)
{
// 우선순위 큐 (최소 비용이 우선되도록 음수로 저장)
priority_queue<pair<int, int>> 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<vector<int>> 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;
}
50 changes: 50 additions & 0 deletions 호텔 대실.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<vector<string>> s_book_time) {
int answer(0), hour(0), min(0);

// book_time[i][0] = 시작시간(분)
// book_time[i][1] = 종료시간(분) + 청소시간(10분)
vector<vector<int>> book_time(s_book_time.size(), vector<int>(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<int, vector<int>, greater<int>> 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;
}