0% found this document useful (0 votes)
37 views6 pages

Dare de Seama: Algoritmul Metodei Aproximative de Rezolvare A Problemei Comisvoiajorului

The document describes an algorithm for approximately solving the traveling salesman problem. It presents the C++ code for a program that implements the algorithm. The program takes input for the number of cities n and routes m, cost matrix c, adjacency matrix a and route limits b. It then uses a heuristic approach to find a solution vector x that minimizes the total cost while satisfying the route limits. The program outputs the solution vector x and its total cost.

Uploaded by

Jenika Capmari
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views6 pages

Dare de Seama: Algoritmul Metodei Aproximative de Rezolvare A Problemei Comisvoiajorului

The document describes an algorithm for approximately solving the traveling salesman problem. It presents the C++ code for a program that implements the algorithm. The program takes input for the number of cities n and routes m, cost matrix c, adjacency matrix a and route limits b. It then uses a heuristic approach to find a solution vector x that minimizes the total cost while satisfying the route limits. The program outputs the solution vector x and its total cost.

Uploaded by

Jenika Capmari
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Universitatea de Stat din Moldova

Dare de seama
Algoritmul metodei aproximative de
Tema:
rezolvare a problemei comisvoiajorului

Executat de: Duca Evghenia MA31

Verificat de: Andrei Postaru, doc. conf.

Chisinau 2011
Testul programului:

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

#include <stdlib.h>

int n,m,i,j;

int c[3][100];

int b[100];

int a[100][100];

int x[100];

int w[100];

int q;

int max;

int maxn;

int u,y,o,s;

void control ()

for ( i = 0 ; i < n ; i++ )

u = c[2][i];

y = 0;

s = 0;

x[u] = 1;

for ( j = 0 ; j < m ; j++ )

for ( o = 0 ; o < n ; o++ )

s+= a[j][o]*x[o];

if ( s <= b[j] ) y++;

s=0;
}

if ( y != m ) x[u] = 0;

//cout<<"pasul"<<i<< "vectorul este";

//for (max =0 ; max < n ; max++)

//cout <<x[max];

//cout <<endl;

};

s = 0;

cout << "Vectorul x este: ";

for ( i = 0 ; i < n ; i++ )

cout <<x[i] << " ";

s+= c[0][i] * x[i];

cout << endl << "valoarea functiei este " << s;

getch ();

void main ()

clrscr ();

randomize ();

cout << "introduceti n si m" << endl;

cin >> n;

cin >> m;

cout <<"Introduceti vectorul c"<< endl;


for ( i = 0 ; i < n ; i++ )

cout <<"c[" << i << "]=";

cin >> c[0][i];

c[1][i] = c[0][i];

c[2][i] = i;

x[i] = 0;

w[i] = i;

cout << endl;

cout << "Introduceti matricia a"<< endl;

for ( i = 0 ; i < m ; i ++ )

for ( j = 0; j < n ; j++ )

cout <<"a["<<i<<"]["<<j<<"]=";

cin >> a[i][j];

cout <<"Introduceti vectorul b"<<endl;

for ( i = 0 ; i < m ; i++ )

cout <<"b["<<i<<"]=";

cin >> b[i];

clrscr ();

for ( j = 0; j < n ; j++ )

max = c[1][j];

maxn = c[2][j];

for ( i = j+1 ; i < n ; i++ )

if ( max < c[1][i] )


{

max = c[1][i];

maxn = c[2][i];

y = i;

c[1][y] = c[1][j];

c[1][j] = max;

c[2][y] = c[2][j];

c[2][j] = maxn;

//

for (q = 0 ; q < n ; q++) cout <<c[2][q];

control ();

int qwe=0;

for (;;)

cout << endl << "Pentru a lua un vector aleator apasati 1" << endl;

cin >> qwe;

if ( qwe != 1 ) break;

for ( i = 0 ; i < n ; i++ )

x[i] = 0;

w[i] = i;

int r,e;

e=n;

for ( i = 0 ; i < n ; i++)

x[i] = 0;
r = random (e);

c[2][i] = w[r];

c[1][i] = c[0][w[r]];

e--;

for ( j = r; j < e ; j++ )

w[j] = w[j+1];

};

for ( i = 0 ; i < n ; i++ )

cout << c[2][i] <<" ";

getch();

control ();

};

};

Executarea programului: Ex. Rezolvat pe foaie.

You might also like