回溯法-類型1-子集樹

來源:互聯網
上載者:User

標籤:

0 1 背包問題遞迴求滿足兩個條件的子集樹 #include<stdio.h>#define N 3#define C 15int w[N]={10,8,5};int v[N]={5,4,1};int x[N]={0,0,0}; int cur_weight = 0;int cur_value = 0; int best_value=0;int bestx[N]={0,0,0};void backtrack(int t){    if(t>N-1)    {        if(cur_value>best_value)        {            best_value=cur_value;            int z=0;            for(z=0;z<N;z++)            {                bestx[z]=x[z];            }        }    }    else    {        int z=0;        for(z=0;z<=1;z++)        {            x[t]=z;            if(z==0)            {                backtrack(t+1);            }            else            {                if((cur_weight+w[t])<=C)                {                    cur_value+=v[t];                    cur_weight+=w[t];                    backtrack(t+1);                    cur_value-=v[t];                    cur_weight-=w[t];                }            }         }    }}int main(){    backtrack(0);    printf("%d\n",best_value);    for(int x=0;x<3;x++)    {        printf("%d\n",bestx[x]);    }    return 0;}

 

回溯法-類型1-子集樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.