Sunteți pe pagina 1din 8

TEM PROGRAMARE

Student:
Grupa:

Programul surs:
// livraripiese.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "string.h"
#define NUM_ORASE 11
#define MAX_INT 65535
#define NUM_PIESE 25
#define NUM_MASINI 3
class autoutilitara
{
public:
autoutilitara(char* nr_inmatric, int sarc, int vol): sarcina(sarc), volum(vol)
{
strcpy(nr_inmatriculare, nr_inmatric);
}
~autoutilitara()
{
}
char nr_inmatriculare[7];
int sarcina;
int volum;
};
class oras
{
public:
oras()
{}//constructor null
char nume[20];
char index;//indexul in vectorul de orase
};
class piesa
{
public:
piesa(int greut, int vol,int indexdest):greutate(greut), volum(vol), dest(indexdest)
{
}
int greutate;
int volum;
int dest;
};
class graf
{
public:
graf(unsigned short m_distante[], char nume_orase[][20])
{
char i;
memcpy(distante,m_distante,NUM_ORASE*NUM_ORASE*sizeof(unsigned
short) );
orase= new oras[NUM_ORASE];
for (i=0;i<NUM_ORASE;i++)
{

orase[i].index=i;
strcpy(orase[i].nume, nume_orase[i]);

}
unsigned short distante[NUM_ORASE][NUM_ORASE];
oras* orase;
void FindShortestPath(piesa** piese,autoutilitara** autoutil );

bool SunttoateOrasele(char*traseu)//DACA SUNT TOATE ORASELE ACOPERITE INTOARCE 1


//DACA NU INTOARCE 0
{
int i,j,selected;
for( i=0;i<NUM_ORASE; i++)
{
selected=0;
for(j=0; j < strlen(traseu); j++)
if(traseu[j]==orase[i].index)
{
selected= 1;
break;
}
if(!selected)
return 0;
}
return 1;
}
bool EsteOrasulSelectat(char index, char*traseu)//DACA ORASUL ESTE ACOPERIT ANTERIOR
INTOARCE 1
//
{
int i,j,selected;
for(j=0; j < strlen(traseu); j++)
{
if(traseu[j]==index)
{
selected= 1;
return 1;
}
}
if(index==0)
return 1;
return 0;
}
};
void graf::FindShortestPath(piesa**piese,autoutilitara** autoutil)
{
//INCARCAM AUTOUTILITARA
int k, l=0, n=0;
int masa_totala=0;
int volum_total=0;
for (k=0; (k<3)&&(l<25); )
{
masa_totala+=piese[l]->greutate;
volum_total+= piese[l++]->volum;

if((masa_totala>= autoutil[k]->sarcina)||(volum_total>=autoutil[k]>volum))

printf("SE INCARCA PESELE CU INDEXUL DE LA %d LA %d IN


AUTOUTILITARA %d\n\r", n, l, k);
n=l;
k++;
}
}
int index, first, second, previous;
char traseu[22];
memset(traseu, 0, 22);
traseu[0]=0;//ORASUL DE PORNIRE ESTE 0
first = 0;
index = 0;
previous=0;
int indexintoarcere=0;
while(!SunttoateOrasele(traseu)&&(index<22))
{
//CAUTA CEL MAI APROPIAT ORAS
unsigned int cost=MAX_INT;
for(int i=0; i<NUM_ORASE; i++)
{
if((!this->EsteOrasulSelectat(i, traseu))&&(i!=first))//DACA ORASUL ESTE
CUPRINS ANTERIOR
if (distante[i][first]< cost)
//NU CALCULAM COSTUL
{
cost=distante[i][first];
second= i;
}
}
if(cost==MAX_INT)
{//NE INTOARCEM INAPOI PENTRU CA NU S-A GASIT UN ORAS
NOU APROPIAT

if(index>indexintoarcere)
first=indexintoarcere;
traseu[index++]=first;
else

}
{

indexintoarcere=second;
previous= first;
first= second;
traseu[index++]= second;
printf("mergem in orasul %s pe distanta

%d\n\r",orase[second].nume, cost );
}
}

}
unsigned short
matr_distante[]=//MATRICE SIMETRICA
{0, 100, 50, 85, MAX_INT, MAX_INT, 110, MAX_INT, MAX_INT, MAX_INT, MAX_INT,
100, 0, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 100, MAX_INT, 350, 400, MAX_INT,
50, MAX_INT, 0, MAX_INT, 70, MAX_INT, 50, MAX_INT, MAX_INT, MAX_INT, MAX_INT,
85, MAX_INT, MAX_INT, 0, 50, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT,

MAX_INT, MAX_INT, 70, 85, 0, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT,
MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 0, 250, 110, MAX_INT, MAX_INT, 150,
110, 100, 50, MAX_INT, MAX_INT, 250, 0, MAX_INT, MAX_INT, MAX_INT, MAX_INT,
MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 110, MAX_INT, 0, MAX_INT, MAX_INT, 140,
MAX_INT, 350, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 0, 200, MAX_INT,
MAX_INT, 400, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 200, 0, 320,
MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 150, MAX_INT, 140, MAX_INT, 320, 0 };
unsigned int
volum_piesa[25]={10, 30, 240, 140, 10, 80, 35, 100, 240, 100, 230, 100, 320, 40, 20, 150,
140, 100, 90, 200, 210, 30, 100, 14, 20};
unsigned int
greutate[]={4, 7, 10, 79, 267, 460, 2, 50, 200, 16, 25, 10, 300, 10, 149, 170, 8, 275, 10, 20,
29, 126, 16, 266, 76};
unsigned int
destinatie[]={1,2,3,4,5,6,7,8,9,10,6,9,5,8,10,4,2,7,4,8,1,9,3,6,9};
char nume_orase[][20]=
{
{"BUCURESTI"},//ORASUL DE PLECARE
{"PITESTI"},
{"PLOIESTI"},
{"TARGOVISTE"},
{"SINAIA"},
{"BACAU"},
{"BUZAU"},
{"IASI"},
{"TARGU MURES"},
{"CLUJ NAPOCA"},
{"SUCEAVA"}
};
char numar_inmatric[3][7]=
{
{"B01PSE"},
{"B02PSE"},
{"B03NUV"}
};
unsigned short sarcina[3]=//kg
{
2000,
2000,
2500
};
unsigned short volum[3]=//l
{
4000,
4000,
5000
};

int _tmain(int argc, _TCHAR* argv[])


{
//INITIALIZARI
int i;
printf("SE INCARCA DATELE\n\r");
autoutilitara* autoutil[3];
piesa* lista_piese[NUM_PIESE];
for(i=0; i<NUM_MASINI; i++)
{

autoutil[i]= new autoutilitara(numar_inmatric[i], sarcina[i], volum[i]);


}
for( i=0; i<NUM_PIESE; i++)
{
lista_piese[i]= new piesa(greutate[i], volum_piesa[i], destinatie[i]);
}
graf m_graf(matr_distante, nume_orase);
printf("SE CALCULEAZA DRUMUL OPTIM\n\r");
m_graf.FindShortestPath(lista_piese, autoutil);
return 0;
}

Descrierea programului:
Programul a fost rulat folosind o matrice de distane construit innd cont de un
numr de 11 orase i distanele dintre ele. Matricea are dimensiunea 11x11 i
este simetric. Distana de la un ora la el nsui este 0, iar oraele neconectate
prin drum direct au distana maxima MAXINT=65535. Oraele sunt prezentate
n vectorul char nume_orase[][20]=
{
{"BUCURESTI"},//ORASUL DE PLECARE, {"PITESTI"},{"PLOIESTI"},{"TARGOVISTE"},
{"SINAIA"},
{"BACAU"},{"BUZAU"},{"IASI"},{"TARGU MURES"},{"CLUJ NAPOCA"},{"SUCEAVA"}};
Matricea distanelor este: {0, 100, 50, 85, MAX_INT, MAX_INT, 110, MAX_INT, MAX_INT,
MAX_INT, MAX_INT,
100, 0, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 100, MAX_INT, 350, 400, MAX_INT,
50, MAX_INT, 0, MAX_INT, 70, MAX_INT, 50, MAX_INT, MAX_INT, MAX_INT, MAX_INT,
85, MAX_INT, MAX_INT, 0, 50, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT,
MAX_INT, MAX_INT, 70, 85, 0, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT,
MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 0, 250, 110, MAX_INT, MAX_INT, 150,
110, 100, 50, MAX_INT, MAX_INT, 250, 0, MAX_INT, MAX_INT, MAX_INT, MAX_INT,
MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 110, MAX_INT, 0, MAX_INT, MAX_INT, 140,
MAX_INT, 350, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 0, 200, MAX_INT,
MAX_INT, 400, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 200, 0, 320,
MAX_INT, MAX_INT, MAX_INT, MAX_INT, MAX_INT, 150, MAX_INT, 140, MAX_INT, 320, 0 };

A fost considerat un numr de 3 autoutilitare i un numr de 25 de piese


cu caracteristicile indicate n vectorii volum_pies(litri) i greutate(kg)
respectiv:
volum_piesa[25]={10, 30, 240, 140, 10, 80, 35, 100, 240, 100, 230, 100, 320, 40, 20, 150,
140, 100, 90, 200, 210, 30, 100, 14, 20}
greutate[]={4, 7, 10, 79, 267, 460, 2, 50, 200, 16, 25, 10, 300, 10, 149, 170, 8, 275, 10, 20,
29, 126, 16, 266, 76}

Autoutilitarele au avut caracteristicile definite de ctre vectorii:


char numar_inmatric[3][7]=
{
{"B01PSE"},
{"B02PSE"},
{"B03NUV"}
};
unsigned short sarcina[3]=//kg
{
2000,
2000,

2500
};
unsigned short volum[3]=//l
{
4000,
4000,
5000}

Autoutilitarele au fost numerotate de la 0, 1, 2 i au fost ncarcate conform


condiilor de greutate i volum impuse de autoutilitar. Astfel s-au obinut
urmtoarele rezultate:

Distanele optime ntre orae au fost determinate folosindu-se oraul de


index 0 (Bucuresti) drept start i s-a cutat drumul minim din ora n ora ,
folosindu-se o adaptare a unui algoritm de drum minim.
7

Versiunea de soft folosit este Microsoft Visual Studio Proffesional 2010


ver.10.0.30319.1

Bibliografie: - Algoritmi de drum minim in grafuri(Dijkstra) sursa Internet.


- Ajutor din partea unor persoane apropiate mie.

S-ar putea să vă placă și