Skip to content

Conversation

@KII1ua
Copy link
Member

@KII1ua KII1ua commented Jan 26, 2026

🚀 이슈 번호

Resolve: {#2331}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 플로이드 워셜, 재귀
  • 🔹 어떤 방식으로 접근했는지
    워셜로 각 노드에서 노드로 가는 최단거리를 구한 후 만약 다른 노드를 거쳐 최단거리를 갱신할 수 있는 경우 path 값에 저장하여 재귀를 사용하여 호출하였습니다. 이때 중간 지점을 중복으로 두번 넣지 않기 위해서 뒤에 있는 것을 pop 해주었습니다.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(N^3)
  • 이유:
    3중 for문

💻 구현 코드

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define endl "\n"

struct Tree {
    int Node, left, right;
};

const int INF = 1e9;
const int MAX = 101;
const int MOD = 1e9 + 7;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int n, m;
int graph[MAX][MAX];
int path[MAX][MAX];
vector<int> v;

void Print() {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(graph[i][j] == INF) cout << 0 << " ";
            else cout << graph[i][j] << " ";
        }
        cout << endl;
    }
}

void find_route(int start, int end) {
    if(path[start][end] == 0) {
        v.push_back(start);
        v.push_back(end);
        return;
    }
    find_route(start, path[start][end]);
    v.pop_back();
    find_route(path[start][end], end);
}

void solve() {
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                int value = graph[i][k] + graph[k][j];

                if(value < graph[i][j]) {
                    graph[i][j] = value;
                    path[i][j] = k;
                }
            }
        }
    }

    Print();
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(i == j || graph[i][j] == INF) cout << 0 << endl;
            else {
                v.clear();
                find_route(i, j);
                cout << v.size() << " ";
                for(auto &it : v) {
                    cout << it << " ";
                }
                cout << endl;
            }
        }
    }
}

void Init() {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(i == j) continue;
            graph[i][j] = INF;
        }
    }
}

void input() {
    cin >> n >> m;

    Init();

    for(int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        graph[a][b] = min(graph[a][b], c);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    input();
    solve();
}

@KII1ua KII1ua self-assigned this Jan 26, 2026
@KII1ua KII1ua linked an issue Jan 26, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260126 : 코딩테스트

2 participants