DFS求助剪枝

P2340 [USACO03FALL] Cow Exhibition G

代码已更改 ```cpp #include<bits/stdc++.h> #define int long long using namespace std; struct data{ int s,f; }cow[405]; int n,ans; bool vis[1005]; void dfs(int s,int f,int c,int i){ if(c>n)return; if(s>=0&&f>=0)ans=(s+f)>ans?(s+f):ans; if(!(cow[i].s<=0&&cow[i].f<=0))dfs(s+cow[i].s,f+cow[i].f,c+1,i+1); if(!(cow[i].s>0&&cow[i].f>0))dfs(s,f,c+1,i+1); } signed main(){ // freopen(".in","r",stdin); // freopen(".out","w",stdout); ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=n;++i){ cin>>cow[i].s; cin>>cow[i].f; } dfs(0,0,0,1); cout<<ans; return 0; } /* 5 -5 7 8 -6 6 -3 2 1 -8 -5 */ ```
by wsdyz2010 @ 2023-09-22 14:30:21


63pts,TLE on 2 7 10 11
by wsdyz2010 @ 2023-09-22 14:31:25


|