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;
}

 

+ Recent posts