Baekjoon/Graph
[백준 11404] 플로이드 (Floyd-Warshall) (C/C++)
워니-
2020. 2. 7. 14:34
#include <stdio.h> #include <iostream> #include <vector> #include <map> using namespace std; int cityNum, busNum; int start; int d[110][110]; map<pair<int, int>, int> visited; void floyd() { for(int k = 1; k <= cityNum; k++) { for(int i = 1; i <= cityNum; i++) { for(int j = 1; j <= cityNum; j++) { if(d[i][k] + d[k][j] < d[i][j]) { d[i][j] = d[i][k] + d[k][j]; } } } } } int main(void) { // freopen("B11404_input.txt", "r", stdin); cin >> cityNum >> busNum; for(int i = 1; i <= cityNum; i++) { for(int j = 1; j <= cityNum; j++) { d[i][j] = 199999999; } } for(int i = 1; i <= busNum; i++) { int from, to, weight; cin >> from >> to >> weight; if(visited[{from, to}] == 0) { d[from][to] = weight; visited[{from, to}] = 1; } else { if(weight < d[from][to]) { d[from][to] = weight; } } } floyd(); for(int i = 1; i <= cityNum; i++) { for(int j = 1; j <= cityNum; j++) { // i와 j가 같은 경우는 항상 최소비용 0 if(i == j) { cout << "0 "; } // i에서 j로 갈 수 없는 경우 else if(d[i][j] == 199999999) { cout << "0 "; } else { cout << d[i][j] << " "; } } cout << endl; } return 0; } | cs |