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
55 changes: 55 additions & 0 deletions 13_최단 경로/필수/1238.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#define MAX 1001
#define INF 1e9
using namespace std;

int N, M, X;
vector<pair<int, int>> graph[MAX];
int dist[MAX];

void dijkstra(int start) {
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, start));
dist[start] = 0;
while(!pq.empty()) {
int cost = -pq.top().first;
int here = pq.top().second;
pq.pop();
if(dist[here] < cost) continue;
for(int i=0; i<graph[here].size(); i++) {
int there = graph[here][i].first;
int nextDist = cost + graph[here][i].second;
if(dist[there] > nextDist) {
dist[there] = nextDist;
pq.push(make_pair(-nextDist, there));
}
}
}
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

cin >> N >> M >> X;
for(int i=0; i<M; i++) {
int a, b, t;
cin >> a >> b >> t;
graph[a].push_back(make_pair(b, t));
}
int result = 0;
for(int i=1; i<=N; i++) {
fill(dist, dist+MAX, INF);
dijkstra(i);
int temp = dist[X];
fill(dist, dist+MAX, INF);
dijkstra(X);
Copy link
Contributor

Choose a reason for hiding this comment

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

P3. dijkstra(X)는 한 번만 해도 되기 때문에 for문 전에 실행한 후 결과를 저장해서 사용하는 게 좋습니다! 😄

temp += dist[i];
result = max(result, temp);
}
cout << result;
return 0;
}
76 changes: 76 additions & 0 deletions 13_최단 경로/필수/15685.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#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++) {
// 해당 좌표와 우측, 아래, 우측아래 좌표가 모두 true일 경우, 정사각형을 이룬다고 판단하여 개수 증가
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);
// g세대만큼 반복하여 드래곤 커브를 그림
while (g--) {
int size_d = direct.size();
for (int j = size_d - 1; j >= 0; j--) { // 이전 세대의 방향을 거꾸로 참조하여 새로운 방향을 계산
int next_d = (direct[j] + 1) % 4;
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;
}
72 changes: 72 additions & 0 deletions 13_최단 경로/필수/2458.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#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;
}
}
// 자기 자신으로의 거리는 0으로 초기화
for (int i = 1; i <= n; i++) {
graph[i][i] = 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 i = 1, cnt = 0;
for (i = 1; i <= n; i++) {
// 학생과 다른 모든 학생들과의 거리가 무한대이면 키 순서를 알 수 없음
if (student != i && graph[student][i] == INF && graph[i][student] == INF) {
return 0;
}
}
// 학생의 키 순서를 알 수 있으면 1 반환
return 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;
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;
}