Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 팰린드롬 만들기
- DirectX11
- 언리얼엔진5
- C++
- NRVO
- RVO
- softeer
- DeferredRendering
- C
- RootMotion
- directx
- 오블완
- winapi
- const
- Unreal Engine5
- 티스토리챌린지
- UE5
- 2294
- GeeksForGeeks
- IFileDialog
- UnrealEngine4
- Frustum
- Programmers
- algorithm
- 백준
- 프로그래머스
- 1563
- baekjoon
- 줄 세우기
- UnrealEngine5
Archives
- Today
- Total
Game Develop
[Algorithm]Baekjoon 11562번 :: 백양로 브레이크 본문
https://www.acmicpc.net/problem/11562
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
#include <functional>
#include <sstream>
#include <memory.h>
#include <deque>
#include <set>
#include <unordered_set>
#include <thread>
#include <atomic>
using namespace std;
int n, m, k;
const int maxNum = 0x3f3f3f3f;
int adjArray[251][251];
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
memset(adjArray, 0x3f, sizeof(adjArray));
for (int i = 0; i < m; ++i)
{
int a, b, c;
cin >> a >> b >> c;
adjArray[a][a] = adjArray[b][b] = adjArray[a][b] = 0;
adjArray[b][a] = c == 0 ? 1 : 0;
}
for (int k = 1; k <= n; ++k)
{
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (adjArray[i][k] + adjArray[k][j] < adjArray[i][j])
{
adjArray[i][j] = adjArray[i][k] + adjArray[k][j];
}
}
}
}
cin >> k;
vector<int> answers;
for (int i = 0; i < k; ++i)
{
int s, e;
cin >> s >> e;
answers.push_back(adjArray[s][e]);
}
for (int i = 0; i < answers.size(); ++i)
{
printf("%d\n", answers[i]);
}
}
|
cs |
코드자체는 그냥 플로이드기본풀이같지만.. 저렇게 표현하기위해 문제를 어떻게 해석하는지가 중요하다.
간선정보가 주어지는데 단방향일수도, 양방향일 수도 있다.
그러다보니 특정시작지점에서 특정도착지점까지 갈 수도 있고, 못갈수도 있을 것이다.
그러면 이 때, 단방향간선을 양방향으로 바꿀 수 있다고 가정했을 때 최소 몇번을 바꿔야 도착지점에 갈수있는지 알아야하는 문제이다.
처음에 간선정보가 주어질 때, 1 2 0 이라면 1번지점에서 2번지점까지 단방향간선이 있다는 의미인데, 그렇기 때문에 인접배열에 기록할 때 2 1 1 로 기록해준다.
즉, 2번지점에서 1번지점으로 가기위해서는 단방향을 양방향으로 바꿔줘야한다는 것을 가중치 1로 표현한 것이다.
이걸 하나의 인접배열에 가중치로 표현했으니, 이제는 그냥 플로이드와샬로 쉽게 구할 수 있다.
'Algorithm > Baekjoon' 카테고리의 다른 글
[Algorithm]Baekjoon 2015번 :: 수들의 합 4 (0) | 2025.02.08 |
---|---|
[Algorithm]Baekjoon 24479번 :: 알고리즘 수업 - 깊이 우선 탐색 1 (0) | 2025.02.05 |
[Algorithm]Baekjoon 16638번 :: 괄호 추가하기 2 (0) | 2025.02.03 |
[Algorithm]Baekjoon 1174번 :: 줄어드는 수 (0) | 2025.02.02 |
[Algorithm]Baekjoon 10819번 :: 차이를 최대로 (0) | 2025.01.26 |