Acasa Tehnologie Algoritmul Bellman-Ford

Algoritmul Bellman-Ford

by Dragos Schiopu

>

//algoritmul Bellman-Ford
#include<stdio.h>
#include<conio.h>
int a[20][20],d[20],r,n,i,j;

void citire(void)
{
FILE *f=fopen("c:in.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
}

void main()
{
citire();
printf("Dati nodul initial r=");
scanf("%d",&r);
//se copiaza linia r din matr costurilor directe in vectorul de cost
for(i=1;i<=n;i++)
d[i]=a[r][i];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(d[j]>d[i]+a[i][j]) d[j]=d[i]+a[i][j];

for(i=1;i<=n;i++)
printf("Costul minim de la %d la %d este %dn",r,i,d[i]);
getch();
clrscr();
}

s-ar putea sa-ti placa