青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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 閱讀(420) 評(píng)論(0)  編輯 收藏 引用


只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


導(dǎo)航

<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

統(tǒng)計(jì)

常用鏈接

留言簿

隨筆檔案(4)

文章檔案(3)

搜索

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            红桃av永久久久| 久久动漫亚洲| 榴莲视频成人在线观看| 亚洲一级二级| 亚洲欧美日韩一区在线观看| 亚洲视频在线观看免费| 亚洲美女在线视频| 午夜精品区一区二区三| 久久久欧美精品sm网站| 欧美国产免费| 制服诱惑一区二区| 欧美专区在线观看| 欧美国产在线观看| 国产精品久久午夜| 在线观看精品一区| 亚洲欧美999| 美国成人直播| 中文国产一区| 久久综合福利| 国产精品久久久一区二区| 伊人久久婷婷色综合98网| 一本一道久久综合狠狠老精东影业 | 伊人蜜桃色噜噜激情综合| 日韩视频不卡| 久久亚洲国产成人| 一区二区三区欧美视频| 麻豆精品视频在线| 国产欧美精品在线播放| 日韩午夜电影av| 久久精品日韩一区二区三区| 亚洲免费黄色| 免费亚洲电影| 国产一区二区福利| 亚洲欧美日本精品| 亚洲大胆人体视频| 亚洲深夜福利在线| 欧美激情按摩在线| 在线播放国产一区中文字幕剧情欧美| 国产精品99久久久久久久女警| 久久免费国产精品| 亚洲欧美在线播放| 国产精品美女久久久久aⅴ国产馆| 亚洲日本免费电影| 欧美大片18| 老司机午夜精品视频在线观看| 国产日本亚洲高清| 欧美在线看片| 欧美一级视频| 国产精品国产三级国产aⅴ入口| 国产专区一区| 欧美一级黄色录像| 99re热这里只有精品免费视频| 你懂的网址国产 欧美| 黄色一区二区在线| 久久免费99精品久久久久久| 先锋a资源在线看亚洲| 国产精品国产精品国产专区不蜜| 99在线精品观看| 亚洲乱码国产乱码精品精天堂| 欧美国产视频在线| 亚洲美女黄色| 日韩香蕉视频| 国产精品久久久久久影视 | 亚洲精品少妇网址| 欧美日本一区| 亚洲免费一级电影| 亚洲一区二区三区国产| 国产精品天天看| 久久黄色小说| 久久精品在线免费观看| 在线观看日韩av| 亚洲国产精品一区在线观看不卡| 蜜桃伊人久久| 中日韩午夜理伦电影免费| 一区二区三区四区五区精品视频 | 亚洲精品欧美日韩专区| 欧美美女喷水视频| 欧美一级二级三级蜜桃| 久久国产直播| 一本到高清视频免费精品| 亚洲一区免费观看| 黄色国产精品| 91久久国产综合久久蜜月精品| 欧美精品v日韩精品v韩国精品v | 久久婷婷麻豆| 在线亚洲精品| 欧美一二三区精品| 亚洲精品日韩一| 国产精品99久久久久久白浆小说 | 亚洲综合国产| 久久综合伊人77777蜜臀| av成人免费观看| 欧美一级二区| 一本在线高清不卡dvd| 欧美一级黄色录像| 日韩视频在线一区| 欧美影院在线| 亚洲天堂成人| 久久久久久有精品国产| 亚洲视频视频在线| 亚洲人成高清| 午夜精彩视频在线观看不卡 | 99精品视频免费观看| 亚洲免费中文| 99热在这里有精品免费| 亚洲一区三区视频在线观看| 亚洲欧洲精品一区二区三区 | 欧美精品三级| 久久米奇亚洲| 国产精品视频| 亚洲精品网站在线播放gif| 国产一区二区三区成人欧美日韩在线观看 | 午夜精品区一区二区三| 一本色道久久综合亚洲精品婷婷| 久久国产日本精品| 亚洲欧美一级二级三级| 欧美激情小视频| 免费日韩精品中文字幕视频在线| 国产精品久久久久99| 亚洲国产1区| 亚洲国产精品va在线观看黑人 | 亚洲国产精品一区二区三区| 国产午夜亚洲精品羞羞网站| 99国产精品国产精品久久| 亚洲精品久久久久久一区二区| 久久久精品久久久久| 久久嫩草精品久久久久| 国产日韩亚洲欧美| 亚洲欧美激情在线视频| 午夜精品视频一区| 国产精品免费看| 亚洲综合色在线| 久久se精品一区精品二区| 国产精品一区二区欧美| 午夜欧美大片免费观看| 久久国产视频网| 国精产品99永久一区一区| 久久国产一二区| 美乳少妇欧美精品| 悠悠资源网亚洲青| 牛牛国产精品| 亚洲日本视频| 亚洲专区在线视频| 国产欧美日韩精品一区| 久久国产精品久久国产精品| 久久色在线观看| 亚洲欧洲精品一区二区三区| 欧美激情一级片一区二区| 99精品免费网| 久久成年人视频| 亚洲国产精品美女| 欧美日韩精品在线| 亚洲亚洲精品在线观看| 久久久久久穴| 亚洲欧洲精品成人久久奇米网| 欧美久久婷婷综合色| 欧美成人国产va精品日本一级| 亚洲靠逼com| 性色av一区二区怡红| 国产日韩免费| 蜜臀久久99精品久久久久久9| 亚洲经典自拍| 欧美一区二区日韩| 在线观看成人小视频| 欧美精品在线一区| 午夜精品久久久久久久99热浪潮| 久久精品噜噜噜成人av农村| 亚洲成在人线av| 欧美涩涩网站| 久久动漫亚洲| 日韩一区二区久久| 久久久夜精品| 亚洲先锋成人| 亚洲国产美女久久久久 | 91久久精品一区二区别| 欧美日韩在线直播| 久久久久久免费| 一本久久a久久免费精品不卡| 久久精品国产欧美激情| 日韩一级不卡| 伊人成年综合电影网| 欧美三级日韩三级国产三级| 欧美伊人久久久久久午夜久久久久| 欧美国产免费| 久久久久久9| 亚洲女与黑人做爰| 亚洲乱码国产乱码精品精| 国产午夜一区二区三区| 欧美三区在线| 欧美激情一区二区三区成人| 午夜日韩电影| 亚洲一区二区视频| 99视频精品| 亚洲黄色三级| 免费欧美网站| 美女国产一区| 久久夜色精品国产欧美乱| 欧美一级久久久久久久大片| 999亚洲国产精| 亚洲精品专区|