Little Jay

Floyd-Warshall 본문

Univ/Algorithm

Floyd-Warshall

Jay, Lee 2022. 6. 4. 23:22
#include<iostream>
#define NUMBER 8
#define INF 987654321
using namespace std;

int table[NUMBER][NUMBER] = {
   {0, 3, INF, INF, INF, INF, 2, 4},
   {3, 0, 2, 4, INF ,INF , INF, INF},
   {INF, 2, 0, 4, INF, 5, INF, INF},
   {INF, 4, 4, 0, 2, 5, INF, INF},
   {INF ,INF ,INF, 2, 0, INF, 3 , 3},
   {INF, INF ,5 ,5 ,INF, 0, INF ,2},
   {2, INF, INF, INF, 3, INF, 0, 2},
   {4, INF, INF, INF, 3, 2, 2, 0}
};

void printTable() {
    for (int i = 0; i < NUMBER; i++) {
        for (int j = 0; j < NUMBER; j++) {
            if (table[i][j] == INF) cout << "- ";
            else  cout << table[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    cout << endl;
}


void FloydFunction() {
    for (int k = 0; k < 2; k++) {
        for (int i = 0; i < NUMBER; i++) {
            for (int j = 0; j < NUMBER; j++) {
                int temp = table[i][k] + table[k][j];
                if (temp < table[i][j])
                    table[i][j] = temp;

            }
        }
        printTable();
    }
}

int main() {

    FloydFunction();

    return 0;
}

'Univ > Algorithm' 카테고리의 다른 글

Prim Algorithm  (0) 2022.06.29
Comments