Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我能用的办法都用了,DFS,背包,动态规划,几乎每一种都能过BBS的测试数据,但是就是TLE谁帮我看看.我的代码很简洁: #include <stdarg.h> #include<iostream> #include<vector> #include <algorithm> #include <functional> using namespace std; vector<int> cfsz; vector<bool> used; bool dfs(int left,int n) { if (left==0) { return true; } if (left<0||n<0) { return false; } if (dfs(left-cfsz[n],n-1)==true) { return true; } return dfs(left,n-1); } int main() { int sz[6]; int cscs=1; memset(sz,1,sizeof(sz)); while (!((sz[0]==0)&&(sz[1]==0)&&(sz[2]==0)&&(sz[3]==0)&&(sz[4]==0)&&(sz[5]==0))) { cin>>sz[0]>>sz[1]>>sz[2]>>sz[3]>>sz[4]>>sz[5]; used.clear(); cfsz.clear(); int sum=0; for (int i=0;i<6;i++) { sum+=sz[i]*(i+1); } if (sum%2!=0) { cout<<"Collection #"<< cscs<<":"<<endl<<"Can't be divided."; continue; } else { sum/=2; for (int i=0;i<6;i++) { for (int j=0;j<sz[i];j++) { cfsz.push_back(i+1); used.push_back(false); } } sort(cfsz.begin(),cfsz.end(),greater<int> ()); //存放数组 if (dfs(sum,int(cfsz.size()-1))==true) { cout<<"Collection #"<<cscs<<":"<<endl<<"Can be divided."; } else { cout<<"Collection #"<<cscs<<":"<<endl<<"Can't be divided."; } } cscs+=1; } return 0; } 是不是剪枝力度不够啊,谁给我想想啊. Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator