码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-30 15:53:04      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

uses math;
const maxn=105;
      INF=2000000000;
var n,m,i,x,y,sum,ans,delta:longint;
    map:array[0..maxn,0..maxn] of longint;
    flag:array[0..maxn] of boolean;
    T:double;
    ok:boolean;
function ran:double;
begin
    exit(random(12345)/12345);
end;
procedure add(x,y:longint);
begin
    inc(map[x,0]); map[x,map[x,0]]:=y;
end;
begin
    randomize;
    readln(n,m);
    for i:=1 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)+1;
        until flag[x];
        delta:=-1;
        for i:=1 to map[x,0] do
        begin
            if map[x,i]=x then inc(delta);
            if not flag[map[x,i]] then inc(delta);
        end;
        ok:=false;
        if delta<0 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:=1 to map[x,0] do
                flag[map[x,i]]:=true;
        end;
    end;
    writeln(ans);
end.

 

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

标签:

原文地址:http://www.cnblogs.com/rpSebastian/p/4378021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!