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

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

by dkswnkk 2021. 10. 27.
 

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

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

www.acmicpc.net

#include <iostream>
#include <vector>
#define INF 1e9        //๋ฌดํ•œ์„ ๋œปํ•˜๋Š” ๊ฐ’์œผ๋กœ 10์–ต์„ ์ •์˜
using namespace std;

int n, m;    //n=๋„์‹œ์˜ ๊ฐœ์ˆ˜, m=๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜
int graph[101][101];
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);
    }

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

    for (int i = 0; i < m; i++) {    //๊ฐ„์„  ์ •๋ณด ์ž…๋ ฅ.
        int a, b, c; cin >> a >> b >> c;
                                            //a์—์„œ b๋กœ ๊ฐ€๋Š” ๋น„์šฉ์€ c
        graph[a][b] = min(graph[a][b], c); // ์‹œ์ž‘๋…ธ์„ ๊ณผ ๋„์ฐฉ๋…ธ์„ ์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ์ˆ˜ ์žˆ์œผ๋‹ˆ, ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ 

    }

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


}

๋Œ“๊ธ€