๊ทธ๋ํ์์ ๋ชจ๋ ์ ์ ์ ๊ฐ์ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ํ ๋ฒ์ ์ฐพ๋ ์๊ณ ๋ฆฌ์ฆ์
๋๋ค. ๋ค์ต์คํธ๋ผ(Dijikstra) ์๊ณ ๋ฆฌ์ฆ์ด ํ ์ ์ ์์ ๋ค๋ฅธ ๋ชจ๋ ์ ์ ๊น์ง์ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ์ฐพ๋ ๊ฒ๊ณผ ๋ฌ๋ฆฌ, Floyd-Warshall์ ๋ชจ๋ ์ ์ ์์ ๋ํ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ตฌํ ์ ์์ต๋๋ค.
(์์ ์ฌ์ดํด์ด ์๋ค๋ ๊ฐ์ ํ์ ์์ ๊ฐ์ค์น๊ฐ ์์ด๋ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ ์ ์๋ค๋ ์ฅ์ )
# V๋ ์ ์ ์ ๊ฐ์
# dist[i][j]๋ ์ ์ i์์ j๋ก ๊ฐ๋ ์ต๋จ ๊ฑฐ๋ฆฌ
for k in range(V):
for i in range(V):
for j in range(V):
if dist[i][k] + dist[k][j] < dist[i][j]:
dist[i][j] = dist[i][k] + dist[k][j]
โก๏ธ ์๊ฐ ๋ณต์ก๋: O(Vยณ)
๐งน ์ ๋ฆฌ
1) ์ ์ ๋ค์ ๋ํ ์ฐ๊ฒฐ ๊ฐ์ค์น๋ฅผ ๋ด์ 2์ฐจ์ ๋ฐฐ์ดA์ ์ต๋จ๊ฑฐ๋ฆฌ ๊ฐ์ค์น๋ฅผ ๋ด์ 2์ฐจ์ ๋ฐฐ์ดB๋ฅผ ์ ์ธ
2) 3์ค for๋ฌธ์ ํตํด ์งํต(i,j)์ผ๋ก ๊ฐ๋ ๋ฐฉ๋ฒ๊ณผ (i,k)์์ (k,j)๋ก ๊ฐ๋ ๋ฐฉ๋ฒ ์ค ๊ฐ์ค์น๊ฐ ๋ ์์ ์๋ก B๋ฐฐ์ด์ ์
๋ฐ์ดํธ ํ๋ค.
11403 ๊ฒฝ๋ก์ฐพ๊ธฐ
์ ๋ฌธ์ ์์๋ ๊ฐ์ค์น๊ฐ ์๋, ๋จ์ง ์ฐ๊ฒฐ๋์ด์์์ ๋ฌผ์ด๋ณด๋ ๋ฌธ์ ์ด๊ธฐ ๋๋ฌธ์ 0 ๋๋ 1๋ก ์ฌ๋ถ๋ฅผ ๋ด์์ค๋ค.


#include <iostream>
#include <string>
#include <queue>
#define MAX 101
using namespace std;
struct node{
int x,y;
};
int dir[4][2]={
0,1,
1,0,
0,-1,
-1,0
};
int N ;
int map[MAX][MAX];
void floydWarshall(int x, int y){
int result[N+1][N+1] = {0};
for(int i = 1 ; i <= N ; i ++ ){
for(int j = 1 ; j <= N ; j ++ ){
result[i][j] = map[i][j];
}
}
for(int k = 1 ; k <= N ; k ++ ){
for(int i = 1 ; i <= N ; i ++ ){
for(int j = 1 ; j <= N ; j ++ ){
if(result[i][k] + result[k][j] > 1 )
result[i][j] = 1 ;
}
}
}
for(int i = 1 ; i <= N ; i ++ ){
for(int j = 1 ; j <= N ; j ++ ){
cout<<result[i][j]<<" ";
}
cout<<'\n';
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> N;
for(int i = 1 ; i <= N ; i ++ ){
for(int j = 1 ; j <= N ; j ++ ){
cin>>map[i][j];
}
}
floydWarshall(0,0);
return 0;
}
๋ฐฑ์ค ์ฐธ๊ณ ๋ฌธ์ :
์ผ๋น ๋ฒ ์ด์ปจ์ 6๋จ๊ณ ๋ฒ์น
ํ๋ก์ด๋
ํค ์์