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
77 changes: 77 additions & 0 deletions 13_최단 경로/필수/1238.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <iostream>
#include <vector>
#include <queue>
#include <climits>

using namespace std;

const int INF = 1e9;

vector<int> dijkstra(const vector<vector<pair<int, int>>>& graph, int start) {
vector<int> distance(graph.size(), INF);
distance[start] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push(make_pair(0, start));

while (!pq.empty()) {
int dist = pq.top().first;
int node = pq.top().second;
pq.pop();

if (distance[node] < dist) {
continue;
}

for (const auto& edge : graph[node]) {
int next_node = edge.first;
int next_dist = edge.second;
int cost = dist + next_dist;

if (cost < distance[next_node]) {
distance[next_node] = cost;
pq.push(make_pair(cost, next_node));
}
}
}

return distance;
}

int solution(int N, int M, int X, const vector<vector<int>>& edges) {
vector<vector<pair<int, int>>> graph(N + 1);
vector<vector<pair<int, int>>> reverse_graph(N + 1);

for (const auto& edge : edges) {
int u = edge[0];
int v = edge[1];
int w = edge[2];
graph[u].push_back(make_pair(v, w));
reverse_graph[v].push_back(make_pair(u, w));
}

vector<int> forward_distance = dijkstra(graph, X);
vector<int> backward_distance = dijkstra(reverse_graph, X);

int max_distance = 0;
for (int i = 1; i <= N; i++) {
int distance = forward_distance[i] + backward_distance[i];
max_distance = max(max_distance, distance);
}

Comment on lines +51 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 💯

return max_distance;
}

int main() {
int N, M, X;
cin >> N >> M >> X;
vector<vector<int>> edges(M, vector<int>(3));

for (int i = 0; i < M; i++) {
cin >> edges[i][0] >> edges[i][1] >> edges[i][2];
}

int answer = solution(N, M, X, edges);
cout << answer << "\n";

return 0;
}
59 changes: 59 additions & 0 deletions 13_최단 경로/필수/15685.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 101;
bool visited[MAX][MAX];

int dx[] = { 1, 0, -1, 0 }; // 동, 북, 서, 남
int dy[] = { 0, -1, 0, 1 };

int main() {
int N;
cin >> N;

vector<int> curve;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

방향을 저장하는 벡터네요. curve보다는 방향이라는 것을 나타낼 수 있는 이름이 좋을 것 같아요.


// 드래곤 커브 그리기
for (int i = 0; i < N; i++) {
int x, y, d, g;
cin >> x >> y >> d >> g;

curve.clear();
curve.push_back(d);

// 각 세대별로 이전 세대를 시계방향으로 90도 회전한 후 추가
for (int j = 0; j < g; j++) {
int size = curve.size();
for (int k = size - 1; k >= 0; k--) {
curve.push_back((curve[k] + 1) % 4);
}
}

// 드래곤 커브 방문 처리
visited[y][x] = true;

// 드래곤 커브 이동
for (int dir : curve) {
x += dx[dir];
y += dy[dir];
visited[y][x] = true;
}
}
Comment on lines +19 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수로 묶으면 좋을 것 같아요!


int count = 0;

// 정사각형 개수 세기
for (int i = 0; i < MAX - 1; i++) {
for (int j = 0; j < MAX - 1; j++) {
if (visited[i][j] && visited[i + 1][j] && visited[i][j + 1] && visited[i + 1][j + 1]) {
count++;
}
}
}
Comment on lines +47 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 함수로 묶을 수 있을 것 같아요!


cout << count << "\n";

return 0;
}
65 changes: 65 additions & 0 deletions 13_최단 경로/필수/2458.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
const int INF = 501; // 최대 n-1개의 간선을 지나므로 n * (가중치 최대값)

void init(int n, vector<vector<int>>& graph) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
graph[i][j] = INF; // 그래프 초기화. 두 노드 사이 값(가중치)
}
}
for (int i = 1; i <= n; i++) {
graph[i][i] = 0; // 자기 자신으로의 거리는 0
}
}

void floydWarshall(int n, vector<vector<int>>& graph) {
for (int k = 1; k <= n; k++) { // 중간 정점
for (int i = 1; i <= n; i++) { // 출발 정점
for (int j = 1; j <= n; j++) { // 도착 정점
// 중간에 k를 거쳐서 i에서 j로 갈 때의 비용
int cost = graph[i][k] + graph[k][j];
// 더 짧은 경로 선택
graph[i][j] = min(graph[i][j], cost);
}
}
}
}

int countStudents(int student, int n, vector<vector<int>>& graph) {
int cnt = 0;//?
for (int i = 1; i <= n; i++) {
if (student != i && graph[student][i] == INF && graph[i][student] == INF) { // 상대방과의 키 우열을 모르는 경우
return 0;//몰라요. (우열 모른단 뜻)
}
}
return 1;//위에서 안 걸리면 1 리턴
}
/*
* 키순서
* 내 키의 순위를 안다 <-> 내가 상대방과의 키 우열을 안다 or 상대방이 나와의 키 우열을 안다
*/
int main()
{
int n, m;//학생수, 비교횟수
int answer = 0;//답
vector<vector<int>> graph(INF, vector<int>(INF));//그래프선언
// 입력
cin >> n >> m;//입력받기
init(n, graph); // 초기화
while (m--) {//비교횟수만큼
int a, b; cin >> a >> b;//a가 b보다 키 작다.
graph[a][b] = 1; // 키 우열을 아는 두 정점의 거리를 1이라고 하자
}
// 연산
floydWarshall(n, graph);//연산
for (int i = 1; i <= n; i++) { // 자신의 키가 몇 번째인지 알 수 있는 학생들이 몇 명인지 계산
answer += countStudents(i, n, graph);//
}
// 출력
cout << answer << '\n';
return 0;
}