👤

stie cineva cum se face un apm (arbore partial de cost minim) programat dinamic la info?
ps: se include doar biblioteca
#include <fstream>      //, urmata de
using namespace std;


Răspuns :

#include<iostream.h>
typedef int mat[20][20];
typedef int sir[20];
mat cost;
int i,k,j,n,pl;
double min,costul;
main(){
cout<<"n=";cin>>n;
for(i=1;i<=n-1;i++)
for(j=i+1;i<=n;j++)
{
cout>>cost["<<i<<","<<j<<"]=";
cin>>cost[i][j];
cost[j][i]=cost[i][j];
costul=0;
cout<<"nodul de plecare:";cin>>pl;
ap[pl]=0;
for(i=1;i<=n;i++)
 if(i!=pl)ap[i]=pl;
for(k=1;k<=n-2;k++)
{
min=100000;
for(i=2;i<=n;i++)
if(ap[i]!=0)
if(cost[i][ap[i]]<min)
{
min=cost[i]ap[i];
j=i;
}
tata[j]=ap[j];
costul=costul+cost[ap[j]][j];
ap[j]=0;
for(i=1;i<=n;i++)
if((ap[i]!=0)&&(cost[i][ap[i]]>cost[i][j]))
ap[i]=j;
}
cout<<  ->aici scrii ce anume doresti sa afiseze programul.




Testeaza-l in Mingw sa vezi daca il poti compila,probabil sunt sanse sa fi gresit la scriere.Bafta!