๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 11780๋ฒˆ - ํ”Œ๋กœ์ด๋“œ2

by dkswnkk 2021. 10. 31.
 

11780๋ฒˆ: ํ”Œ๋กœ์ด๋“œ 2

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ n์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ m์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ m+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ ๋ฒ„์Šค์˜ ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ๊ฐ€

www.acmicpc.net

#include <iostream>
#include <vector>
#define INF 1e9     //๋ฌดํ•œ์„ ์˜๋ฏธํ•˜๋Š” ๊ฐ’์œผ๋กœ 10์–ต์„ ์ง€์ • 

using namespace std;

int graph[101][101];
int n, m,cnt;    //n=๋„์‹œ์˜ ๊ฐœ์ˆ˜, m=๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜
int path[101][101];

void findCost(int a, int b)    {    //๊ฒฝ๋กœ์— ํฌํ•จ๋œ ๋„์‹œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
    if (path[a][b] != INF) {
        cnt++;
        findCost(a, path[a][b]);
        findCost(path[a][b], b);
    }
}

void findPath(int a, int b) {    //์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๊ฒฝ๋กœ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
    if (path[a][b] != INF) {
        findPath(a, path[a][b]);
        cout << path[a][b] << ' ';
        findPath(path[a][b], b);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;

    for (int i = 0; i < 101; i++) {        //์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ๋ฌดํ•œ์œผ๋กœ ์ดˆ๊ธฐํ™”;
        fill(graph[i], graph[i] + 101, INF);
        fill(path[i], path[i] + 101, INF);
    }

    for (int a = 1; a <= n; a++) {        //์ž๊ธฐ ์ž์‹ ์—์„œ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์€ 0
        for (int b = 1; b <= n; b++) {
            if (a == b) {
                graph[a][b] = 0;
                path[a][b] = 0;
            }
        }
    }


    for (int i = 0; i < m; i++) {    //๊ฐ„์„  ์ •๋ณด ์ž…๋ ฅ
        int a, b, c; cin >> a >> b >> c;
        graph[a][b] = min(graph[a][b], c);    //a์—์„œ b๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ c๋งŒํผ์˜ ๋น„์šฉ์ด ๋“ ๋‹ค.
    }

    for (int k = 1; k <= n; k++) {        //ํ”Œ๋กœ์ด๋“œ-์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜ํ–‰
        for (int a = 1; a <= n; a++) {
            for (int b = 1; b <= n; b++) {
                if (graph[a][k] + graph[k][b] < graph[a][b]) {
                    graph[a][b] = graph[a][k] + graph[k][b];
                    path[a][b] = k;
                }
            //    graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b]);
            }

        }
    }
    for (int a = 1; a <= n; a++) {        //i->j๋กœ ๊ฐ€๋Š”๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ ๋น„์šฉ ์ถœ๋ ฅ    
        for (int b = 1; b <= n; b++) {
            if (graph[a][b] == INF) cout << 0 << ' ';
            else cout << graph[a][b] << ' ';
        }
        cout << "\n";
    }

    for (int a = 1; a <= n; a++) {
        for (int b = 1; b <= n; b++) {
            if (graph[a][b] == INF||a==b) cout << 0<<'\n';
            else {
                cnt = 2;    //์‹œ์ž‘๊ณผ ๋์€ ๋ฌด์กฐ๊ฑด ์ง€๋‚˜๋ฏ€๋กœ 2๋ถ€ํ„ฐ ์‹œ์ž‘
                findCost(a, b);
                cout << cnt << ' ' << a << ' ';
                findPath(a, b);
                cout << b << "\n";
            }
        }
    }


}

๋Œ“๊ธ€