USACO: Mother’s Milk

Problem Link : http://train.usaco.org/usacoprob2?a=tffkkLavmsp&S=milk3


/*
PROG: milk3
LANG: C++
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

struct data
{
    int a,b,c;
    data()
    {
        ;
    }
    data(int x, int y, int z)
    {
        a=x,b=y,c=z;
    }
};

int A,B,C;

bool ans[30];

bool visited[30][30][30];
int first=100;

void BFS(data x)
{
    map<data,bool>mp;
    queue<data>Q;
    visited[x.a][x.b][x.c]=1;
    Q.push(x);
    while(!Q.empty())
    {
        data u=Q.front();
        Q.pop();
        if(u.a==0) ans[u.c]=1,first=min(first,u.c);
        int bb=B-u.b,aa=A-u.a,cc=C-u.c;
        if(u.c)
        {
            data c2b=u,c2a=u;
            if(bb>=u.c)
            {
                c2b.b+=u.c;
                c2b.c=0;
            }
            else if(bb<u.c)
            {
                c2b.b=B;
                c2b.c-=bb;
            }

            if(aa>=u.c)
            {
                c2a.a+=u.c;
                c2a.c=0;
            }
            else if(aa<u.c)
            {
                c2a.a=A;
                c2a.c-=aa;
            }
            if(!visited[c2b.a][c2b.b][c2b.c])
            {
                Q.push(c2b);
                visited[c2b.a][c2b.b][c2b.c]=1;
            }
            if(!visited[c2a.a][c2a.b][c2a.c])
            {
                Q.push(c2a);
                visited[c2b.a][c2b.b][c2b.c]=1;
            }
        }

        if(u.b)
        {
            data b2a=u,b2c=u;
            if(aa>=u.b)
            {
                b2a.a+=u.b;
                b2a.b=0;
            }
            else
            {
                b2a.a=A;
                b2a.b-=aa;
            }
            if(cc>=u.b)
            {
                b2c.c+=u.b;
                b2c.b=0;
            }
            else
            {
                b2c.c=C;
                b2c.b-=cc;
            }
            if(!visited[b2a.a][b2a.b][b2a.c])
            {
                visited[b2a.a][b2a.b][b2a.c]=1;
                Q.push(b2a);
            }
            if(!visited[b2c.a][b2c.b][b2c.c])
            {
                visited[b2c.a][b2c.b][b2c.c]=1;
                Q.push(b2c);
            }
        }

        if(u.a)
        {
            data a2b=u,a2c=u;
            if(bb>=u.a)
            {
                a2b.b+=u.a;
                a2b.a=0;
            }
            else
            {
                a2b.b=B;
                a2b.a-=bb;
            }
            if(cc>=u.a)
            {
                a2c.c+=u.a;
                a2c.a=0;
            }
            else
            {
                a2c.c=C;
                a2c.c-=cc;
            }
            if(!visited[a2c.a][a2c.b][a2c.c])
            {
                visited[a2c.a][a2c.b][a2c.c]=1;
                Q.push(a2c);
            }
            if(!visited[a2b.a][a2b.b][a2b.c])
            {
                visited[a2b.a][a2b.b][a2b.c]=1;
                Q.push(a2b);
            }
        }

    }
}

int main()
{
    freopen("milk3.in","r",stdin);
    freopen("milk3.out","w",stdout);
    cin>>A>>B>>C;
    BFS(data(0,0,C));

    cout<<first;
    for(int i=first+1; i<=C; i++) if(ans[i]) cout<<" "<<i;
    cout<<endl;
    return 0;
}


Advertisements