小P的图论课 (模拟退火)

uses math;
const maxn=;
INF=;
var n,m,i,x,y,sum,ans,delta:longint;
map:array[..maxn,..maxn] of longint;
flag:array[..maxn] of boolean;
T:double;
ok:boolean;
function ran:double;
begin
exit(random()/);
end;
procedure add(x,y:longint);
begin
inc(map[x,]); map[x,map[x,]]:=y;
end;
begin
randomize;
readln(n,m);
for i:= to m do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
fillchar(flag,sizeof(flag),true);
T:=0.1;
sum:=n; ans:=INF;
while T>0.000001 do
begin
T:=T*0.999;
repeat
x:=random(n)+;
until flag[x];
delta:=-;
for i:= to map[x,] do
begin
if map[x,i]=x then inc(delta);
if not flag[map[x,i]] then inc(delta);
end;
ok:=false;
if delta< then ok:=true
else
if ran<exp(-delta/T) then ok:=true;
if ok then
begin
sum:=sum+delta;
if sum<ans then ans:=sum;
flag[x]:=false;
for i:= to map[x,] do
flag[map[x,i]]:=true;
end;
end;
writeln(ans);
end.
上一篇:C语言错误: HEAP CORRUPTION DETECTED


下一篇:【iCore3 双核心板】例程十二:通用定时器实验——定时点亮LED