알고리즘/BaekJoon
[c++] 11403번 : 경로 찾기
__jhp_+
2022. 9. 19. 10:45
https://www.acmicpc.net/problem/11403
11403번: 경로 찾기
가중치 없는 방향 그래프 G가 주어졌을 때, 모든 정점 (i, j)에 대해서, i에서 j로 가는 경로가 있는지 없는지 구하는 프로그램을 작성하시오.
www.acmicpc.net
#include <iostream>
using namespace std;
void findRoute(int nodeCnt, bool edge[][101])
{
for (int mid = 1; mid <= nodeCnt; ++mid)
{
for (int from = 1; from <= nodeCnt; ++from)
{
for (int to = 1; to <= nodeCnt; ++to)
{
if (edge[from][to] == false && edge[from][mid] == true && edge[mid][to] == true)
{
edge[from][to] = true;
}
}
}
}
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int nodeCnt = 0;
bool edge[101][101] = { 0, };
cin >> nodeCnt;
for (int i = 1; i <= nodeCnt; ++i)
{
for (int j = 1; j <= nodeCnt; ++j)
{
cin >> edge[i][j];
}
}
findRoute(nodeCnt, edge);
for (int i = 1; i <= nodeCnt; ++i)
{
for (int j = 1; j <= nodeCnt; ++j)
{
cout << edge[i][j] << ' ';
}
cout << '\n';
}
return 0;
}