Tags » BF's

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;

bool visited;
int first=100;

void BFS(data x)
{
    map<data,bool>mp;
    queue<data>Q;
    visited=1;
    Q.push(x);
    while(!Q.empty())
    {
        data u=Q.front();
        Q.pop();
        if(u.a==0) ans=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)
            {
                Q.push(c2b);
                visited=1;
            }
            if(!visited)
            {
                Q.push(c2a);
                visited=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)
            {
                visited=1;
                Q.push(b2a);
            }
            if(!visited)
            {
                visited=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)
            {
                visited=1;
                Q.push(a2c);
            }
            if(!visited)
            {
                visited=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;
} 116 more words
USACO

Flows [Time Line]

If you are not familiar with the subject please read from this sources.

see this TopCoder Sources [12]

and see this post on… 704 more words

Bfs

British Fantasy Award Nominees 2015 (all the squee)

Don’t mind me, I’ll just be giggling madly in the corner here…  So, this morning the ever lovely Stephen Theaker posted the BF Awards shortlist for this year…. 322 more words

Editing

janedwardsblog reblogged this on Jan Edwards and commented:

Alchemy Press and Fox Spirit in the BFAs

[LC] Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
OJ’s undirected graph serialization:
Nodes are labeled uniquely. 251 more words

OJ Problem

Thoughts on BFS/CFS

This syndrome started for me a couple of years back as nightly foot cramps. I dreaded going to bed at night, because I knew my feet (and toes) would just continue to basically curl up until I had somehow managed to fall asleep. 297 more words

Benign Fasciculation Syndrome

Word Ladder

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start toend, such that:

  1. Only one letter can be changed at a time…
  2. 242 more words
BFS

Please collect the notes of BFS from the xerox center tomorrow

Banking In Financial Systems