3/14 - Algoritmul Roy-Warshall - Drum Minim cu costuri
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int a[100][100],n,m;
void citire()
{ifstream f("date");
f>>n>>m;
int x,y,z,i,j;
for(i=1;i<=m;i++)
{f>>x>>y>>z;
a[x][y]=z;}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]==0)
a[i][j]=999;
}
void afisare()
{int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)
cout<<setw(4)<<a[i][j];
cout<<endl;}
cout<<endl;
}
void roy()
{int k,i,j;
for(k=1;k<=n;k++){
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]>a[i][k]+a[k][j])
a[i][j]=a[i][k]+a[k][j];
afisare();}
}
int main()
{citire();
roy();
afisare();
}
Fisier
4 6
1 2 10
2 3 5
2 4 7
3 4 1
4 1 3
4 2 20
marți, 13 martie 2012
by DlMuresan
Categories:
Graf,
graf orientat,
Roy-Warshall
|