-
Notifications
You must be signed in to change notification settings - Fork 0
[최단경로] 1126 #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[최단경로] 1126 #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // 파티 | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <queue> | ||
| #include <algorithm> | ||
|
|
||
| using namespace std; | ||
|
|
||
| const int INF = 1e9; // 큰 값 | ||
|
|
||
| vector<pair<int, int>> graph[1001]; // 인접 리스트 그래프 | ||
|
|
||
| // X에서 다른 마을로 가는 최단 거리 구하기(다익스트라 알고리즘 이용) | ||
| vector<int> dijkstra(int start, int n) { | ||
| vector<int> dist(n + 1, INF); | ||
| priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; | ||
|
|
||
| dist[start] = 0; | ||
| pq.push({0, start}); | ||
|
|
||
| while (!pq.empty()) { | ||
| int cur_dist = pq.top().first; | ||
| int cur_node = pq.top().second; | ||
| pq.pop(); | ||
|
|
||
| if (cur_dist > dist[cur_node]) continue; | ||
|
|
||
| for (auto &edge : graph[cur_node]) { | ||
| int next_node = edge.first; | ||
| int weight = edge.second; | ||
|
|
||
| if (dist[cur_node] + weight < dist[next_node]) { | ||
| dist[next_node] = dist[cur_node] + weight; | ||
| pq.push({dist[next_node], next_node}); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return dist; | ||
| } | ||
|
|
||
| // 각 마을에서 X로 가는 최단 거리 계산 | ||
| int findMax(int x, int n, vector<int> &dist_from_x){ | ||
| int max_time = 0; | ||
|
|
||
| for (int i = 1; i <= n; i++) { | ||
| if (i == x) continue; | ||
|
|
||
| vector<int> dist_to_x = dijkstra(i, n); | ||
| max_time = max(max_time, dist_to_x[x] + dist_from_x[i]); | ||
| } | ||
|
|
||
| return max_time; | ||
| } | ||
|
|
||
| int main() { | ||
|
|
||
| // 입력 | ||
| int n, m, x; | ||
| cin >> n >> m >> x; | ||
|
|
||
| for (int i = 0; i < m; i++) { | ||
| int u, v, t; | ||
| cin >> u >> v >> t; | ||
| graph[u].push_back({v, t}); | ||
| } | ||
|
|
||
| // 연산 | ||
| vector<int> dist_from_x = dijkstra(x, n); | ||
| int max_time = findMax(x, n, dist_from_x); | ||
|
|
||
| // 출력 | ||
| cout << max_time << endl; | ||
|
|
||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // 드래곤 커브 | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <set> | ||
|
|
||
| using namespace std; | ||
|
|
||
| // 방향 이동 배열 (우, 상, 좌, 하) | ||
| const int dx[] = {1, 0, -1, 0}; | ||
| const int dy[] = {0, -1, 0, 1}; | ||
|
|
||
| // 드래곤 커브 생성 | ||
| void generateDragonCurve(vector<pair<int, int>> &curve, int g) { | ||
| for (int generation = 0; generation < g; ++generation) { | ||
| int size = curve.size(); | ||
| pair<int, int> end = curve.back(); | ||
| for (int i = size - 2; i >= 0; --i) { | ||
| int x = curve[i].first; | ||
| int y = curve[i].second; | ||
| // 회전 변환 | ||
| int newX = end.first - (y - end.second); | ||
| int newY = end.second + (x - end.first); | ||
| curve.push_back({newX, newY}); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 1x1 정사각형의 개수 계산 | ||
| int calSquare(vector<vector<bool>> &grid){ | ||
|
|
||
| int squareCount = 0; | ||
| for (int i = 0; i < 100; ++i) { | ||
| for (int j = 0; j < 100; ++j) { | ||
| if (grid[i][j] && grid[i + 1][j] && grid[i][j + 1] && grid[i + 1][j + 1]) { | ||
| ++squareCount; | ||
| } | ||
| } | ||
| } | ||
| return squareCount; | ||
|
|
||
| } | ||
|
|
||
| int main() { | ||
| int n; | ||
| cin >> n; | ||
|
|
||
| // 101x101 격자를 초기화 (격자는 0~100까지 사용) | ||
| vector<vector<bool>> grid(101, vector<bool>(101, false)); | ||
|
|
||
| // 입력 및 드래곤 커브 생성 | ||
| for (int i = 0; i < n; ++i) { | ||
| int x, y, d, g; | ||
| cin >> x >> y >> d >> g; | ||
|
|
||
| // 초기 드래곤 커브 설정 | ||
| vector<pair<int, int>> curve; | ||
| curve.push_back({x, y}); | ||
| curve.push_back({x + dx[d], y + dy[d]}); | ||
|
|
||
| // 드래곤 커브 생성 | ||
| generateDragonCurve(curve, g); | ||
|
|
||
| // 격자에 드래곤 커브 표시 | ||
| for (auto c : curve) { | ||
| int cx = c.first; | ||
| int cy = c.second; | ||
| if (cx >= 0 && cx <= 100 && cy >= 0 && cy <= 100) { | ||
| grid[cx][cy] = true; | ||
| } | ||
| } | ||
|
Comment on lines
+64
to
+70
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2. 문제에서 드래곤 커브는 격자의 크기를 벗어나지 않는다고 했으니 |
||
| } | ||
|
|
||
| int squareCount = calSquare(grid); | ||
|
|
||
| // 출력 | ||
| cout << squareCount << endl; | ||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // 키 순서 | ||
| #include <iostream> | ||
| #include <vector> | ||
| using namespace std; | ||
|
|
||
| const int INF = 1e9; | ||
|
|
||
| // 플로이드-워셜 알고리즘으로 모든 쌍의 관계 계산한다. | ||
| void calGraph(vector<vector<bool>> &graph, int n){ | ||
|
|
||
| for (int k = 1; k <= n; ++k) { | ||
| for (int i = 1; i <= n; ++i) { | ||
| for (int j = 1; j <= n; ++j) { | ||
| if (graph[i][k] && graph[k][j]) { | ||
| graph[i][j] = true; | ||
|
Comment on lines
+11
to
+15
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 배운 개념을 활용해서 잘 구현해주셨네요 👍👍👍 |
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // 자신의 키 순위를 알 수 있는 학생 찾아서 반환한다. | ||
| int findAnswer(vector<vector<bool>> &graph, int n){ | ||
| int count = 0; | ||
|
|
||
| for (int i = 1; i <= n; ++i) { | ||
| int smaller = 0, larger = 0; | ||
|
|
||
| for (int j = 1; j <= n; ++j) { | ||
| if (graph[i][j]) smaller++; // i보다 큰 학생 수 | ||
| if (graph[j][i]) larger++; // i보다 작은 학생 수 | ||
| } | ||
|
|
||
| if (smaller + larger == n - 1) { | ||
| count++; // 자신보다 크거나 작은 학생 수가 정확히 N-1이면 순위를 알 수 있음 | ||
| } | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| int main() { | ||
| int n, m; // 학생들의 수 n (2 ≤ n ≤ 500), 두 학생 키를 비교한 횟수 m (0 ≤ m ≤ n(n-1)/2) | ||
|
|
||
| // 입력 | ||
| cin >> n >> m; | ||
| vector<vector<bool>> graph(n + 1, vector<bool>(n + 1, false)); | ||
| for (int i = 0; i < m; ++i) { | ||
| int a, b; | ||
| cin >> a >> b; | ||
| graph[a][b] = true; // a < b | ||
| } | ||
|
|
||
| // 연산 | ||
| calGraph(graph, n); | ||
| int answ = findAnswer(graph, n); | ||
|
|
||
| // 출력 | ||
| cout << answ << endl; | ||
|
|
||
| return 0; | ||
| } | ||
|
Comment on lines
+43
to
+63
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메인함수 깔끔하고 좋습니다👍👍👍 |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2. 격자의 크기를 상수 변수로 선언해서 사용하면 가독성도 좋아지고 실수할 일이 줄어들겠네요 🥰