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

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 閱讀(422) 評論(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免费人成黑人精品 | 99视频精品在线| 日韩视频中文| 国产麻豆综合| 欧美成人蜜桃| 国产精品白丝黑袜喷水久久久| 亚洲男人av电影| 久久er精品视频| 亚洲开发第一视频在线播放| 一区二区三区四区五区精品| 国产综合色产| 99热免费精品| 合欧美一区二区三区| 亚洲国产精品一区二区久| 国产精品超碰97尤物18| 久久青草福利网站| 欧美精品一区二区视频 | 黄色精品一二区| 亚洲人成人99网站| 国产在线一区二区三区四区| 亚洲精品久久久蜜桃| 国产午夜精品麻豆| 99re6这里只有精品| 激情综合自拍| 亚洲一区免费视频| 亚洲免费福利视频| 欧美在线看片a免费观看| 一区二区三区.www| 久久亚洲图片| 欧美在线免费| 国产精品久久国产愉拍 | 欧美激情bt| 国产视频一区在线| aa国产精品| 日韩视频专区| 久久久久久久波多野高潮日日| 在线性视频日韩欧美| 久久深夜福利| 久久视频在线免费观看| 国产精品麻豆成人av电影艾秋| 亚洲国产日韩一级| 在线精品亚洲一区二区| 欧美中文字幕| 久久都是精品| 国产精品视频网| 中文日韩欧美| 亚洲一区在线免费观看| 欧美国产在线视频| 亚洲高清不卡av| 亚洲国产成人精品久久久国产成人一区| 亚洲欧美视频一区| 香蕉久久夜色精品国产| 国产精品久久久久久久久久久久久久| 亚洲欧洲另类国产综合| 亚洲伦理在线| 欧美精品亚洲精品| 亚洲精品乱码久久久久久日本蜜臀 | 欧美人妖在线观看| 亚洲国产欧美一区二区三区久久 | 午夜精品久久久久久久久久久久久| 欧美激情第3页| 亚洲精品久久久久中文字幕欢迎你| 亚洲激情另类| 欧美伦理91| 一区二区三区国产在线| 亚洲欧美另类久久久精品2019| 欧美经典一区二区| 日韩视频一区二区三区在线播放免费观看 | 久久精品理论片| 激情久久综合| 免费在线观看成人av| 亚洲黄页一区| 亚洲视频在线观看视频| 国产精品一区二区你懂得| 午夜精品久久久99热福利| 久久精品一区二区三区四区 | 国产精品一区亚洲| 欧美中文在线字幕| 亚洲国产日本| 亚洲欧美日本国产专区一区| 国产日韩欧美91| 久久琪琪电影院| 亚洲毛片播放| 久久黄金**| 99国产精品国产精品久久| 国产精品久久久久久亚洲毛片| 午夜精品久久99蜜桃的功能介绍| 鲁大师成人一区二区三区| 亚洲激情成人网| 国产精品美女久久久| 久久午夜影视| 亚洲午夜视频在线| 欧美高清视频免费观看| 亚洲一区二区在线观看视频| 国产在线精品一区二区夜色| 欧美日韩国产精品成人| 欧美一区二区三区免费在线看| 亚洲观看高清完整版在线观看| 亚洲欧美精品一区| 亚洲人永久免费| 国产香蕉久久精品综合网| 欧美精品一区二区视频| 久久九九99视频| 亚洲女女做受ⅹxx高潮| 亚洲国产色一区| 久久免费精品视频| 亚洲综合日韩| 99国产欧美久久久精品| 在线精品亚洲一区二区| 国产伦精品一区二区三区视频黑人| 玖玖国产精品视频| 久久se精品一区二区| 国产精品99久久久久久有的能看 | 久久国产精品第一页| 99re6热只有精品免费观看| 激情成人综合网| 国产九区一区在线| 国产精品久久精品日日| 欧美久久久久久久久| 男女激情久久| 麻豆久久婷婷| 久久久综合精品| 久久精品人人做人人综合 | 欧美激情第9页| 久久在精品线影院精品国产| 欧美伊人久久久久久久久影院| 一区二区三区国产盗摄| 亚洲伦理久久| 日韩视频在线一区| 亚洲卡通欧美制服中文| 亚洲精品资源美女情侣酒店| 亚洲国产精品久久久久婷婷884| 国内精品免费在线观看| 国产有码在线一区二区视频| 国产色综合久久| 黑人巨大精品欧美一区二区| 国产一区二区av| 激情久久中文字幕| 亚洲国产精品电影在线观看| 在线日韩av片| 91久久精品www人人做人人爽 | 国产精品久99| 国产精品久久久久高潮| 国产精品一区二区在线观看网站 | 欧美视频在线观看免费网址| 欧美精品乱人伦久久久久久| 欧美日本一区二区三区 | 欧美一区二区三区在| 亚洲综合国产| 欧美综合国产精品久久丁香| 久久久精品国产免费观看同学| 久久精品欧美日韩| 免费成人黄色av| 欧美日韩一级大片网址| 国产精品视频自拍| 一区二区视频免费完整版观看| 亚洲啪啪91| 亚洲在线成人精品| 久久久精品国产免费观看同学| 欧美大片免费久久精品三p| 日韩午夜在线视频| 午夜视频在线观看一区二区| 久久综合电影| 欧美午夜不卡视频| 伊人成人网在线看| 99爱精品视频| 欧美在线视频二区| 欧美激情va永久在线播放| 一本久久综合| 久久精品国产91精品亚洲| 欧美金8天国| 国产麻豆视频精品| 亚洲精品资源| 久久精品国产成人| 亚洲九九爱视频| 欧美专区第一页| 欧美日韩喷水| 亚洲国产成人在线| 欧美在线一级视频| 亚洲黄色在线| 久久久精品免费视频| 欧美色另类天堂2015| 激情亚洲成人| 亚洲伊人一本大道中文字幕| 欧美~级网站不卡| 香蕉亚洲视频| 欧美午夜精品电影| 亚洲欧洲精品天堂一级| 久久久久久久久久久久久久一区| 亚洲精品国产精品国产自|