试了很多种爆搜和剪枝,最后发现还是状压的比较好用

#include <bits/stdc++.h>
using namespace std;
// #define IO
#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define rep(i,s,t) for(int i=s;i<t;i++)
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define DOW(i,s,t) for(int i=s;i>=t;i--)
#define dow(i,s,t) for(int i=s;i>t;i--)
#define clr(a,b) memset(a,b,sizeof(a))
#define debug(x) cout<<#x<<' '<<x<<endl typedef long long ll;
typedef pair<int,int>pii;
inline int lowbit(int x){ return x&(-x); }
template<typename T>
inline void read(T&x){
x=;int f=;char ch=getchar();
while(ch<'' ||ch>''){ if(ch=='-')f=-; ch=getchar(); }
while(ch>='' && ch<=''){ x=x*+ch-''; ch=getchar(); }
x*=f;
}
const int N = ;
ll g[N][N];
ll n,ans; void dfs(int mask,int num,int pre,ll preans){
if(num<<==n){
ans=max(ans,preans);return;
}
if(n--pre+num<n/)return; //表示即使是接下来的所有数都选,也不符合条件(集合不能够选够n/2个元素),就直接返回(最优性剪枝)
for(int i=pre+;i<n;i++){
ll nowans=preans;
rep(j,,n){
if(mask&(<<j))nowans-=g[i][j];
else nowans+=g[i][j];
}
dfs(mask|(<<i),num+,i,nowans);
}
}
int main(){
cin>>n;n<<=;
rep(i,,n) rep(j,,n) read(g[i][j]);
ll nowans=; //现在将第一个点放到另一个集合中
rep(i,,n)nowans+=g[][i];
dfs((<<),,,nowans);
printf("%lld\n",ans);
return ;
}

然后还有一种不同思路的搜索,一开始两个集合都为空,然后往两个集合里填元素,这样会少很多冗余的搜索

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,vis[],mp[][],ans; int s1[],s2[],top1,top2;
void dfs(int pos,ll now){
if(pos>*n){
ans=max(ans,now);
return;
}
if(top1<n){
s1[++top1]=pos;
ll nxt=now;
for(int i=;i<=top2;i++)
nxt+=mp[pos][s2[i]];
dfs(pos+,nxt);
top1--;
}
if(top2<n){
s2[++top2]=pos;
ll nxt=now;
for(int i=;i<=top1;i++)
nxt+=mp[pos][s1[i]];
dfs(pos+,nxt);
top2--;
}
} int main(){
cin>>n;
for(int i=;i<=*n;i++)
for(int j=;j<=*n;j++)
scanf("%d",&mp[i][j]);
ans=;
dfs(,);
cout<<ans<<endl;
}
/*
(28,14)=
*/

优化剪枝搜索——牛客多校第二场F的更多相关文章

  1. 2019牛客多校第二场F Partition problem 暴力+复杂度计算+优化

    Partition problem 暴力+复杂度计算+优化 题意 2n个人分成两组.给出一个矩阵,如果ab两个在同一个阵营,那么就可以得到值\(v_{ab}\)求如何分可以取得最大值 (n<14 ...

  2. 2019年牛客多校第二场 F题Partition problem 爆搜

    题目链接 传送门 题意 总共有\(2n\)个人,任意两个人之间会有一个竞争值\(w_{ij}\),现在要你将其平分成两堆,使得\(\sum\limits_{i=1,i\in\mathbb{A}}^{n ...

  3. 2019牛客多校第二场F Partition problem(暴搜)题解

    题意:把2n个人分成相同两组,分完之后的价值是val(i, j),其中i属于组1, j属于组2,已知val表,n <= 14 思路:直接dfs暴力分组,新加的价值为当前新加的人与不同组所有人的价 ...

  4. 2019牛客多校第二场 A Eddy Walker(概率推公式)

    2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...

  5. 牛客多校第二场A run(基础DP)

    链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言2621 ...

  6. run (牛客多校第二场)计数DP

    链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 题目描述 White Cloud is exercising in the playground ...

  7. 2019牛客多校第二场F-Partition problem(搜索+剪枝)

    Partition problem 题目传送门 解题思路 假设当前两队的对抗值为s,如果把红队中的一个人a分配到白队,s+= a对红队中所有人的对抗值,s-= a对白队中所有人的对抗值.所以我们可以先 ...

  8. 2019 牛客多校第二场 H Second Large Rectangle

    题目链接:https://ac.nowcoder.com/acm/contest/882/H 题目大意 给定一个 n * m 的 01 矩阵,求其中第二大的子矩阵,子矩阵元素必须全部为 1.输出其大小 ...

  9. 2019牛客多校第二场H-Second Large Rectangle

    Second Large Rectangle 题目传送门 解题思路 先求出每个点上的高,再利用单调栈分别求出每个点左右两边第一个高小于自己的位置,从而而得出最后一个大于等于自己的位置,进而求出自己的位 ...

随机推荐

  1. spring security学习总结

    这几天一直在学习spring security的相关知识.逛各大论坛,看相关api与教学视频,获益良多! 简介 Spring Security是为基于Spring的企业应用系统提供声明式的安全访问控制 ...

  2. Magento站点优化方案

    Magento 是一个开源电子商务系统,尤其以扩展性高著称,但是很高的扩展性往往是牺牲了速度为代价的,虽然现在magento为速度提升做了很多工作,但是还是没能达到人们对速度的要求.既然如此还是很自然 ...

  3. Robot Framework:随机数

    脚本 随机数 # 随机生成几位随机数 ${num} set variable 6 ${random} evaluate "".join(random.sample(string.l ...

  4. 【Codeforces Beta Round #88 C】Cycle

    [Link]:http://codeforces.com/problemset/problem/117/C [Description] 问你一张图里面有没有一个三元环,有的话就输出. [Solutio ...

  5. 【Flutter学习】基本组件之基本网格Gradview组件

    一,概述 数据量很大的时用矩阵方式排列比较清晰,此时用网格列表组件,即为GridView组件,可实现多行多列的应用场景. 使用GridView创建网格列表有多种方式: GridView.count 通 ...

  6. go语言中使用正则表达式

    一.代码 package main import ( "fmt" "regexp" ) func main() { text := `Hello 世界!123 ...

  7. PHP导出excel word的代码

    php导出为word原理 一般,有2种方法可以导出doc文档,一种是使用com,并且作为php的一个扩展库安装到服务器上,然后创建一个com,调用它的方法.安装过office的服务器可以调用一个叫wo ...

  8. makefile.new(7117) : error U1087: cannot have : and :: dependents for same target

    makefile.new(7117) : fatal error U1087: cannot have : and :: dependents for same target(2012-05-21 2 ...

  9. 1、postman介绍与安装

    postman介绍 官方介绍:Developers use Postman to build modern software for the API-first world. 个人理解postman是 ...

  10. 拾遗:Docker 基本应用

    https://wiki.gentoo.org/wiki/Docker 检查内核内核选项 exec /usr/share/docker/contrib/check-config.sh 使用 btrfs ...