依據上面的推斷,可以在這些範圍內枚舉
sort(all(w),greater<>());
for(int i=w[0];i<=sum/2;i++){
if(sum%i) continue;
if(dfs(...)){
cout << i << '\n';
break;
}
}
接著進今天的主題:剪枝
在枚舉的時候有以下幾點可以直接略過,以加快dfs的速度
int used[MAXN],w[MAXN],total,maxW;
bool dfs(int W,int complete,int now){
//(1) W為當前配了多重,complete為配完幾組,now為現在遍歷的節點編號
if(W==maxW){ //匹配完一組的時候
complete++; //完成的配對數+1
if(complete==total) return true; //(7) 完成匹配
else{
for(now=0;used[now];now++); //(2) 先用最大的
used[now]=true;
if(dfs(w[now],complete,now+1)) return true; //找到解直接回傳
used[now]=false;
}
}else{
for(int i=now;i<n;i++){ //遍歷每個節點
if(used[i] || (i && !used[i-1] && w[i-1]==w[i]) || W+w[i]>maxW) continue; //(3) (4) (5) 避免不必要的遞迴
used[i]=true;
if(dfs(W+w[i],complete,i+1)) return true; //找到解直接回傳
used[i]=false;
if(W+w[i]==maxW) return false; //(6)
}
}
return false;
}
for迴圈要更新maxW及total:
for(int i=w[0];i<=sum/2;i++){
if(sum%i) continue;
maxW=i,total=sum/i;
if(dfs(0,0,0)){
cout << i << '\n';
return;
}
}
#include <bits/stdc++.h>
#define all(a) (a).begin(),(a).end()
#define mem(a) memset(a,0,sizeof(a))
#define MAXN 100
using namespace std;
int a[MAXN],maxW,n,m,N;
bool used[MAXN];
bool dfs(int w,int cnt,int now){
if(w==maxW){
cnt++;
if(cnt==m) return true;
else{
for(now=0;used[now];now++);
used[now]=true;
if(dfs(a[now],cnt,now+1)) return true;
used[now]=false;
}
}else{
for(int i=now;i<n;i++){
if(used[i] || (a && a[i-1]==a[i] && !used[i-1]) || w+a[i]>maxW) continue;
used[i]=true;
if(dfs(w+a[i],cnt,i+1)) return true;
used[i]=false;
if(w+a[i]==maxW) return false;
}
}
return false;
}
int main(){
cin.sync_with_stdio(0),cin.tie(0);
cin >> N;
while(N--){
cin >> m >> n;
mem(used);
int sum=0;
for(int i=0;i<n;i++){
cin >> a[i];
sum+=a[i];
}
sort(a,a+n,greater<int>());
if(sum%m){
cout << 0 << '\n';
continue;
}
maxW=sum/m;
if(dfs(0,0,0)) cout << 1 << '\n';
else cout << 0 << '\n';
}
}
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing