Created
June 7, 2026 09:02
-
-
Save thinkphp/c415b266051e060d0fa89e7f5af31090 to your computer and use it in GitHub Desktop.
Algoritmul Roy Floyd
This file contains hidden or 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 <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