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
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"vector": "cpp"
}
}
75 changes: 75 additions & 0 deletions 13_최단 경로/필수/1238.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int INF = 987654321;

vector<int> dijkstra(int start, int N, const vector<vector<pair<int, int>>>& graph)
{
vector<int> distance(N + 1, INF);
distance[start] = 0;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, start});

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

if (dist > distance[cur])
continue;

for (int i = 0; i < graph[cur].size(); i++)
{
int next = graph[cur][i].first;
int weight = graph[cur][i].second;

if (distance[next] > dist + weight)
{
distance[next] = dist + weight;
pq.push({distance[next], next});
}
}

}

return distance;
}

int main()
{
int N, M, X;
cin >> N >> M >> X;

vector<vector<pair<int, int>>> graph(N + 1);

for (int i = 0; i < M; i++)
{

int start, end, time;
cin >> start >> end >> time;
graph[start].push_back({end, time});
}

vector<int> a = dijkstra(X, N, graph);

int maxTime = 0;

for (int i = 1; i <= N; i++)
{
if (i == X)
{
continue;
}
vector<int> b = dijkstra(i, N, graph);
maxTime = max(maxTime, a[i] + b[X]);
}

cout << maxTime;

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

using namespace std;

const int SIZE = 100;

// 방향: 우(0), 상(1), 좌(2), 하(3)
int dy[4] = { 0, -1, 0, 1 };
int dx[4] = { 1, 0, -1, 0 };

// 1x1 정사각형 개수 계산
int cntSquares(vector<vector<bool>>& plane) {
int ans = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
// 현재 위치 (i, j)와 이웃한 네 개의 정사각형이 모두 true일때 정사각형의 개수를 1 증가시킴

if (plane[i][j] && plane[i + 1][j] && plane[i][j + 1] && plane[i + 1][j + 1]) {
ans++;
}
}
}
return ans;
}

// 평면에 드래곤 커브를 표시
void drawDragonCurve(vector<vector<bool>>& plane, int x, int y, int d, int g) {
vector<int> direct; // 방향 저장
plane[y][x] = plane[y + dy[d]][x + dx[d]] = true; // 평면에 드래곤 커브 초기 위치를 표시
x += dx[d];
y += dy[d];
direct.push_back(d);
while (g--) { // 1 ~ g 세대까지 반복
int size_d = direct.size();
for (int j = size_d - 1; j >= 0; j--) { // 이전 세대의 방향을 역순으로 확인
int next_d = (direct[j] + 1) % 4; // 오른쪽으로 90도 회전해서 다음 방향 계산
x += dx[next_d];
y += dy[next_d];
plane[y][x] = true; // 평면에 드래곤 커브를 표시
direct.push_back(next_d); // 다음 세대의 방향을 저장
}
}
}

/*
* 규칙
* 0 세대: 0
* 1 세대: 0 1
* 2 세대: 0 1 2 1
* 3 세대: 0 1 2 1 2 3 2 1
* ...
* N 세대: concat((N-1세대), ((N-1세대 거꾸로) + 1)%4)
* 평면(좌측 상단이 (0, 0))에 드래곤 커브를 그린 후 정사각형의 개수를 계산
* 드래곤 커브는 평면 밖으로 나가지 않음으로 범위를 확인할 필요 없음
* 1. 0 세대의 드래곤 커브를 먼저 저장 (초기 조건)
* 2. 세대를 거듭하면서 드래곤 커브를 그림 (규칙을 파악하는 것이 중요)
* 3. 드래곤 커브가 그려진 평면 상의 정사각형의 개수 계산 (네 꼭짓점 확인)
*/

int main() {
int n, x, y, d, g;
vector<vector<bool>> plane(SIZE + 1, vector<bool>(SIZE + 1, false)); // 평면 초기화!
// 입력
cin >> n;
// 연산 & 출력
while (n--) { // n개의 드래곤 커브 그리기
cin >> x >> y >> d >> g;
drawDragonCurve(plane, x, y, d, g);
}
cout << cntSquares(plane) << '\n'; // 드래곤 커브가 그려진 평면의 정사각형 개수 출력
return 0;
}
69 changes: 69 additions & 0 deletions 13_최단 경로/필수/2458.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <vector>

using namespace std;

const int INF = 987654321;

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++)
{
if (graph[i][j] > graph[i][k] + graph[k][j])
{
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
}
}

int main()
{
int n, m;
cin >> n >> m;
int ans = 0;

vector<vector<int>> graph(n + 1, vector<int>(n + 1, INF));

for (int i = 1; i <= n; i++)
{
graph[i][i] = 0;
}

while(m--)
{
int a, b;
cin >> a >> b;
graph[a][b] = 1;
}

floydWarshall(n, graph);

for (int i = 1; i <= n; i++)
{
bool flag = true;

for (int j = 1; j <= n; j++)
{
if (graph[i][j] == INF && graph[j][i] == INF)
{
flag = false;
break;
}
}
if (flag)
{
ans++;
}
}
Comment on lines +47 to +63
Copy link
Contributor

Choose a reason for hiding this comment

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

💯


cout << ans;

return 0;
}
/**/