1. 입력값이 1 2 면 [1][2] = -1 [2][1] = 1
2. Floyd-Warshall 실행
3. 그대로 출력
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
/** | |
* https://www.acmicpc.net/problem/1613 | |
* BOJ 백준온라인져지 1613 역사 풀이 | |
*/ | |
using namespace std; | |
int N, M; | |
void floyd (int edges[][401]) { | |
for (int k = 1; k <= N; k++) { | |
for (int i = 1; i <= N; i++) { | |
for (int j = 1; j <= N; j++) { | |
if (edges[i][k] == 1 && edges[k][j] == 1) edges[i][j] = 1, edges[j][i] = -1; | |
} | |
} | |
} | |
} | |
int main () { | |
scanf("%d%d", &N, &M); | |
int (*edges)[401] = new int[401][401]; | |
for (int i = 0; i < M; i++) { | |
int x, y; | |
scanf("%d%d", &x, &y); | |
edges[x][y] = -1; | |
edges[y][x] = 1; | |
} | |
floyd(edges); | |
scanf("%d", &M); | |
for (int i = 0; i < M; i++) { | |
int x, y; | |
scanf("%d%d", &x, &y); | |
printf("%d\n", edges[x][y]); | |
} | |
} |
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 1219 오민식의 고민 풀이 (0) | 2018.02.08 |
---|---|
BOJ 백준온라인져지 1865 웜홀 풀이 (0) | 2018.02.06 |
BOJ 백준온라인져지 10159 저울 풀이 (0) | 2018.02.01 |
BOJ 백준온라인져지 1389 케빈 베이컨의 6단계 법칙 풀이 (0) | 2018.01.30 |
BOJ 백준온라인져지 11376 열혈강호 2 풀이 (0) | 2018.01.27 |