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

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


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


導航

<2009年8月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

統計

常用鏈接

留言簿

隨筆檔案(4)

文章檔案(3)

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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| 亚洲一区亚洲二区| 久久久久久久999精品视频| 欧美顶级大胆免费视频| 久久午夜精品| 欧美性大战久久久久久久蜜臀| 亚洲一区二区日本| 国产精品99免费看 | 欧美wwwwww| 欧美激情a∨在线视频播放| 欧美激情久久久久久| 91久久国产综合久久| 亚洲激情在线| 欧美 日韩 国产在线| 亚洲人成在线观看一区二区| 99精品视频免费全部在线| 欧美激情第3页| 亚洲伊人伊色伊影伊综合网| 久久精品国产精品亚洲| 在线视频日韩精品| 亚洲一区二区网站| 国产精品嫩草99a| 99精品久久| 久热精品视频在线观看| 1000部国产精品成人观看| 免费在线成人| 老牛嫩草一区二区三区日本| 一区二区三区产品免费精品久久75| 欧美日韩免费在线观看| 亚洲尤物影院| 亚洲图片欧洲图片日韩av| 久久久免费观看视频| 久久综合图片| 久久国产综合精品| 日韩系列欧美系列| 亚洲精品一区二区三区不| 亚洲欧美日韩另类| 在线亚洲激情| 在线播放一区| 欧美一区二区精品在线| 一区二区三区成人| 你懂的亚洲视频| 亚洲激情视频网站| 亚洲国产欧美日韩精品| 国产精品美女久久久久久免费| 久久亚洲视频| 女人色偷偷aa久久天堂| 久久综合综合久久综合| 久久精品中文字幕一区二区三区| 亚洲国产精品va在看黑人| 激情久久久久| 国产一区二区三区免费在线观看 | 一本大道av伊人久久综合| 亚洲黄色天堂| 亚洲精品日韩在线| 亚洲人成网站777色婷婷| 韩日成人在线| 在线国产精品一区| 欧美成人精品福利| 欧美日韩不卡视频| 欧美另类变人与禽xxxxx| 欧美中文字幕在线观看| 欧美成人国产| 麻豆国产精品va在线观看不卡| 亚洲欧美网站| 久久视频在线免费观看| 亚洲欧美第一页| 一本一本久久a久久精品综合妖精| 亚洲国内精品在线| 国产精品一区二区三区久久| 国产精品国产自产拍高清av| 亚洲欧美另类中文字幕| 蜜桃av综合| 国产女优一区| 亚洲国产欧美日韩精品| 午夜精品久久久久久久久久久久久| 久久青草久久| 亚洲综合第一| 欧美日韩精品久久| 国内精品久久久久久久影视蜜臀| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 欧美一区二区三区免费看| 欧美aaa级| 亚洲国产成人av| 欧美xxx成人| 久久精品欧美| 国外精品视频| 免费人成精品欧美精品| 久久久亚洲精品一区二区三区| 国产精品影片在线观看| 欧美亚洲视频| 亚洲欧美激情一区二区| 精品二区视频| 欧美成熟视频| 美女国内精品自产拍在线播放| 亚洲国产二区| 99在线热播精品免费| 欧美婷婷在线| 翔田千里一区二区| 亚洲自拍16p| 亚洲麻豆一区| 欧美国产综合一区二区| 久久女同精品一区二区| 国产一区视频在线看| 亚洲一区二区三区色| 一本大道久久精品懂色aⅴ| 欧美.日韩.国产.一区.二区| 久久视频在线免费观看| 国内成人精品一区| 久久精品免费看| 牛夜精品久久久久久久99黑人| 国产在线高清精品| 久久精品三级| 久久美女性网| 亚洲精品乱码久久久久久黑人 | 欧美超级免费视 在线| 中文欧美日韩| 欧美大片免费| 免费在线国产精品| 国产精品欧美精品| 亚洲作爱视频| 亚洲视频在线观看| 欧美女激情福利| 亚洲国产视频一区| 亚洲国产精品精华液网站| 欧美一区二区三区免费看| 久久精品亚洲热| 国产主播一区二区三区| 午夜宅男欧美| 欧美成在线观看| 亚洲精品一区二区三区福利| 欧美v日韩v国产v| 最新国产成人av网站网址麻豆| 91久久久久久| 国产精品欧美风情| 久久av一区二区| 久久久中精品2020中文| 久久成人精品视频| 免费一级欧美片在线播放| 国产中文一区二区三区| 久久综合久久综合久久| 一本久道久久综合中文字幕| 国产精品美女www爽爽爽| 亚洲国产日韩在线一区模特| 欧美日本高清视频| 亚洲一级在线观看| 男男成人高潮片免费网站| 亚洲欧美日韩国产| 老司机成人网| 亚洲免费伊人电影在线观看av| 亚洲伊人一本大道中文字幕| 国产欧美日韩亚洲精品| 亚洲精品中文字幕在线| 国产日韩欧美在线播放| 国语自产精品视频在线看一大j8| 亚洲国产精品一区二区尤物区| 国产日韩欧美日韩| 亚洲专区一区| 欧美一区二区高清| 亚洲人成啪啪网站| 亚洲欧美国产va在线影院| 老司机aⅴ在线精品导航| 在线视频中文亚洲| 伊人精品在线| 国产精品一区在线观看| 欧美日本一区二区视频在线观看| 亚洲欧美日韩另类| 日韩亚洲欧美一区| 欧美成人午夜免费视在线看片| 午夜精彩国产免费不卡不顿大片| 在线看成人片| 1024日韩| 最新国产拍偷乱拍精品 | 久久国产日韩| 午夜亚洲激情| 性色一区二区三区| 先锋a资源在线看亚洲| 亚洲欧美电影院| 先锋影音网一区二区| 午夜精品久久久久久久99热浪潮| 亚洲午夜精品17c| 亚洲自拍偷拍视频| 亚洲欧美在线另类| 久久精品国产99国产精品澳门| 亚洲欧美日韩综合一区| 久久精品国产亚洲aⅴ| 欧美日韩a区| 国产精品www色诱视频| 国产精品人成在线观看免费| 国产精品日韩精品欧美在线| 国产日韩亚洲欧美综合| 国语自产精品视频在线看一大j8 | 亚洲欧洲一区二区在线播放| 亚洲人成精品久久久久| 亚洲午夜激情网站| 久久久国际精品| 国产精品久久久久aaaa樱花| 国产一区二区无遮挡| 亚洲午夜久久久| 欧美高清视频一区二区三区在线观看 |