티스토리 뷰
문제 링크입니다 : https://www.acmicpc.net/problem/11404
11404번: 플로이드
첫째 줄에 도시의 개수 n(1 ≤ n ≤ 100)이 주어지고 둘째 줄에는 버스의 개수 m(1 ≤ m ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 �
www.acmicpc.net
플로이드 와샬 문제!
#include <iostream>
#include <vector>
#define INF 987654321
using namespace std;
int n;
vector<vector<int>> board;
vector<vector<int>> floydWarshall() {
vector<vector<int>> ret;
ret = board;
for(int k = 0; k < n; k++) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(ret[i][k] + ret[k][j] < ret[i][j]) {
ret[i][j] = ret[i][k] + ret[k][j];
}
}
}
}
return ret;
}
int main() {
int m;
cin >> n >> m;
board.assign(n, vector<int>(n, INF));
int u, v, cost;
for(int i = 0; i < m; i++) {
cin >> u >> v >> cost;
if(board[u - 1][v - 1] > cost) {
board[u - 1][v - 1] = cost;
}
}
for(int i = 0; i < n; i++) {
board[i][i] = 0;
}
vector<vector<int>> temp = floydWarshall();
for(const auto& el : temp) {
for(const auto& e : el) {
if(e == INF) cout << 0 << ' ';
else cout << e << ' ';
}
cout << '\n';
}
return 0;
}
개발 환경 : vscode
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
'알고리즘 > 백준(BOJ)' 카테고리의 다른 글
백준 - 1956 운동 (0) | 2020.06.22 |
---|---|
백준 - 10217 KCM Travel (2) | 2020.06.22 |
백준 - 2037 문자메시지 (0) | 2020.06.21 |
백준 - 3568 iSharp (0) | 2020.06.21 |
백준 - 1174 줄어드는 숫자 (0) | 2020.06.16 |
댓글