zoukankan      html  css  js  c++  java
  • Healthy Holsteins chapter 2.1

      dfs+枚举,最近发现dfs+保存中间过程+dfs前后回复变量 ,简直是太好用了,主要剪枝是:对于已用feeds种类==已知最小种类 先判断 然后直接在dfs里return,剪掉了>与已知种类的dfs

    结果还是蛮高兴的

    Executing...
       Test 1: TEST OK [0.000 secs, 3500 KB]
       Test 2: TEST OK [0.011 secs, 3500 KB]
       Test 3: TEST OK [0.000 secs, 3500 KB]
       Test 4: TEST OK [0.000 secs, 3500 KB]
       Test 5: TEST OK [0.000 secs, 3500 KB]
       Test 6: TEST OK [0.000 secs, 3500 KB]
       Test 7: TEST OK [0.000 secs, 3500 KB]
       Test 8: TEST OK [0.000 secs, 3500 KB]
       Test 9: TEST OK [0.000 secs, 3500 KB]
       Test 10: TEST OK [0.000 secs, 3500 KB]
    
    All tests OK.

    YOUR PROGRAM ('holstein') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

    /*
    
    ID: hubiao cave
    
    PROG: holstein
    
    LANG: C++
    
    */
    
    
    
    
    #include<iostream>
    
    #include<fstream>
    
    #include<string>
    
    using namespace std;
    
    
    int vnum;
    int vneed[27];
    int fnum;
    int feeds[17][27];
    bool used[17];
    int minik=999;
    int tstep;
    
    int usedfeed[17];
    int tempfeed[17];
    
    void dfs(int);
    bool canfit();
    
    int main()
    
    {
    
    
    
    
        ifstream fin("holstein.in");
    
        ofstream fout("holstein.out");
        fin>>vnum;
        for(int i=1;i<=vnum;i++)
        {
            fin>>vneed[i];
        }
    
        fin>>fnum;
        for(int i=1;i<=fnum;i++)
        {
            for(int j=1;j<=vnum;j++)
                fin>>feeds[i][j];
        }
        dfs(1);
        fout<<minik;
        for(int i=1;i<=minik;i++)
            fout<<" "<<usedfeed[i];
        fout<<endl;
    
        return 0;
    
    
    }
    
    void dfs(int n)
    {
        if(tstep==minik)
        {
            if(!canfit())
                return;
            else
            {
                for(int i=1;i<=tstep;i++)
                {
                    if(tempfeed[i]==usedfeed[i])
                        continue;
                    if(tempfeed[i]>usedfeed[i])
                        return;
                    if(tempfeed[i]<usedfeed[i])
                    {
                        for(int j=i;j<=tstep;j++)
                            usedfeed[j]=tempfeed[j];
                        return;
                    }
    
                }
            }
        }
    
        if(n<=fnum)
        {
        for(int i=0;i<=1;i++)
        {
            if(used[n]=i)
            {
                tstep++;
                tempfeed[tstep]=n;
                dfs(n+1);
                tstep--;
                used[n]=0;
            }
            else
             dfs(n+1);
        }
        }
        else
        {
    
            if(canfit())
            {
                if(tstep<minik)
                {
                    minik=tstep;
                    for(int i=1;i<=tstep;i++)
                    {
                        usedfeed[i]=tempfeed[i];
                    }
                }
    
            }
    
        }
    
    }
    
    bool canfit()
    {
        for(int i=1;i<=vnum;i++)
        {
            int t=0;
            for(int j=1;j<=fnum;j++)
            {
                t+=feeds[j][i]*(int)used[j];
            }
    
            if(t<vneed[i])
                return false;
        }
        return true;
    }
  • 相关阅读:
    锁(lock)和闩(latch)
    <线程池-定时任务> ScheduledExecutorService之shutdown引发的RejectedExecutionException问题
    get与post需要注意的几点 (转)
    TCP 的那些事儿(上)(转)
    程序员编程艺术:面试和算法心得-(转 July)
    存根类(stub) 是什么意思?有什么作用?(转)
    A writer of dictionaries,a harmless druge.
    第四次作业--项目选题报告(团队)
    第五次作业--原型设计(结对)
    第三次作业--团队展示(团队)
  • 原文地址:https://www.cnblogs.com/cavehubiao/p/3280117.html
Copyright © 2011-2022 走看看