Skip to content
Snippets Groups Projects
Commit 6b9867a4 authored by julien.debray's avatar julien.debray
Browse files

ex done

parent 0c136370
Branches main
No related tags found
No related merge requests found
Makefile 0 → 100644
CC = gcc
CFLAGS = -Wall -Wextra -fsanitize=address -fsanitize=leak
FOLDER = .
SOURCES = $(wildcard $(FOLDER)/*.c)
OBJECTS = $(SOURCES:.c=.o)
TARGET = all
$(TARGET) : $(OBJECTS)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
.PHONY: clean
clean:
rm -f $(TARGET) $(OBJECTS)
rebuild: clean all
ex1.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
#define N 5
#define inf 100
void print_matrice(int** w, int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(w[i][j] == inf) {
printf(" inf");
} else {
printf("%4d", w[i][j]);
}
}
printf("\n");
}
printf("\n");
}
int** floyd_warshall(int** w, int n) {
for(int k = 0; k < n; k++) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(w[i][j] > w[i][k] + w[k][j]) {
w[i][j] = w[i][k] + w[k][j];
}
}
}
print_matrice(w, n);
}
return w;
}
int** init_matrice(int n) {
int** w = malloc(n * sizeof(int*));
for(int i = 0; i < n; i++) {
w[i] = malloc(n * sizeof(int));
}
return w;
}
void free_matrice(int** w, int n) {
for(int i = 0; i < n; i++) {
free(w[i]);
}
free(w);
}
int main() {
int data[N][N] = {
{0, 3, 8, inf, -4},
{inf, 0, inf, 1, 7},
{inf, 4, 0, inf, inf},
{2, inf, -5, 0, inf},
{inf, inf, inf, 6, 0}
};
// int data[N][N] = {
// {0, 2, 4, inf, 3},
// {2, 0, 8, inf, 1},
// {6, 2, 0, 4, 3},
// {1, inf, inf, 0, 5},
// {inf, inf, inf, 1, 0}
// };
int** mat = init_matrice(N);
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
mat[i][j] = data[i][j];
}
}
print_matrice(mat, N);
floyd_warshall(mat, N);
free_matrice(mat, N);
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment