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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| #include <stdio.h>
#include <stdlib.h>
#define N 4
typedef struct
{
}Sommet;
typedef struct
{
int n;
Sommet *tabSomm;
int **matrice;
}Graphe;
typedef struct CellSommet // liste des sommets : 0 1 2 3 4 5 ...
{
int nom;
int marque;
int poids;
struct CellSommet *antecedent;
struct CellArc *liste_arcs;
struct CellSommet *suivant;
}TypeSommet;
typedef struct CellArc // listes des arcs liés à chaqun de ces sommets
{
int poids_arc;
struct CellSommet *extremite;
struct CellArc *suivant;
}TypeArc;
typedef TypeSommet *TypeGraphe;
Graphe InitMat() //Initialisation de la matrice d'adjacence et des conduitions sur le stanleaux
{
Graphe G;
G.n=N;
int i;
G.matrice = (int**)malloc(N*sizeof(char));
if ( G.matrice == NULL )
exit(0);
for (i=0;i<N;i++)
{
G.matrice[i]=(int *)malloc(N * sizeof(char));
if (G.matrice[i]==NULL)
exit(0);
}
G.matrice[0][0]=0;G.matrice[1][0]=100;G.matrice[2][0]=120;G.matrice[3][0]=0;
G.matrice[0][1]=100;G.matrice[1][1]=0;G.matrice[2][1]=10;G.matrice[3][1]=0;
G.matrice[0][2]=120;G.matrice[1][2]=10;G.matrice[2][2]=0;G.matrice[3][2]=200;
G.matrice[0][3]=0;G.matrice[1][3]=0;G.matrice[2][3]=200;G.matrice[3][3]=0;
return G;
}
TypeGraphe convert_list(Graphe *G) // Conversion de la matrice en liste
{
printf("fd");
TypeGraphe Gp,temp,s;
TypeArc *tmp;
int i,j;
Gp=(TypeGraphe) calloc(1,sizeof(TypeGraphe));
temp=Gp;
printf("hh");
for (i=1;i<G->n;i++)
{
temp->suivant=(TypeSommet *)malloc(sizeof(TypeSommet));
temp=temp->suivant;
temp->poids=-1;
temp->marque=0;
temp->antecedent=NULL;
temp->nom = i;
}
temp=Gp;
for (i=0;i<G->n;i++,temp=temp->suivant)
{
s=Gp;
for (j=0;j<G->n;j++,s=s->suivant)
{
if (G->matrice[i][j]>0)
{
if (temp->liste_arcs==NULL)
{
temp->liste_arcs = (TypeArc *)malloc(sizeof(TypeArc));
tmp=temp->liste_arcs;
tmp->extremite=s;
tmp->poids_arc=G->matrice[i][j];
}
else
{
tmp->suivant=(TypeArc*)malloc(sizeof(TypeArc));
tmp->suivant->extremite=s;
tmp=tmp->suivant;
tmp->poids_arc=G->matrice[i][j];
}
}
}
}
printf("fin");
return Gp;
}
TypeSommet * trouve_min(TypeSommet * liste_sommet) // trouve le minimum des sommet du plus petit poids
{
TypeSommet * sommet,*mini;
int min = liste_sommet->poids;
for (sommet=liste_sommet->suivant;sommet!=NULL;sommet=sommet->suivant)
{
if(sommet->poids < min && sommet->marque==0 && sommet->poids>=0)
{
min = sommet->poids;
mini = sommet;
}
}
return mini;
}
void LireChemin(TypeSommet * liste_sommet) // trouve le minimum des sommet du plus petit poids
{
TypeSommet * sommet;
for (sommet=liste_sommet->suivant;sommet!=NULL;sommet=sommet->suivant)
{
printf("sommet : %d \n",sommet->nom);
}
}
void Dijkstra(TypeSommet *s)
{
TypeSommet *min;
TypeArc * arc;
int nb_sommets=4,i=0;
while (i<nb_sommets)
{
min = trouve_min(s);
min->marque=1;
for (arc = min->liste_arcs->suivant; arc != NULL ; arc=arc->suivant) // recherche des fils
{
if ((arc->extremite->marque == 0 && min->poids + arc->poids_arc < arc->extremite->poids) || (arc->extremite->poids ==-1))
{
arc->extremite->poids = min->poids + arc->poids_arc;
arc->extremite->antecedent = min;
}
}
i+=1;
}
LireChemin(s);
}
int main()
{
Graphe G;
G = InitMat();
Dijkstra(convert_list(&G));
printf("finiggg");
return 0;
} |
Partager