-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflashmba.java
executable file
·74 lines (59 loc) · 1.37 KB
/
flashmba.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package testJava;
import java.util.*;
public class flashmba {
public static void main(String[] args)
{
//DO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int i,j,x;
ArrayList<ArrayList<Integer> > a
= new ArrayList<ArrayList<Integer> >();
for(i=0;i<n;i++)
{ a.add(new ArrayList<Integer>());
for(j=0;j<n;j++)
{
x=sc.nextInt();
a.get(i).add(j,x);
}
}
if(solve(a,n)==true){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}
public static boolean solve(ArrayList<ArrayList<Integer> > a ,int n)
{
int i,j,k;
int dist[][]=new int[n][n];
for (i = 0; i < n;i++)
{
for (j = 0; j < n;j++)
{
dist[i][j] = a.get(i).get(j);
}
}
for (k = 0; k < n;k++)
{
for (i = 0; i < n;i++)
{
for (j = 0; j < n;j++)
{
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
for (i = 0; i < n;i++){
if (dist[i][i] < 0)
return true;
}
return false;
}
}