• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 97, 文章 - 22, 評(píng)論 - 81, 引用 - 0
            數(shù)據(jù)加載中……

            Pku 1599 Station Balance (DFS)

            解題思路:
            枚舉所有物品的子集,然后再枚舉子集的組合,數(shù)據(jù)量很小,不需要剪枝。

            #include <iostream>
            #include 
            <cmath>
            using namespace std;

            struct Stack {
                
            int a[100];
                
            int top;
                
            int num;
            }
            stack, buf[100], rou;
            int top;

            int num[1001];
            double am;
            int c, s;
            int hash[100];
            double Min;
            int u;

            void dfs(int index) {
                
            int i;

                buf[top].top 
            = stack.top;
                
            for(i = 0; i < stack.top;i++){
                    buf[top].a[i] 
            = stack.a[i];
                }

                top 
            ++;

                
            for(i = index; i < s; i++{
                    
            if(hash[i] == 1)
                        
            continue;
                    
            if(stack.top == 2)
                        
            continue;
                    stack.a[ stack.top
            ++ ] = i;
                    hash[i] 
            = 1;
                    dfs(i
            +1);
                    hash[i] 
            = 0;
                    stack.top 
            --;
                }

            }


            void DFS(int index, int sum) {

                
            int i, j;

                
            if(stack.top > c)
                    
            return ;

                
            if(sum == (1<<s) - 1{

                    
            double sz = 0, sl;
                    
            for(i = 0; i < stack.top; i++{
                        sl 
            = 0;
                        
            for(j = 0; j < buf[ stack.a[i] ].top; j++{
                            
            int y = buf[ stack.a[i] ].a[j];
                            sl 
            += num[y];
                        }

                        sz 
            += fabs(am - sl);
                    }
                

                    
            for(i = stack.top; i < c; i++{
                        sz 
            += fabs(am);
                    }


                    
            if(sz < Min) {
                        Min 
            = sz;
                        rou.top 
            = 0;

                        
            for(i = 0; i < stack.top; i++{
                            rou.a [rou.top 
            ++= stack.a[i];
                        }

                        
                        
            for(i = stack.top; i < c; i++{
                            rou.a[ rou.top
            ++ ] = 0;
                        }

                        rou.top 
            = c;
                    }

                    
            return ;
                }


                
            for(i = index; i < top; i++{
                    
            if(hash[i])
                        
            continue;
                    
            if(sum & buf[i].num)
                        
            continue;
                    stack.a[ stack.top 
            ++ ] = i;
                    hash[i] 
            = 1;
                    DFS(i
            +1, (sum|buf[i].num) );
                    hash[i] 
            = 0;
                    stack.top 
            --;
                }

            }

            int main() {

                
            int i, j;
                
            int cas = 1;
                
            while(scanf("%d %d"&c, &s) != EOF) {


                    am 
            = 0;
                    Min 
            = 1000000000.0;
                    
            for(i = 0; i < s; i++{
                        scanf(
            "%d"&num[i]);
                        am 
            += num[i];
                        hash[i] 
            = 0;
                    }

                    am 
            /= c;
                    stack.top 
            = 0;
                    top 
            = 0;
                    dfs(
            0);
                    
            for(i = 0; i < top; i++{
                        buf[i].num 
            = 0;
                        
            for(j = 0; j < buf[i].top; j++{
                            buf[i].num 
            |= (1<<(buf[i].a[j]));
                        }

                    }

                    memset(hash, 
            0sizeof(hash));

                    stack.top 
            = 0;
                    DFS(
            00);

                    printf(
            "Set #%d\n", cas ++);

                    
            int rt = 0;

                    
            for(i = 0; i < rou.top; i++{

                        
            if(rou.a[i]) {
                                printf(
            " %d:", rt ++);
                                
            for(j = 0; j < buf[ rou.a[i] ].top; j++)
                                    printf(
            " %d", num[ buf[ rou.a[i] ].a[j] ]);
                                puts(
            "");
                        }

                    }


                    
            for(i = rt; i < c; i++)
                        printf(
            " %d:\n", i);
                    printf(
            "IMBALANCE %.5lf\n", Min);
                    puts(
            "");
                }

                
            return 0;

            }

            posted on 2009-03-03 10:37 英雄哪里出來(lái) 閱讀(290) 評(píng)論(0)  編輯 收藏 引用 所屬分類: ACM

            精品乱码久久久久久夜夜嗨| 久久午夜伦鲁片免费无码| 亚洲天堂久久精品| 久久国产精品免费一区| 久久夜色撩人精品国产| 老色鬼久久亚洲AV综合| 国产午夜福利精品久久| 久久久久免费精品国产| 亚洲国产精品人久久| 欧美日韩中文字幕久久久不卡 | 国产人久久人人人人爽| 久久97久久97精品免视看| 久久久精品人妻一区二区三区蜜桃| 久久亚洲美女精品国产精品| 精品久久久久久久久久久久久久久 | 亚洲av日韩精品久久久久久a| 狠狠色伊人久久精品综合网 | 久久99国产精品二区不卡| 香蕉久久夜色精品国产2020| 久久99国产精品久久| 麻豆一区二区99久久久久| 久久久久亚洲精品日久生情| 久久久久无码中| 精品熟女少妇av免费久久| 久久精品综合网| 久久精品不卡| 91久久精品视频| 国产成人久久精品一区二区三区| 欧美亚洲色综久久精品国产| 久久久久久亚洲精品成人| 久久精品国产99国产精品导航| 亚洲欧美精品一区久久中文字幕| 亚洲国产精品久久66| 久久精品欧美日韩精品| 亚洲乱码日产精品a级毛片久久| 亚洲欧美一区二区三区久久| 18禁黄久久久AAA片| 69国产成人综合久久精品| 国产日韩久久久精品影院首页| 亚洲va久久久噜噜噜久久男同 | 亚洲伊人久久综合影院|