• <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>

            Google code jam 2008 R1A - Milkshakes

            Problem

            You own a milkshake shop. There are N different flavors that you can prepare, and each flavor can be prepared "malted" or "unmalted". So, you can make 2N different types of milkshakes.

            Each of your customers has a set of milkshake types that they like, and they will be satisfied if you have at least one of those types prepared. At most one of the types a customer likes will be a "malted" flavor.

            You want to make N batches of milkshakes, so that:

            • There is exactly one batch for each flavor of milkshake, and it is either malted or unmalted.
            • For each customer, you make at least one milkshake type that they like.
            • The minimum possible number of batches are malted.

            Find whether it is possible to satisfy all your customers given these constraints, and if it is, what milkshake types you should make.

            If it is possible to satisfy all your customers, there will be only one answer which minimizes the number of malted batches.

            Input

            • One line containing an integer C, the number of test cases in the input file.

            For each test case, there will be:

            • One line containing the integer N, the number of milkshake flavors.
            • One line containing the integer M, the number of customers.
            • M lines, one for each customer, each containing:
              • An integer T >= 1, the number of milkshake types the customer likes, followed by
              • T pairs of integers "X Y", one for each type the customer likes, where X is the milkshake flavor between 1 and N inclusive, and Y is either 0 to indicate unmalted, or 1 to indicated malted. Note that:
                • No pair will occur more than once for a single customer.
                • Each customer will have at least one flavor that they like (T >= 1).
                • Each customer will like at most one malted flavor. (At most one pair for each customer has Y = 1).
              All of these numbers are separated by single spaces.

            Output

            • C lines, one for each test case in the order they occur in the input file, each containing the string "Case #X: " where X is the number of the test case, starting from 1, followed by:
              • The string "IMPOSSIBLE", if the customers' preferences cannot be satisfied; OR
              • N space-separated integers, one for each flavor from 1 to N, which are 0 if the corresponding flavor should be prepared unmalted, and 1 if it should be malted.

            Limits

            Small dataset

            C = 100
            1 <= N <= 10
            1 <= M <= 100

            Large dataset

            C = 5
            1 <= N <= 2000
            1 <= M <= 2000

            The sum of all the T values for the customers in a test case will not exceed 3000.

            Sample


            Input
             

            Output
             
            2
            5
            3
            1 1 1
            2 1 0 2 0
            1 5 0
            1
            2
            1 1 0
            1 1 1
            Case #1: 1 0 0 0 0
            Case #2: IMPOSSIBLE

            In the first case, you must make flavor #1 malted, to satisfy the first customer. Every other flavor can be unmalted. The second customer is satisfied by getting flavor #2 unmalted, and the third customer is satisfied by getting flavor #5 unmalted.

            In the second case, there is only one flavor. One of your customers wants it malted and one wants it unmalted. You cannot satisfy them both.

            Analysis
            On the surface, this problem appears to require solving the classic problem "Satisfiability," the canonical example of an NP-complete problem. The customers represent clauses, the milkshake flavors represent variables, and malted and unmalted flavors represent whether the variable is negated.

            We are not evil enough to have chosen a problem that hard! The restriction that makes this problem easier is that the customers can only like at most one malted flavor (or equivalently, the clauses can only have at most one negated variable.)

            Using the following steps, we can quickly find whether a solution exists, and if so, what the solution is.

            1. Start with every flavor unmalted and consider the customers one by one.
            2. If there is an unsatisfied customer who only likes unmalted flavors, and all those flavors have been made malted, then no solution is possible.
            3. If there is an unsatisfied customer who has one favorite malted flavor, then we must make that flavor malted. We do this, then go back to step 2.
            4. If there are no unsatisfied customers, then we already have a valid solution and can leave the remaining flavors unmalted.

            Notice that whenever we made a flavor malted, we were forced to do so. Therefore, the solution we got must have the minimum possible number of malted flavors.

            With clever data structures, the above algorithm can be implemented to run in linear time.

            More information:

            The Satisfiability problem - Horn clauses


            Source Code
            #include <iostream>

            using namespace std;

            #define Rep(i,n) for (int i(0),_n(n); i<_n; ++i)

            struct Flavor{
                
            int X;
                
            char Y;
            }
            ;

            struct Customer{
                
            int T;
                Flavor
            * F;
                Customer() 
            {
                    F 
            = NULL;
                }

                
            ~Customer() {
                    
            if(NULL!=F) {
                        delete[] F;
                        F 
            = NULL;
                    }

                }

                
            void Init(int t) {
                    T 
            = t;
                    F 
            = new Flavor[T];
                }

                
            void SetFlavor(int i, int X, int Y) {
                    F[i].X 
            = X;
                    F[i].Y 
            = Y;
                }

                
            int GetFlavorX(int i) {
                    
            return F[i].X;
                }

                
            int GetFlavorY(int i) {
                    
            return F[i].Y;
                }

                
            bool IsSatisfied() {
                    
            return T==0;
                }

                
            void Satisfy() {
                    T
            =0;
                    
            if(NULL!=F) {
                        delete[] F;
                        F 
            = NULL;
                    }

                }

                
            bool IsSatisfing(int i, int *f) {
                    
            return f[F[i].X]==F[i].Y;
                }

                
            void SetMalted(int i, int *f) {
                    f[F[i].X] 
            = 1;
                }

            }
            ;

            int main()
            {
                
            int C;
                FILE 
            *fp = fopen("A.out""w");
                scanf(
            "%d"&C);
                Rep(c, C) 
            {
                    
            int N;
                    scanf(
            "%d"&N);
                    
            int* f = new int[N+1];
                    Rep(i ,N
            +1{
                        f[i]
            =0;
                    }

                    
            int M;
                    scanf(
            "%d"&M);
                    Customer
            * customer = new Customer[M];
                    Rep(m, M) 
            {
                        
            int T;
                        scanf(
            "%d"&T);
                        customer[m].Init(T);
                        Rep(t, T) 
            {
                            
            int X, Y;
                            scanf(
            "%d%d"&X,&Y);
                            customer[m].SetFlavor(t,X,Y);
                        }

                    }

                    
            bool findSolution = true;
                    
            int m = 0;
                    
            while(m<M) {
                        
            if(customer[m].IsSatisfied()) {
                            m
            ++;
                            
            continue;
                        }

                        
            bool malted = false;
                        
            int idx;
                        
            bool satisfied = false;
                        Rep(t, customer[m].T) 
            {
                            
            if(customer[m].GetFlavorY(t)==1{
                                malted 
            = true;
                                idx 
            = t;
                            }

                            
            if(customer[m].IsSatisfing(t,f)) {
                                satisfied 
            = true;
                            }

                        }

                        
            if(!satisfied) {
                            
            if(malted) {
                                customer[m].SetMalted(idx,f);
                                customer[m].Satisfy();
                                m
            =0;
                            }
             else {
                                findSolution 
            = false;
                                
            break;
                            }

                        }
             else {
                            m
            ++;
                        }

                    }

                    fprintf(fp,
            "Case #%d: ", c+1);
                    
            if(findSolution) {
                        Rep(i ,N) 
            {
                            fprintf(fp,
            "%d ", f[i+1]);
                        }

                        fprintf(fp,
            "\n");
                    }
             else {
                        fprintf(fp,
            "IMPOSSIBLE\n");
                    }

                    delete[] customer;
                    delete[] f;

                }

                fclose(fp);
            }

            posted on 2009-08-12 21:17 Chauncey 閱讀(408) 評論(0)  編輯 收藏 引用

            導航

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            統計

            常用鏈接

            留言簿

            隨筆檔案(4)

            文章檔案(3)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品美女久久久| 99久久国产亚洲高清观看2024 | 亚洲va中文字幕无码久久| 亚洲欧美精品一区久久中文字幕 | 99久久免费国产精精品| 国产精品熟女福利久久AV| 久久天天躁狠狠躁夜夜2020一| 伊人久久大香线蕉av不变影院| 91精品无码久久久久久五月天| 色婷婷噜噜久久国产精品12p | 久久久久久毛片免费播放| 国产高潮国产高潮久久久91 | 72种姿势欧美久久久久大黄蕉| 久久免费观看视频| 99精品国产在热久久无毒不卡| 亚洲人成无码网站久久99热国产 | 亚洲综合伊人久久大杳蕉| 久久不射电影网| 国产精品久久新婚兰兰| 成人a毛片久久免费播放| 国产V综合V亚洲欧美久久| 精品久久久无码21p发布| 欧美色综合久久久久久| 青青草原综合久久大伊人精品| 亚洲精品无码久久久影院相关影片 | 久久人人爽人人人人片av| 久久亚洲国产精品123区| 国产高潮国产高潮久久久91| 久久精品成人免费看| 精品国产乱码久久久久久郑州公司 | 亚洲综合久久夜AV | 国产三级精品久久| 久久香蕉一级毛片| 久久精品国产亚洲网站| 国产精品久久久久久久久免费| 97久久精品人妻人人搡人人玩| 婷婷五月深深久久精品| 色偷偷久久一区二区三区| 色综合久久久久久久久五月 | 久久精品国产精品亜洲毛片| 久久综合狠狠综合久久激情 |