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

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| 亚洲精品免费网站| 亚洲综合第一页| 欧美人与禽猛交乱配| 国产综合网站| 亚洲综合导航| 亚洲国产精品一区二区www| 久久国产精品久久久| 欧美日韩成人一区二区三区| 一色屋精品视频在线观看网站| 亚洲视频免费| 亚洲福利视频专区| 久久精品国产亚洲5555| 国产精品美女一区二区在线观看| 亚洲黄色av一区| 久久美女性网| 性视频1819p久久| 国产精品福利片| 亚洲视频网在线直播| 亚洲国产精品久久久久秋霞影院 | 91久久精品日日躁夜夜躁欧美| 午夜精品久久久久久久白皮肤| 欧美+亚洲+精品+三区| 一区视频在线| 久久久久久久一区二区三区| 欧美一区二区三区喷汁尤物| 欧美日韩一区三区四区| 一本到12不卡视频在线dvd| 亚洲国产一区二区三区a毛片 | 1024欧美极品| 亚洲女女女同性video| 99国产精品国产精品久久| 欧美日韩国产精品一区| 亚洲精品一区二区三区在线观看| 亚洲第一福利社区| 免费观看亚洲视频大全| 免费高清在线一区| 亚洲人午夜精品免费| 亚洲国产精品激情在线观看| 欧美日韩不卡一区| 午夜日韩在线| 久久精品免费电影| 影音先锋在线一区| 亚洲一区免费| 欧美亚洲视频| 亚洲大片在线| 一区二区欧美国产| 国产精品www.| 久久亚洲国产成人| 免费毛片一区二区三区久久久| 亚洲美女福利视频网站| 亚洲久久视频| 国模精品一区二区三区| 欧美激情视频一区二区三区在线播放 | 欧美午夜在线视频| 亚洲欧美一区二区原创| 先锋资源久久| 亚洲日本va午夜在线电影| 久久久久www| 欧美一区二区三区视频在线| 91久久国产精品91久久性色| 亚洲综合首页| 夜夜嗨av一区二区三区免费区| 久久精品国产精品亚洲精品| 亚洲伊人网站| 欧美日韩免费高清| 欧美激情小视频| 激情欧美一区二区| 亚洲天天影视| 99热免费精品| 久久夜精品va视频免费观看| 久久精品日韩一区二区三区| 欧美视频免费| 亚洲精品一区中文| 亚洲娇小video精品| 久久超碰97中文字幕| 欧美一区二区三区四区在线| 国产精品每日更新在线播放网址| 亚洲免费精品| 一区二区三区av| 欧美激情视频一区二区三区在线播放| 老司机免费视频一区二区| 国产一区自拍视频| 欧美一区二区三区四区在线 | 亚洲国产精品第一区二区三区| 激情视频一区二区| 久久久久国产成人精品亚洲午夜| 久久久精彩视频| 国产一级久久| 久久精品伊人| 欧美成人综合在线| 亚洲精品视频免费观看| 欧美精品在线一区| 99在线视频精品| 亚洲欧美国产毛片在线| 国产精品一区二区三区四区| 亚洲一二三区精品| 欧美一区二区精品在线| 国产视频久久网| 久久青草久久| 亚洲国产精品电影在线观看| 在线视频你懂得一区| 国产精品网红福利| 欧美一区深夜视频| 欧美成人首页| 中日韩美女免费视频网站在线观看| 欧美三级视频在线| 午夜欧美精品| 欧美成人综合网站| 亚洲影视在线| 一区二区亚洲精品国产| 欧美大片第1页| 在线视频一区观看| 玖玖玖国产精品| 一个色综合导航| 国产精品久久一区二区三区| 久久国产精品亚洲va麻豆| 欧美激情第三页| 亚洲欧美日韩精品在线| 精品成人国产| 欧美午夜无遮挡| 久久青草久久| 亚洲一区二区伦理| 欧美激情成人在线视频| 午夜精品久久久久久久久久久久久| 国内精品视频在线观看| 欧美精品在线一区| 在线成人h网| 久久国产99| 亚洲免费观看高清完整版在线观看熊| 欧美在线日韩精品| 亚洲日本aⅴ片在线观看香蕉| 国产精品久久久久久久久免费| 久久精品国产96久久久香蕉| 一本色道久久综合狠狠躁的推荐| 美国三级日本三级久久99| 中文有码久久| 亚洲国产岛国毛片在线| 国产精品日本| 欧美日韩福利视频| 久久免费精品日本久久中文字幕| aa成人免费视频| 欧美国产日韩一区二区| 久久激情五月丁香伊人| 亚洲视频免费看| 亚洲精品人人| 极品少妇一区二区三区精品视频| 国产精品福利久久久| 欧美精品免费看| 裸体丰满少妇做受久久99精品| 亚洲在线一区二区| 亚洲免费成人av电影| 亚洲国产一区二区三区在线播 | 国产亚洲精品bv在线观看| 欧美日韩一区二区高清| 欧美大片免费观看| 久久亚洲综合色一区二区三区| 欧美淫片网站| 亚洲欧美日韩国产另类专区| 一区二区欧美精品| 99精品国产高清一区二区| 亚洲激情视频| 亚洲成色999久久网站| 美女视频黄 久久| 免费观看日韩av| 欧美~级网站不卡| 蜜桃av久久久亚洲精品| 久久亚洲高清| 欧美成人自拍视频| 亚洲国产成人久久| 亚洲高清av| 亚洲日本理论电影| 亚洲伦理自拍| 99在线观看免费视频精品观看| 亚洲精品日韩精品| 一区二区三区国产| 亚洲一二三区在线| 性欧美大战久久久久久久免费观看 | 91久久精品国产91久久性色tv | 91久久在线播放| 亚洲欧洲视频在线| 99在线精品观看| 亚洲一级二级在线| 香蕉视频成人在线观看| 久久久久久婷| 欧美成人午夜激情| 亚洲精品五月天| 亚洲一区二区三区中文字幕| 午夜欧美电影在线观看| 久久伊人一区二区| 欧美精品 国产精品| 国产精品高清一区二区三区| 国产欧美日韩在线| 欧美国产第二页| 久久精品亚洲精品|