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

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 11:12 Chauncey 閱讀(248) 評論(0)  編輯 收藏 引用


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


導航

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

統計

常用鏈接

留言簿

隨筆檔案(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电影| 亚洲理论在线观看| 欧美精品激情在线| 在线中文字幕一区| 久久精品国产91精品亚洲| 黄页网站一区| 欧美国产亚洲精品久久久8v| 亚洲精品一区二区三区99| 亚洲伊人久久综合| 国产在线一区二区三区四区| 乱码第一页成人| 一本色道久久99精品综合| 亚久久调教视频| 在线看片一区| 欧美日韩一区综合| 欧美一区二区三区四区高清 | 农夫在线精品视频免费观看| 亚洲三级性片| 欧美一区免费| 91久久精品日日躁夜夜躁国产| 欧美日韩国产综合视频在线观看| 亚洲综合国产| 亚洲国产色一区| 久久久不卡网国产精品一区| 亚洲人体影院| 国产一区日韩二区欧美三区| 欧美日韩999| 久久精品一区二区三区不卡牛牛| 亚洲片在线资源| 久久久一区二区三区| 中国成人在线视频| 在线电影国产精品| 国产精品女人毛片| 欧美国产日产韩国视频| 欧美一级二级三级蜜桃| 日韩小视频在线观看专区| 美女视频黄 久久| 欧美一区二区三区免费大片| 亚洲精品免费在线| 狠狠久久亚洲欧美| 国产精品免费网站| 欧美另类高清视频在线| 久久精品麻豆| 亚洲欧美久久| 一本一本a久久| 亚洲欧洲精品成人久久奇米网| 久久视频国产精品免费视频在线| 亚洲一区二区三区精品在线观看| 亚洲激情在线观看视频免费| 国产一区二区三区免费不卡 | 一本久久综合| 亚洲国产日韩在线| 男女激情视频一区| 久久免费高清| 欧美在线高清视频| 香蕉久久夜色精品| 亚洲一区中文| 亚洲一区二区视频在线| 亚洲精品视频在线看| 亚洲国产欧美久久| 亚洲高清免费视频| 在线观看的日韩av| 影视先锋久久| 狠狠色丁香婷婷综合影院 | 亚洲成人原创| 影音先锋亚洲精品| 一区久久精品| 在线日韩电影| 亚洲黄页视频免费观看| 亚洲国产专区| 亚洲精品一级| 一区二区三区视频在线| 中文网丁香综合网| 亚洲综合成人婷婷小说| 亚洲一区激情| 欧美一级专区| 久久久久久穴| 蜜月aⅴ免费一区二区三区| 蜜臀久久99精品久久久画质超高清| 久久伊人一区二区| 免费在线成人| 亚洲清纯自拍| 一区二区三区四区国产精品| 亚洲香蕉在线观看| 欧美伊人久久久久久午夜久久久久| 欧美在线免费观看| 榴莲视频成人在线观看| 欧美激情一区二区三区在线| 欧美少妇一区| 国产亚洲免费的视频看| 一区二区三区在线观看国产| 亚洲国产美女精品久久久久∴| 亚洲精选久久| 午夜一级在线看亚洲| 久久精品国产亚洲高清剧情介绍 | 欧美大学生性色视频| 亚洲激情av在线| 亚洲午夜极品| 久久免费视频在线| 欧美日韩91| 国产欧美日韩亚洲| 亚洲韩日在线| 欧美一区午夜精品| 欧美激情精品久久久久久免费印度| 亚洲娇小video精品| 亚洲欧美综合精品久久成人| 久久嫩草精品久久久久| 欧美日韩小视频| 国外成人网址| 在线亚洲伦理| 另类天堂av| 一区二区三区四区五区在线 | 国产一级揄自揄精品视频| 亚洲激情网站免费观看| 午夜精品视频在线观看一区二区| 玖玖玖国产精品| 在线视频欧美日韩| 久久婷婷丁香| 国产精品一区二区你懂得| 亚洲精品免费电影| 久久国产精品高清| 亚洲美女中文字幕| 久久免费高清| 国产日韩视频| 亚洲自拍另类| 亚洲人成毛片在线播放| 欧美一区在线视频| 欧美日韩国产一区精品一区| 1024亚洲| 久久成人精品视频| 日韩视频在线免费| 麻豆国产精品va在线观看不卡| 国产精品综合网站| 一区二区三区国产在线| 欧美二区在线观看| 欧美在线免费观看视频| 国产精品久久激情| 99成人在线| 亚洲福利视频二区| 久久久夜夜夜| 国内伊人久久久久久网站视频 | 亚洲国产高清高潮精品美女| 欧美一区91| 亚洲小少妇裸体bbw| 欧美日韩在线播放三区四区| 亚洲国产一区二区三区在线播| 久久久久国色av免费观看性色| 亚洲视频在线二区| 欧美午夜精品久久久久久人妖| 亚洲免费av电影| 亚洲国产一区二区三区高清| 欧美成年人视频| 亚洲狠狠丁香婷婷综合久久久| 久久综合激情| 久久99伊人| 国产一区在线看| 久久久综合激的五月天| 欧美一级专区| 国产自产v一区二区三区c| 久久精品国产免费看久久精品| 亚洲欧美在线一区二区| 国产欧美二区| 久久久久久有精品国产| 欧美一区二区在线免费观看| 国产一区视频在线看| 久久久久综合网| 久久久久久久综合| 91久久夜色精品国产网站| 欧美激情一区二区在线| 欧美激情精品久久久久久蜜臀 | 亚洲欧美国产日韩中文字幕| 国产精品一区二区三区久久| 羞羞视频在线观看欧美| 欧美伊人影院| 亚洲国产精品精华液2区45| 亚洲第一成人在线| 欧美日韩成人精品| 亚洲男人的天堂在线aⅴ视频| 一区二区三区国产在线| 国产精品综合不卡av| 久久一区视频| 欧美久久99| 欧美一区二区免费| 久久久天天操| 一区二区激情| 亚洲一区二区在线| 在线播放日韩| 亚洲美女性视频| 国产欧美一区二区三区在线老狼| 久久久久久久久综合| 免费永久网站黄欧美| 亚洲无限av看| 久久精品视频一| 亚洲精品在线电影| 亚洲欧美大片| 亚洲黄色在线观看| 亚洲免费在线电影| 亚洲国产一区二区三区a毛片| 一本一道久久综合狠狠老精东影业 |