Skip to content

Commit

Permalink
2024-05-22 특정 거리의 도시 찾기
Browse files Browse the repository at this point in the history
  • Loading branch information
InSange committed May 21, 2024
1 parent 60f4d81 commit 5b65708
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 1 deletion.
143 changes: 143 additions & 0 deletions InSange/BFS/18352.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//#include <iostream>
//#include <queue>
//#include <vector>
//
//using namespace std;
//
//int N, M, K, X, a, b, minDist;
//deque<deque<int>> town;
//queue<int> q;
//vector<int> dist;
//vector<int> answer;
//
//void Solve()
//{
// cin >> N >> M >> K >> X;
// N++;
// town.assign(N, deque<int>());
// dist.assign(N, -1);
//
// for (int i = 0; i < M; i++)
// {
// cin >> a >> b;
//
// town[a].push_back(b);
// }
//
// minDist = -1;
// q.push(X);
// dist[X] = 0;
//
// while (!q.empty())
// {
// int cur = q.front();
// q.pop();
//
// for (const int& val : town[cur])
// {
// if (dist[val] != -1) continue;
// q.push(val);
// dist[val] = dist[cur] + 1;
// minDist = max(minDist, dist[val]);
// }
// }
//
// for (int i = 0; i < dist.size(); i++)
// {
// if (dist[i] == K) answer.push_back(i);
// }
//
// if (answer.size())
// {
// for (const int& val : answer)
// {
// cout << val << "\n";
// }
// }
// else cout << -1;
//}
//
//int main()
//{
// cin.tie(nullptr);
// ios::sync_with_stdio(false);
//
// Solve();
//
// return 0;
//}

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int N, M, K, X, A, B;
int cur, dist;
queue<pair<int, int>> q;
vector<vector<int>> towns;
vector<bool> visited;
priority_queue<int, vector<int>, greater<int>> answer;

void Solve()
{
cin >> N >> M >> K >> X;

N++;
towns.assign(N, vector<int>());
visited.assign(N, false);

while (M--)
{
cin >> A >> B;
towns[A].push_back(B);
}

q.push({ X, 0 });
visited[X] = true;

while (!q.empty())
{
cur = q.front().first;
dist = q.front().second;

q.pop();

if (dist == K)
{
answer.push(cur);
continue;
}

for (auto v : towns[cur])
{
if (visited[v]) continue;
q.push({ v, dist + 1 });
visited[v] = true;
}
}

if (answer.empty())
{
cout << -1;
return;
}

while (!answer.empty())
{
cout << answer.top() << "\n";
answer.pop();
}
return;
}

int main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);

Solve();

return 0;
}
3 changes: 2 additions & 1 deletion InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
| 9차시 | 2024.04.12 | 힙 | [Top K Frequent Words](https://leetcode.com/submissions/detail/1180988760/) | [#9](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)]
| 10차시 | 2024.04.30 | 스택 | [오아시스 재결합](https://www.acmicpc.net/problem/3015) | [#10](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/40)]
| 11차시 | 2024.05.03 | DFS | [숫자고르기](https://www.acmicpc.net/problem/2668) | [#11](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/41]
| 12차시 | 2024.05.08 | DP | [전깃줄](https://www.acmicpc.net/problem/2565) | [#11](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)]
| 12차시 | 2024.05.08 | DP | [전깃줄](https://www.acmicpc.net/problem/2565) | [#12](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)]
| 13차시 | 2024.05.22 | BFS | [특정 거리의 도시 찾기](https://www.acmicpc.net/problem/18352) | [#13](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)]
=======
---

0 comments on commit 5b65708

Please sign in to comment.