int void init void FILE file fopen nodes txt int do fscanf file d- if

 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
int **W,n;
void init(void)
{
FILE* file=fopen("nodes.txt","r");
int i,j;
do
{
fscanf(file,"%d-%d",&i,&j);
if(i>n) n=i;
if(j>n) n=j;
}
while(!feof(file));
W=new int*[n];
for(int i=0;i<n;i++)
W[i]=new int[n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
W[i][j]=0;
rewind(file);
while(fscanf(file,"%d-%d\n",&i,&j)!=EOF)
{
W[i-1][j-1]=1;
}
fclose(file);
}
void floyd()
{
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
W[i][j] = W[i][j] || (W[i][k] && W[k][j]);
}
int main(void)
{
init();
floyd();
bool ok=true;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(W[i][j]==0) ok=false;
if(ok) printf("All nodes are reachable\n");
else printf("Not all nodes are reachable\n");
getch();
return 0;
}