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

2. 정확한 순위

#include <iostream>
using namespace std;

#define INF 1e9
#define MAX 502
int n, m, a, b, c;
int d[MAX][MAX];

int main() {
    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == j)
                d[i][j] = 0;
            else
                d[i][j] = INF;
        }
    }

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

    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (d[i][k] == INF || d[k][j] == INF)
                    continue;
                if (d[i][k] + d[k][j] < d[i][j])
                    d[i][j] = d[i][k] + d[k][j];
            }
        }
    }

    int result = 0;
    for (int i = 1; i <= n; i++) {
        int cnt = 0;
        for (int j = 1; j <= n; j++) {
            if (d[j][i] != INF || d[i][j] != INF)
                cnt++;
        }
        if (cnt == n) {
            result++;
        }
    }
    cout << result;
    return 0;
}

 

3. 화성 탐사

#include <iostream>
#include <queue>
using namespace std;

#define INF 1e9
#define MAX 130
int d[MAX][MAX];
int arr[MAX][MAX];
int t, n;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

void dijkstra(int x, int y) {
    d[x][y] = arr[x][y];
    priority_queue<pair<int, pair<int, int>>> pq;
    pq.push({-arr[x][y], {x, y}});

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

        if (dist > d[nowx][nowy]) //이미 처리된 적 있는 노드라면 무시
            continue;
        for (int i = 0; i < 4; i++) {
            int nx = nowx + dx[i];
            int ny = nowy + dy[i];
            int cost = dist + arr[nx][ny];

            if (nx < 0 || nx >= n || ny < 0 || ny >= n)
                continue;
            if (d[nx][ny] > cost) {
                d[nx][ny] = cost;
                pq.push({-cost, {nx, ny}});
            }
        }
    }
    cout << d[n - 1][n - 1] << "\n";
}

int main() {
    cin >> t;

    while (t--) {
        cin >> n;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                d[i][j] = INF;
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cin >> arr[i][j];
            }
        }
        dijkstra(0, 0);
    }

    return 0;
}

+ Recent posts