1. ๋ค์ต์คํธ๋ผ : ์์ ์ ์ ํ๋๋ก๋ถํฐ ๋ค๋ฅธ ๋ชจ๋ ์ ์ ์ผ๋ก ๊ฐ๋ ์ต๋จ ๊ฒฝ๋ก ์ฐพ๊ธฐ (bfs์ ๋น์ท, ๋น์ฉ์ด ์์ => visit ๋์ ) O(E logV)
dist : 1์ฐจ์ ๋ฐฐ์ด(INF๋ก ์ด๊ธฐํ), idx : 1๋ถํฐ ์์,
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n = 6, m, start;
#define INF 1e9
int a, b, c;
vector<pair<int, int>> graph[7]; //๋
ธ๋ ๊ฐ ๊ฐ์
int d[7]; //์ต์ ๋น์ฉ
void dijkstra(int start) { //๋
ธ๋ ์์ ์์ ๊ฒ, ๋ง์ฝ ๊ฑฐ๋ฆฌ๋ฅผ ์์ ์์ผ๋ก ํ๊ณ ์ถ์ผ๋ฉด pq, arr์ ๋ค์ด๊ฐ๋ ์์ ๋ฐ๊ฟ์ผํจ
d[start] = 0;
priority_queue<pair<int, int>> pq; //๊ฐ๊น์ด ์์๋๋ก ์ฒ๋ฆฌํ๋ฏ๋ก
pq.push({start, 0});
while (!pq.empty()) {
int current = pq.top().first;
int distance = -pq.top().second; //์งง์ ๊ฒ์ด ๋จผ์ ์ค๋๋ก ์์ํ
pq.pop();
cout << "cur: " << current << ", ํ์ฌ ๊ฑฐ๋ฆฌ: " << d[current] << ", ๋ฐ๊พธ๋ ค๋ ๊ฑฐ๋ฆฌ: " << distance << endl;
if (d[current] < distance) //์ต๋จ ๊ฑฐ๋ฆฌ๊ฐ ์๋ ๊ฒฝ์ฐ
continue;
for (auto &i: graph[current]) { //์ ํ๋ ๋
ธ๋์ ์ธ์ ๋
ธ๋
int next = i.first;
int nextDistance = distance + i.second;
cout << "next: " << next << ", " << nextDistance << "\n";
if (nextDistance < d[next]) { //๊ธฐ์กด์ ์ต์ ๋น์ฉ๋ณด๋ค ๋ ์ ๋ ดํ๋ค๋ฉด ๊ต์ฒด
pq.push({next, -nextDistance});
d[next] = nextDistance;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (int i = 1; i <= n; i++) { //์ฐ๊ฒฐ๋์ง ์์ ๊ฒฝ์ฐ ๋น์ฉ์ ๋ฌดํ
d[i] = INF;
}
graph[1].emplace_back(2, 2);
graph[1].emplace_back(3, 5);
graph[1].emplace_back(4, 1);
graph[2].emplace_back(1, 2);
graph[2].emplace_back(3, 3);
graph[2].emplace_back(4, 2);
graph[3].emplace_back(1, 5);
graph[3].emplace_back(2, 3);
graph[3].emplace_back(4, 3);
graph[3].emplace_back(5, 1);
graph[3].emplace_back(6, 5);
graph[4].emplace_back(1, 1);
graph[4].emplace_back(2, 2);
graph[4].emplace_back(3, 3);
graph[4].emplace_back(5, 1);
graph[5].emplace_back(3, 1);
graph[5].emplace_back(4, 1);
graph[5].emplace_back(6, 2);
graph[6].emplace_back(3, 5);
graph[6].emplace_back(5, 2);
dijkstra(1);
// [ ์
๋ ฅ๋ฐ๋๋ก ํ๋ ์ฝ๋ ]
//cin >> n >> m >> start; //๋
ธ๋ ๊ฐ์, ๊ฐ์ ์ ๋ณด ๊ฐ์, ์์ ๋
ธ๋
//for (int i = 1; i <= n; i++) {
// d[i] = INF;
//}
//while (m--) {
// cin >> a >> b >> c;
// graph[a].push_back({b, c});
//}
//dijkstra(start);
return 0;
}
//6 20 1
//1 2 2
//1 3 5
//1 4 1
//2 1 2
//2 3 3
//2 4 2
//3 1 5
//3 2 3
//3 4 3
//3 5 1
//3 6 5
//4 1 1
//4 2 2
//4 3 3
//4 5 1
//5 3 1
//5 4 1
//5 6 2
//6 3 5
//6 5 2
2. ํ๋ก์ด๋ : ๋ชจ๋ ์ ์ ์์ ์ต๋จ ๊ฒฝ๋ก ์ฐพ์, k๊ฐ ๊ฑฐ์ณ๊ฐ๋ ์ค์ฌ O(N^3)
dist : 2์ฐจ์ ๋ฐฐ์ด(INF๋ก ์ด๊ธฐํ), idx : 1๋ถํฐ ์์
#include <iostream>
using namespace std;
#define INF 1000000001 //์ ์ 100 x ๋น์ฉ 10๋ง
#define MAX 102
int dist[MAX][MAX];
int n, m, a, b, c;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dist[i][j] = INF;
}
}
while (m--) {
cin >> a >> b >> c;
if (dist[a][b] > c) {
dist[a][b] = c;
}
}
for (int k = 1; k <= n; k++) { //๊ฑฐ์ณ๊ฐ๋๊ฒ ์ค์ฌ
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dist[i][k] == INF || dist[k][j] == INF) {
continue;
}
// i์์ k๋ก ๊ฐ๋๋น์ฉ + k์์ j๋ก ๊ฐ๋๋น์ฉ vs i์์ j๋ก ๊ฐ๋ ๋น์ฉ
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dist[i][j] == INF || i == j) {
cout << "0 ";
} else {
cout << dist[i][j] << " ";
}
}
cout << "\n";
}
return 0;
}
//4 7
//1 2 4
//1 4 6
//2 1 3
//2 3 7
//3 1 5
//3 4 4
//4 3 2
//๊ฒฐ๊ณผ
//0 4 8 6
//3 0 7 9
//5 9 0 4
//7 11 2 0
3. ๋ฒจ๋ง ํฌ๋ : ๊ฐ์ค์น ์์์ธ ๋ฌธ์ ์์ ์ฌ์ฉ -> ๊ฑฐ์ ์๋์ด
4. BFS : ๋์ด ์ฐ์ ํ์(๊ฐ์ค์น๊ฐ 1์ธ ๋ฌธ์ ์์์ ์ต๋จ ๊ฒฝ๋ก) O(N+E)
dist : 2์ฐจ์ ๋ฐฐ์ด , idx : 0๋ถํฐ ์์
1) ๊ทธ๋ํ์ผ ๊ฒฝ์ฐ
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAX 1002
int graph[MAX][MAX];
bool visit[MAX];
int n, m , k, a, b;
int dist[MAX];
void BFS(int V) {
queue<int> Q;
Q.push(V);
visit[V] = 1;
while (!Q.empty()) {
V = Q.front();
Q.pop();
for (int i = 1; i <= n; i++) {
if (graph[V][i] == 1 && !visit[i]) {//๊ฐ์ ์ด ์กด์ฌํ๊ณ ๋ฐฉ๋ฌธํ์ง ์์ ๊ฒฝ์ฐ
Q.push(i);
visit[i] = 1;
dist[i] = dist[V] + 1;
}
}
}
}
int main() {
cin >> n >> m >> k;
while(m--){
cin >> a >> b;
graph[a][b] = 1;
graph[b][a] = 1;
}
BFS(1);
if(dist[n] > 0 && dist[n]<= k ){ //๊ฐ ์ ์๊ณ k๋ณด๋ค ์์ ๊ฒฝ์ฐ
cout << "YES";
} else{
cout << "NO";
}
return 0;
}
2) ํ๋ ฌ์ผ ๊ฒฝ์ฐ (4๋ฐฉํฅ)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n, m;
int map[102][102];
bool visit[102][102];
int check[102][102];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
void bfs(int x, int y) {
visit[x][y] = true;
queue<pair<int, int>> q;
q.push({x, y});
while (!q.empty()) {
x = q.front().first;
y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= m)
continue;
if (!visit[nx][ny] && map[nx][ny] == 1) {
visit[nx][ny] = true;
q.push({nx, ny});
check[nx][ny] = check[x][y] + 1;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
char c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> c;
map[i][j] = c - '0';
}
}
bfs(0, 0);
cout << check[n - 1][m - 1] + 1; //0, 0์ผ๋ก ์์ํ๋ฉด count๊ฐ ์๋ 0 -> 1
return 0;
}
//4 6
//110110
//110110
//111111
//111101
//๊ฒฐ๊ณผ (์ต๋จ๊ฒฝ๋ก)
//9
9-4. ๋ฏธ๋๋์
1๋ถํฐ k๊ฑฐ์น๊ณ x๊น์ง์ ์ต๋จ๊ฑฐ๋ฆฌ ๊ตฌํด๋ผ (100^3ํด๋ ๋ฐฑ๋ง์ด๋ผ 1์ด ๊ฐ๋ฅ)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define INF 1e9
int n, m, x, k, a, b;
int dist[101][101];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dist[i][j] = INF;
}
}
while (m--) {
cin >> a >> b;
dist[a][b] = 1;
dist[b][a] = 1;
}
cin >> x >> k;
for (int z = 1; z <= n; z++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dist[i][z] == INF || dist[z][j] == INF)
continue;
if (dist[i][z] + dist[z][j] < dist[i][j])
dist[i][j] = dist[i][z] + dist[z][j];
}
}
}
if (dist[1][k] + dist[k][x] >= INF)
cout << -1;
else
cout << dist[1][k] + dist[k][x];
return 0;
}
9-5. ์ ๋ณด
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define INF 1e9
int n, m, C, a, b, c;
int dist[30001];
vector<pair<int, int>> graph[30001];
void dijkstra(int start) {
dist[start] = 0;
priority_queue<pair<int, int>> pq;
pq.push({0, start});
while (!pq.empty()) {
int current = pq.top().second;
int distance = -pq.top().first;
pq.pop();
if (dist[current] < distance)
continue;
for (auto &i: graph[current]) {
int next = i.second;
int nextDistance = distance + i.first;
if (nextDistance < dist[next]) {
pq.push({-nextDistance, next});
dist[next] = nextDistance;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> C;
for (int i = 1; i <= n; i++) {
dist[i] = INF;
}
while (m--) {
cin >> a >> b >> c;
graph[a].emplace_back(c, b);
}
dijkstra(C);
int result = 0;
int maxDistance = 0;
for (int i = 1; i <= n; i++) {
if (dist[i] != INF) { //INF ์๋ ๊ฑฐ ๊ฐ์ = C์์ ๋๋ฌํ ์ ์๋ ๋
ธ๋ cnt
result += 1;
maxDistance = max(maxDistance, dist[i]); //๊ฐ์ฅ ๋จผ ์๊ฐ
}
}
cout << result - 1 << " " << maxDistance;
return 0;
}
'โ๏ธAlgorithm > ๐ ์ด์ทจ์ฝ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ด์ทจ์ฝ] ์ ํ๋ณ ๊ธฐ์ถ - ๊ทธ๋ฆฌ๋ ๋ฌธ์ (c++) (0) | 2022.11.02 |
---|---|
[์ด์ทจ์ฝ] 10. ๊ทธ๋ํ ์ด๋ก - C++ (0) | 2022.10.22 |
[์ด์ทจ์ฝ] 8. DP - C++ (0) | 2022.10.21 |
[์ด์ทจ์ฝ] 5. DFS/BFS - C++ (0) | 2022.10.20 |
[์ด์ทจ์ฝ] 2. ๊ตฌํ - C++ (0) | 2022.10.04 |