第五、六章 贪心算法、回溯算法

贪心算法

适合于贪心算法求解的问题具有:贪心选择性质、最优子结构性质。
贪心算法可以获取到问题的局部最优解,不一定能获取到全局最优解。
贪心算法总是作出在当前看来最好的选择;并且每次贪心选择都能将问题化简为一个更小的与原问题具有相同形式的子问题。

活动安排问题

回溯算法

应用回溯法求解时,需要明确定义问题的解空间。
旅行商问题–>使用排列数解决。

用约束函数在扩展结点处剪去不满足约束条件的子树;
用限界函数剪去不能得到最优解的子树。

三个步骤:
1.针对所给问题,定义问题的解空间;
2.确定易于搜索的解空间结构;
3.以深度优先的方式搜索解空间,并且在搜索过程中使用剪枝函数避免无效搜索。
子集树:

void backtrack (int t)  // 形参t为树的深度,根为1
{
    if(t>n)
    {
        update(t);
        return;
    }
    for(int i=0;i<2;i++)
    {
        x[t]=i;
        if(constrant(t)&&bound(t))
            backtrack(t+1);
    }
}

排列树:

void backtrack (int t)  // 形参t为树的深度,根为1
{
    if(t>n)
    {
        update(t);
        return;
    }
    for(int i=0;i<2;i++)
    {
        x[t]=i;
        if(constrant(t)&&bound(t))
            backtrack(t+1);
    }
}

素数环问题

int n,a[N],ans;
bool vis[N];
int check(int x,int y)
{
    int sum=x+y;
    for(int i=2; i*i<=sum; i++)
        if(sum%i==0) return 0;
    return 1;
}
void dfs(int x)
{
    if(x==n+1)
    {
        if(check(a[n],a[1]))
            ans++;
        return;
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]&&check(i,a[x-1]))
        {
            vis[i]=1;
            a[x]=i;
            dfs(x+1);
            vis[i]=0;
        }
    }
}
void solve()
{
    cin>>n;
    dfs(1);
    cout<<ans<<endl;
}

最优装载问题

#include <bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;
const int N =7e3+5;
const int inf=1e18;
const int mod=1e9+7;
const double eps=1e-8;
int n,c,a[N],sum,bestcw,b[N],ans[N];
void dfs(int x,int cw)
{
    if(x==n+1)
    {
        if(cw>bestcw)
        {
            for(int i=1;i<=n;i++) ans[i]=b[i];
            bestcw=cw;
        }
        return;
    }
    sum-=a[x];
    if(cw+a[x]<=c)  //约束函数
    {
        b[x]=1;
        cw+=a[x];
        dfs(x+1,cw);
        cw-=a[x];
    }
    if(cw+sum>bestcw) //限界函数
    {
        b[x]=0;
        dfs(x+1,cw);
    }
    sum+=a[x];
}
signed main()
{
    cin>>n>>c;
    for(int i=1;i<=n;i++)
        cin>>a[i],sum+=a[i];
    dfs(1,0);
    cout<<bestcw<<endl;
    return 0;
}
/*
4 34
21 10 5 2
*/

回溯算法 实现01背包:

#include <bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;
const int N =7e3+5;
const int inf=1e18;
const int mod=1e9+7;
const double eps=1e-8;
int n,c,cw,cv,bestv;
struct node
{
    int w,v;
    double d;
}a[N];
bool cmp(node a,node b)
{
    return a.d>b.d;
}
double check(int x)
{
    int tmp=c-cw;
    double v=cv;
    while(x<=n&&a[x].w<=tmp)
        tmp-=a[x].w,v+=a[x].v,x++;
    if(x<=n) v+=1.0*tmp*a[x].d;
    return v;
}
void dfs(int x)
{
    if(x==n+1)
    {
        bestv=cv;return;
    }
    if(cw+a[x].w<=c)
    {
        cw+=a[x].w;cv+=a[x].v;
        dfs(x+1);
        cw-=a[x].w;cv-=a[x].v;
    }
    if(check(x+1)>bestv)
        dfs(x+1);
}
signed main()
{
    cin>>c>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].w>>a[i].v;
        a[i].d=a[i].v*1.0/a[i].w;
    }
    cout<<1<<endl;
    sort(a+1,a+n+1,cmp);
    dfs(1);
    cout<<bestv<<endl;
    return 0;
}
/*
50 3
10 60
30 120
20 100
*/

n皇后问题

法一:

int n,m,a[105][105],tag[105][3],ans;
void dfs(int x)
{
    if(x==n+1)
    {
        ans++;return;
    }
    for(int i=1;i<=n;i++)
    {
        if(!tag[i][0]&&!tag[i+x][1]&&!tag[30+i-x][2])
        {
            tag[i][0]=1;
            tag[i+x][1]=1;
            tag[30+i-x][2]=1;

            dfs(x+1);
            tag[i][0]=0;
            tag[i+x][1]=0;
            tag[30+i-x][2]=0;
        }
    }
}

法二:

#include <bits/stdc++.h>
#define int long long
#define lowbit(x) ((x)&(-x))
#define endl '\n'
#define lowbit(x) (x&(-x))
using namespace std;
const int N = 7e6+100;
const int mod = 998244353;
int n,m,a[105][105],p[N],ans;
int check(int x)
{
    for(int i=1;i<x;i++)
        if(abs(x-i)==abs(p[x]-p[i])||(p[i]==p[x]))
           return 0;
    return 1;
}
void dfs(int x)
{
    if(x==n+1)
    {
        ans++;
        for(int i=1;i<=n;i++)
            cout<<p[i]<<" ";
        cout<<endl;
        return;
    }
    for(int i=1;i<=n;i++)
    {
        p[x]=i;
        if(check(x))
            dfs(x+1);
    }
}
signed main()
{
    //ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    dfs(1);
    if(ans)
        cout<<ans<<endl;
    else
        cout<<"No Solution!"<<endl;
    return 0;
}

图的m着色问题

解空间的大小为m^n种

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值