Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created June 7, 2026 09:02
Show Gist options
  • Select an option

  • Save thinkphp/c415b266051e060d0fa89e7f5af31090 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/c415b266051e060d0fa89e7f5af31090 to your computer and use it in GitHub Desktop.
Algoritmul Roy Floyd
#include <stdio.h>
#define MAX 105
#define FIN "royfloyd.in"
#define FOUT "royfloyd.out"
//function prototypes
void read();
void RoyFloyd();
void write();
int n, mat[ MAX ][ MAX ];
int main() {
read();
RoyFloyd();
write();
return 0;
};
void read() {
int i,j;
freopen(FIN , "r", stdin);
scanf("%ld", &n);
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
scanf("%d", &mat[ i ][ j ]);
fclose( stdin );
};
//Roy-Floyd in action using Dynamic Programming
void RoyFloyd() {
int i,
j,
k;
for(k = 1; k <= n; k++)
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
if(i != j && mat[ i ][ k ] && mat[ k ][ j ])
if(mat[ i ][ k ] + mat[ k ][ j ] < mat[ i ][ j ] || !mat[ i ][ j ])
mat[ i ][ j ] = mat[ i ][ k ] + mat[ k ][ j ];
};
void write() {
int i,
j;
freopen(FOUT, "w", stdout);
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++)
printf("%d ", mat[ i ][ j ]);
printf("\n");
}
fclose( stdout );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment