Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这道题是搜索吗?我全搜剪枝老是tle

Posted by cpp051300448324 at 2005-04-24 00:31:02 on Problem 2397
#include<iostream>
#include<vector>
#include<fstream>
#include<algorithm>

using namespace std;

vector<int> vec;
bool record[1000];
bool tmp[1000];
int max_;
bool signal;
int limit;

void search(int i,int total,int m){
    if(i==vec.size()){
        if(total!=0)return;
        if(m<max_){
            signal=1;
            max_=m;
            /* cout<<max_<<endl; */
            copy(tmp,tmp+vec.size(),record);
           /*  for(int j=0;j<vec.size();j++){
                cout<<record[j];
            }
            cout<<endl;  */   
        }    
            
        return ;
    }
    tmp[i]=1;
    if(total>limit)return ;
    search(i+1,total+vec[i],total+vec[i]>m?(total+vec[i]):m);
    tmp[i]=0;
    if(total-vec[i]<0)return ;
    search(i+1,total-vec[i],m);
}

int main(){
    //ifstream cin("tmp.txt");
    int count;
    //cin>>count;
    scanf("%d",&count);
    int i,j;
    while(count--){
        int M;
        //cin>>M;
        scanf("%d",&M);
        limit=0;
        for(i=0;i<M;i++){
            int in;
            /* cin>>in; */
            scanf("%d",&in);
            vec.push_back(in);
            limit+=in;
        }
        if(limit%2!=0){
            cout<<"IMPOSSIBLE"<<endl;
            vec.clear();
            continue;
        }
        signal=0;
        limit/=2;    
        max_=1000000;
        search(0,0,0);
        if(signal){
            for(i=0;i<vec.size();i++){
                if(record[i])cout<<"U";
                else cout<<"D";
            }
            cout<<endl;
        }
        else cout<<"IMPOSSIBLE"<<endl; 
        vec.clear(); 
    }
    system("pause");
    return 0;
}              
              
    
        
    

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator