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

            久久精品国产亚洲精品2020| 99久久精品费精品国产一区二区 | 久久国产视屏| 国产精品va久久久久久久| 久久99精品久久久久久| 精品人妻伦九区久久AAA片69| 中文字幕久久精品无码| 久久精品国产亚洲一区二区| 99久久精品国产一区二区三区| 手机看片久久高清国产日韩| 青青青国产成人久久111网站| 日本五月天婷久久网站| 欧洲性大片xxxxx久久久| 国产精品伦理久久久久久| 久久99国产精品久久99| 麻豆精品久久精品色综合| 99热成人精品热久久669| 久久综合香蕉国产蜜臀AV| 久久午夜伦鲁片免费无码| 久久人与动人物a级毛片| 一本色道久久综合亚洲精品| 2021最新久久久视精品爱| 一本综合久久国产二区| 亚洲国产成人乱码精品女人久久久不卡| 91久久九九无码成人网站 | av国内精品久久久久影院| 久久久国产乱子伦精品作者| 久久久亚洲欧洲日产国码二区| 99精品国产综合久久久久五月天| 伊人热热久久原色播放www| 久久综合亚洲色HEZYO社区| 亚洲欧美成人综合久久久| 国产亚洲精午夜久久久久久| 久久中文字幕精品| 精品国产VA久久久久久久冰| 2021国产精品午夜久久| 岛国搬运www久久| 久久福利青草精品资源站免费| 国产A三级久久精品| 7777精品久久久大香线蕉| 亚洲国产成人久久综合碰|