# Include <stdio. h>
# Include <math. h>
# Include <stdlib. h>
# Include <time. h>
# Define t0 1000
# Define tf0.01
# Define t 0.95
# Define n 1000
# Define M 50
Int weight [m] = {80, 82, 85, 70, 72, 70, 66, 50, 55, 25,
50, 55, 40, 48, 50, 32, 22, 60, 30, 32,
40, 38, 35, 32, 25, 28, 30, 22, 50, 30,
45, 30, 60, 50, 20, 65, 20, 25, 30, 10,
20, 25, 15, 10, 10, 10, 4, 4, 2, 1 },
Profit [m] = {220,208,198,192,180,180,165,162,160,158,
155,130,125,122,120,118,115,110,105,101,
100,100, 98, 96, 95, 90, 88, 82, 80, 77,
75, 73, 72, 70, 69, 66, 65, 63, 60, 58,
56, 50, 30, 20, 15, 10, 8, 5, 3, 1 },
Contain = 1000;
Int premaxp = 0, bestmaxp = 0;
Int A [m] = {0}, preseq [m] = {0}, bestseq [m] = {0 };
Int calprofit (int A [m])
{
Int I = 0;
Int p = 0;
For (I = 0; I <m; I ++)
P = P + (A [I] * profit [I]);
Return P;
}
Int calweight (int A [m])
{
Int I = 0;
Int W = 0;
For (I = 0; I <m; I ++)
{
W = W + (weight [I] * A [I]);
}
Return W;
}
Void initialize (void)
{
Int I = 0;
For (I = 0; I <m; I ++)
{
A [I] = 0;
Preseq [I] = 0;
Bestseq [I] = 0;
}
Premaxp = calprofit ();
Bestmaxp = premaxp;
}
Void getrand (int * I, int * j)
{
* I = 0;
* J = 0;
While (* I = * j)
{
* I = rand () % 50;
* J = rand () % 50;
}
}
Void change (void)
{
Int R1 = 0, R2 = 0;
Getrand (& R1, & R2 );
A [R1] = 1;
A [R2] = 1;
If (calweight (a)> contain)
{
A [R2] = 0;
}
If (calweight (a)> contain)
{
A [R1] = 0;
}
}
Void SA (void)
{
Double T = T0;
Int I = 0, j = 0;
Int dif1 = 0, dif2 = 0;
Double P = 0.0;
While (T> TF)
{
For (I = 0; I <n; I ++)
{
Change ();
Dif1 = calprofit (a)-bestmaxp;
If (dif1> 0)
{
For (j = 0; j <m; j ++)
{
Preseq [J] = A [J];
Bestseq [J] = A [J];
}
Premaxp = calprofit ();
Bestmaxp = premaxp;
}
Else
{
Dif2 = calprofit (a)-premaxp;
If (dif2> 0)
{
For (j = 0; j <m; j ++)
{
Preseq [J] = A [J];
}
Premaxp = calprofit ();
}
Else
{
P = rand () % 20001/20000. 0;
If (exp (dif2)/T)> P)
{
For (j = 0; j <m; j ++)
{
Preseq [J] = A [J];
}
Premaxp = calprofit ();
}
Else
{
For (j = 0; j <m; j ++)
{
A [J] = preseq [J];
}
}
}
}
}
T = T * t;
}
}
Int main (void)
{
Int I;
Srand (unsigned) Time (null ));
Initialize ();
SA ();
For (I = 0; I <m; I ++)
{
If (0 = (I % 5 ))
{
Printf ("");
}
Printf ("% d", bestseq [I]);
}
Printf ("\ nthe best value is % d \ n", bestmaxp );
Return 1;
}