• <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, 評論 - 81, 引用 - 0
            數據加載中……

            Pku 1599 Station Balance (DFS)

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

            #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 英雄哪里出來 閱讀(291) 評論(0)  編輯 收藏 引用 所屬分類: ACM

            久久婷婷五月综合色99啪ak| 久久精品国产AV一区二区三区| 亚洲综合精品香蕉久久网| 久久本道综合久久伊人| 亚洲国产精品久久久久| 色综合久久综合网观看| 久久免费高清视频| 久久精品无码一区二区app| 99久久精品免费观看国产| 久久国产影院| 久久久久久久久久久精品尤物| 久久久久久伊人高潮影院| 久久久久亚洲AV无码专区体验| 久久综合噜噜激激的五月天| 97久久综合精品久久久综合| 精品午夜久久福利大片| 国产亚洲美女精品久久久| 一本一道久久a久久精品综合 | 亚洲AV无码久久精品狠狠爱浪潮| 国产69精品久久久久9999APGF| 久久男人Av资源网站无码软件 | 亚洲国产婷婷香蕉久久久久久| 人人狠狠综合88综合久久| 精品久久久中文字幕人妻| 久久91精品久久91综合| 久久亚洲精品无码播放| 性欧美大战久久久久久久久| 久久综合九色综合97_久久久| 日韩欧美亚洲国产精品字幕久久久| 丁香色欲久久久久久综合网| 亚洲精品高清国产一久久| 2020久久精品亚洲热综合一本| 久久天堂AV综合合色蜜桃网 | 99久久精品久久久久久清纯| 99久久精品国产一区二区| 日韩一区二区久久久久久| 97精品依人久久久大香线蕉97 | 国产精品免费看久久久香蕉| 久久亚洲熟女cc98cm| 久久er国产精品免费观看8| 久久精品水蜜桃av综合天堂|