模拟退火|爬山水题记录

· · 个人记录

A

P1337 [JSOI2004]平衡点 / 吊打XXX

模拟退火的教学题。但感觉爬山应该更好。

算是比较契合温度的题目,因为步长可以根据温度调整。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define double long double
#define debug(x) cout<<#x<<':'<<x<<endl
const int N = 1e4+5;
int n,x[N],y[N],w[N];
double ansx,ansy,dis;
double Rand(){return (double)(rand())/RAND_MAX;}
double calc(double xx,double yy)
{
    double res=0;
    for(int i=1;i<=n;i++)
    {
        double dx=x[i]-xx,dy = y[i]-yy;
        res += sqrt(dx*dx+dy*dy)*w[i];
    }
    if(res<dis) dis = res,ansx = xx,ansy = yy;
    return res;
}
void solve()
{
    double t = 100000;
    double nowx=ansx,nowy=ansy;
    while(t>1e-3)
    {
        double nxtx = nowx + t*(Rand()*2-1);
        double nxty = nowy + t*(Rand()*2-1);
        double delta = calc(nxtx,nxty)-calc(nowx,nowy);
        if(exp(-delta/t) > Rand() )nowx = nxtx,nowy = nxty;
        t *= 0.99;
    }
    for(int i=1;i<=1000;i++)
    {
        double nxtx = ansx + t*(Rand()*2-1);
        double nxty = ansy + t*(Rand()*2-1);
        calc(nxtx,nxty);
    }
}
int main()
{
    srand(time(NULL));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",&x[i],&y[i],&w[i]);
        ansx += x[i],ansy += y[i];
    }
    ansx /= n; ansy /= n;
    dis = calc(ansx,ansy);
    solve();
    printf("%.3Lf %.3Lf\n",ansx,ansy);
    return 0;
}

B

P2503 [HAOI2006]均分数据

随机化贪心就可以了,不需要爬山,也不需要模拟退火。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<':'<<x<<endl
double ans = 1e16,ave;
int a[30],b[30];
int n,m;
void solve()
{
    for(int i=1;i<=m;i++)b[i] = 0;
    for(int i=1;i<=n;i++)
    {
        int p=1;
        for(int j=1;j<=m;j++)
            if(b[j]<b[p]) p = j;
        b[p]+=a[i];
    }
    double now = 0;
    for(int i=1;i<=m;i++)now+=(b[i]-ave)*(b[i]-ave);
    now = now/m;
    ans = min(ans,now);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ave+=a[i];
    ave /= m;
    for(int i=1;i<=500000;i++)
    {
        random_shuffle(a+1,a+n+1);
        solve();
    }
    printf("%.2f",sqrt(ans));
    return 0;
}

C

P2210 Haywire

也是随机化贪心就可以过。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<':'<<x<<endl
int ans=1e9;
int a[30],b[30][4];
int n,m;
void solve()
{
    int now = 0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=3;j++)now += abs(a[i]-a[b[i][j]]);
    now = now/2;
    ans = min(ans,now);
}
int main()
{
    srand(100);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)a[i]=i;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=3;j++)scanf("%d",&b[i][j]);
    while(clock()<CLOCKS_PER_SEC*0.9)
    {
        random_shuffle(a+1,a+n+1);
        solve();
    }
    printf("%d",ans);
    return 0;
}

D

P2538 [SCOI2008]城堡

可以先用floyd求出最短路。

实测过随机化贪心不行。没有明显的单峰性质,还是模拟退火吧。

初温要设低一点。搞了几乎过不了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<':'<<x<<endl
const int inf = 1e9;
int ans = inf;
int n,m,k,res;
const int N = 52;
int bo[N],G[N][N];
int h[N],cnt,a[N];
double solve()
{
    int now = 0;
    for(int i=0;i<n;i++)if(!bo[i])
    {
        int s = inf;
        for(int j=0;j<n;j++)if(bo[j])
            s=min(s,G[i][j]);
        now = max(now,s);
        if(now>ans) break;
    }
    ans = min(ans,now);
    return now;
}
double myrand()
{
    return (double)(rand())/RAND_MAX;
}
void SA()
{
    double t = 1;
    while(t>1e-4)
    {
        int x=rand()%k+1,y=rand()%res+1;
        double delta = -solve();
        bo[h[x]] = 0; swap(h[x],h[y+k]);bo[h[x]] = 1;
        delta += solve();
        if(exp(-delta/t)>myrand());
        else 
            bo[h[x]] = 0,swap(h[x],h[y+k]),bo[h[x]]=1;  
        t*=0.98;
    }
}
int main()
{
    srand(time(0));
    scanf("%d%d%d",&n,&m,&k);
    res = n-m-k;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)G[i][j] = inf;
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]),G[i][i]=0;
    for(int i=0,x;i<n;i++)
        scanf("%d",&x),G[a[i]][i]=G[i][a[i]]=min(G[a[i]][i],x);
    for(int k=0;k<n;k++)
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
    for(int i=0,x;i<m;i++)
        scanf("%d",&x),bo[x]=1;
    for(int i=0;i<n;i++)
        if(!bo[i]) h[++cnt] = i;
    for(int i=1;i<=k;i++)bo[h[k]]=1;
    if(!res)
    {
        for(int i=0;i<n;i++)bo[i]=1;
        solve();cout << ans;return 0;
    }
    while(clock() < 0.75*CLOCKS_PER_SEC)SA();
//  for(int i=1;i<=200;i++) SA();
    cout << ans;
    return 0;
}

E

P3878 [TJOI2010]分金币

实测随机化贪心不行 。

模拟退火可以轻易解决。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<':'<<x<<endl
int T,n;
const ll inf = 1e16;
int a[32],cut;
double myrand(){return double(rand())/RAND_MAX;}
ll ans;
ll solve()
{
    ll now = 0;
    for(int i=1;i<=cut;i++)now += a[i];
    for(int i=cut+1;i<=n;i++)now -= a[i];
    now = abs(now);
    ans = min(ans,now);
    return now;
}
void SA()
{
    double t = 1000;
    while(t>1e-4)
    {
        int x=rand()%cut+1,y=rand()%(n-cut)+1+cut;
        double delta=-solve();
        swap(a[x],a[y]);
        delta+=solve();
        if(exp(-delta/t)>myrand());
        else swap(a [x],a[y]);
        t*=0.98;
    }
}
int main()
{
    scanf("%d",&T);
    srand(time(NULL));
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        if(n==1)
        {
            printf("%d\n",a[1]);
            continue;   
        }
        cut = n/2;
        ans = inf;
        for(int i=1;i<=200;i++)SA();
        printf("%lld\n",ans);
    }
    return 0;
}

F

P3936 Coloring

调参调了很久。

也是要模拟退火,初温要低,然后冷却也要慢。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<':'<<x<<endl
double myrand(){return (double)(rand())/RAND_MAX;}
const int inf = 1e9;
int ans=inf;
int n,m,c;
int a[22][22],b[22][22],p[52];
int solve()
{
    int now = 0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            int x=i+1,y=j;
            if(x<=n && y<=m && a[x][y]!=a[i][j])now++;
            x=i;y=j+1;
            if(x<=n && y<=m && a[x][y]!=a[i][j])now++;
        }   
    if(now<ans)
    {
        ans = now;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                b[i][j] = a[i][j];
    }
    return now;
}
void SA()
{
    double t = 1;
    while(t>1e-14)
    {
        int x1=rand()%n+1,y1=rand()%m+1,
        x2=rand()%n+1,y2=rand()%m+1;
        int delta = -solve();
        swap(a[x1][y1],a[x2][y2]);
        delta += solve();
        if(exp(-delta/t)>myrand());
        else swap(a[x1][y1],a[x2][y2]);
        t *= 0.9999;
    }
}
int main()
{
    srand(time(NULL));
    scanf("%d%d%d",&n,&m,&c);
    for(int i=1;i<=c;i++)scanf("%d",&p[i]);
    for(int i=1,t=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            a[i][j] = t;
            p[t]--;if(!p[t])t++;
        }
    while(clock()<4.6*CLOCKS_PER_SEC)SA();
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            printf("%d ",b[i][j]);
        printf("\n");
    }
    return 0;
}

G

P4035 [JSOI2008]球形空间产生器

理论上来说并不是完全单峰的。

所以设了个温度来爬山,和模拟退火挺像的。

因为有很多维,要同时最优比较难。

所以只进行同时调整。

求出平均的dis。然后往那边拉。

就是假装有个力把他拉过去。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<':'<<x<<endl
double myrand(){return double(rand())/RAND_MAX;}
const double inf = 1e8;
double rans=inf;
const int N = 13;
double a[N][N],ans[N];
int n;
double dist(int x)
{
    double h=0;
    for(int i=1;i<=n;i++)
        h+=(a[x][i]-ans[i])*(a[x][i]-ans[i]);
    return sqrt(h);
}
double tmp[N],dis[N];
void SA()
{
    double t = 10000;
    while(t>1e-5)
    {
        double tot = 0;
        for(int i=1;i<=n+1;i++)dis[i]=dist(i),tot+=dis[i];
        tot /= (n+1);
        for(int i=1;i<=n;i++)tmp[i]=0;
        for(int i=1;i<=n+1;i++)
            for(int j=1;j<=n;j++)
                tmp[j] += (dis[i]-tot)*(a[i][j]-ans[j])/tot;
        for(int i=1;i<=n;i++)
            ans[i] += tmp[i]*t;
        t*=0.99995;
    }
}
int main()
{
    srand(time(NULL));
    scanf("%d",&n);
    for(int i=1;i<=n+1;i++)
        for(int j=1;j<=n;j++)scanf("%lf",&a[i][j]),ans[j]+=a[i][j];
    for(int i=1;i<=n;i++)ans[i]/=(n+1);
    SA();
    for(int i=1;i<=n;i++)printf("%.3lf ",ans[i]);
    return 0;
}

H

P4274 [NOI2004] 小H的小屋

贪心题。均分一下,枚举割点即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<':'<<x<<endl
double k1,k2;
int rblock,lblock;
double ans=1e9;
int L,R;
int n,m;
double calc(int block,int len,double k)
{
    if(!block)return 0;
    int llen,rlen,lnum,rnum;
    llen = len/block;rlen = llen+1;
    rnum=len%block;lnum=block-rnum;
    return llen*llen*k*lnum+rlen*rlen*k*rnum;
}
void solve()
{
    if(n%m == 0)
    {
        printf("%.1lf\n",calc(L,100,k1)+calc(L*lblock,100,k2));
        return ;
    }
    int bd=100-R*rblock;
    double tmp;
    for(int cut=lblock*L;cut<=bd;cut++)
    {   
        tmp=calc(L,cut,k1)+calc(R,100-cut,k1)+calc(lblock*L,cut,k2)+calc(rblock*R,100-cut,k2);
        if(ans>tmp)ans=tmp;
        else break;
    }
    printf("%.1lf\n",ans);
}
int main()
{
    scanf("%lf%lf%d%d",&k1,&k2,&m,&n);
    R = n%m; L = m-R;
    lblock = n/m,rblock=n/m+1;
    solve();
    return 0;
}

I

P5544 [JSOI2016]炸弹攻击1

和前面没啥区别。

不过发现了初温高也是有用的。

而且取大于的时候写法有那么一丢丢不一样。

还有就是多次SA时,将答案重置为初始会更好一些。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<':'<<x<<endl
const double inf = 1e14,eps = 1e-7;
inline double sqr(double x){return x*x;}
int ans = 0;
const int N = 1020;
int n,m,R,x[N],y[N],r[N],ex[N],ey[N];
int solve(double nowx,double nowy)
{
    double mind = inf;
    for(int i=1;i<=n;i++)
        mind = min(mind,sqrt(sqr(x[i]-nowx)+sqr(y[i]-nowy))-r[i]);
    if(mind<0) return 0;
    mind = min(mind,(double)R);
    int now = 0;
    for(int i=1;i<=m;i++)
        if(sqrt(sqr(ex[i]-nowx)+sqr(ey[i]-nowy))<mind+eps)
            now++;
    ans = max(ans,now);
    return now;
}
double myrand(){return ((double)rand())/RAND_MAX;}
double nowx,nowy;
void SA()
{
    double t = 100000;
    while(t>1e-3)
    {
        double nxtx = nowx+t*(myrand()*2-1),nxty = nowy+t*(myrand()*2-1);
        double delta = solve(nowx,nowy)-solve(nxtx,nxty);
        if(delta<0)
        {
            nowx=nxtx;
            nowy=nxty;
        }else
        if(exp(delta/t)<myrand())nowx=nxtx,nowy=nxty;
        t*=0.998;
    }
}
int main()
{
    srand(time(NULL));
    scanf("%d%d%d",&n,&m,&R);
    double sumx=0,sumy=0;
    for(int i=1;i<=n;i++)
        scanf("%d%d%d",&x[i],&y[i],&r[i]);
    for(int i=1;i<=m;i++)scanf("%d%d",&ex[i],&ey[i]),sumx+=ex[i],sumy+=ey[i];
    solve(sumx/m,sumy/m);
    while(clock()<CLOCKS_PER_SEC*0.9)nowx=sumx/m,nowy=sumy/m,SA();
    cout << ans;
    return 0;
}