File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class boj1613 {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static StringTokenizer st;
8+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
9+ static int nextInt() {return Integer . parseInt(st. nextToken());}
10+
11+ static int N ;
12+ static int [][] floyd;
13+ static int INF = 100000000 ;
14+ public static void main(String [] args) throws Exception {
15+ nextLine();
16+ N = nextInt();
17+ int K = nextInt();
18+ floyd = new int [N + 1 ][N + 1 ];
19+ for (int i = 0 ; i < N + 1 ; i++ ) {
20+ Arrays . fill(floyd[i], INF );
21+ floyd[i][i] = 0 ;
22+ }
23+ for (int i = 0 ; i < K ; i++ ) {
24+ nextLine();
25+ int a = nextInt();
26+ int b = nextInt();
27+ floyd[a][b] = 1 ;
28+ }
29+ solve();
30+ nextLine();
31+ int S = nextInt();
32+ for (int i = 0 ; i < S ; i++ ) {
33+ nextLine();
34+ int a = nextInt();
35+ int b = nextInt();
36+ if (floyd[a][b] != INF ) System . out. println(- 1 );
37+ else if (floyd[b][a] != INF ) System . out. println(1 );
38+ else System . out. println(0 );
39+ }
40+ }
41+
42+ static void solve() {
43+ for (int k = 1 ; k <= N ; k++ ) {
44+ for (int i = 1 ; i <= N ; i++ ) {
45+ for (int j = 1 ; j <= N ; j++ ) {
46+ if (floyd[i][k] == INF || floyd[k][j] == INF ) continue ;
47+ floyd[i][j] = Math . min(floyd[i][j], floyd[i][k]+ floyd[k][j]);
48+ }
49+ }
50+ }
51+ }
52+ }
53+ ```
You can’t perform that action at this time.
0 commit comments