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

尼玛,什么情况,uva上AC了,POJ上一直TLE,虽然我的dfs代码跟主流的DFS搜法有点不同。。。。。。。。。。。。求解释。。

Posted by dppoqfasd788 at 2013-03-17 11:25:56 on Problem 1011
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int num[80],n,vis[80],noexit[80],flag;
bool cmp(int p1,int p2)
{
    return p1>p2;
}
int  dfs(int S,int cur,int maxn)
{
      int i,j,flag,doc[80],len1;
      if(S==0) return flag=1;
      //printf("fuck!\n");
     for(i=0,len1=0;i<n;i++)
          if(!vis[i])
      {
             if(cur+num[i]<=maxn&&!noexit[cur+num[i]])
             {
                 vis[i]=1;
                 flag=dfs(S-num[i],(cur+num[i])%maxn,maxn);
                 if(flag)return flag;
                else{
                     noexit[cur+num[i]]=1;
                     doc[len1++]=cur+num[i];
                }
                  vis[i]=0;
                  if((cur+num[i])%maxn==0||cur==0){
                     for(j=0;j<len1;j++)
                           noexit[doc[j]]=0;
                        return 0;
                  }
             }
      }
      for(j=0;j<len1;j++)
             noexit[doc[j]]=0;
      return 0;
}
int main()
{
     int S,i,j,maxn;
     while(scanf("%d",&n),n)
     {
         memset(vis,0,sizeof(vis));
         for(i=0,S=0,maxn=-10000;i<n;i++)
         {
               scanf("%d",&num[i]);
               S+=num[i];
               if(num[i]>maxn)maxn=num[i];
         }
         sort(num,num+n,cmp);
          for(j=maxn,flag=0;j<=S/2;j++)
             if(S%j==0)
           {
                memset(noexit,0,sizeof(noexit));
               memset(vis,0,sizeof(vis));
               flag=dfs(S,0,j);
               if(flag)break;
            }
            if(!flag)cout<<S<<endl;
          else
            cout<<j<<endl;
     }
    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