POJ_1698_Alice's Chance

#include <iostream>

#include <queue>

#include <climits>

#include <cstring>

using namespace std;

const int MAX_SIZE = 400;

int capacity[MAX_SIZE][MAX_SIZE];

int parent[MAX_SIZE];

bool visit[MAX_SIZE];



bool Edmonds_Karp( int start, int end ){

    queue<int> Q;

    memset( visit, false, sizeof( visit ) );

    visit[start] = true;

    Q.push( start );

    while( !Q.empty() ){

        int temp = Q.front();

        Q.pop();

        for( int i = start; i <= end; ++i ){

            if( capacity[temp][i] && !visit[i] ){

                visit[i] = true;

                parent[i] = temp;

                Q.push( i );

                if( i == end ) return true;

            }

        }

    }

    return false;

}



int Ford_Fulkerson( int start, int end ){

    int max_flow = 0;

    while( true ){

        if( !Edmonds_Karp( start, end ) ) break;

        int flow = INT_MAX;

        int path = end;

        while( path != start ){

            flow = min( flow, capacity[parent[path]][path] );

            path = parent[path];

        }

        path = end;

        while( path != start ){

            capacity[path][parent[path]] += flow;

            capacity[parent[path]][path] -= flow;

            path = parent[path];

        }

        max_flow += flow;

    }

    return max_flow;

}



int main(){

    int t;

    cin>>t;

    while( t-- ){

        int start = 0;

        int end = MAX_SIZE - 1;

        memset( capacity, 0, sizeof( capacity ) );

        int film_num;

        int sum_day = 0;

        cin>>film_num;

        for( int i = 1; i <= film_num; ++i ){

            int flag[8];

            int day, week;

            for( int j = 1; j <= 7; ++j ) cin>>flag[j];

            cin>>day>>week;

            capacity[start][i] = day;

            sum_day += day;

            for( int p = 0; p < week; ++p ){

                for( int q = 1; q <= 7; ++q ){

                    int index = film_num + 7 * p + q;

                    capacity[i][index] = flag[q];

                    if( flag[q] ) capacity[index][end] = 1;

                }

            }

        }

        /*

        for( int i = 0; i <= film_num; ++i ){

            for( int j = 0; j <= 40; ++j ){

                cout<<capacity[i][j]<<" ";

            }

            cout<<endl;

        }

        cout<<endl;*/

        int max_flow = Ford_Fulkerson( start, end );

        if( max_flow == sum_day ) cout<<"Yes"<<endl;

        else cout<<"No"<<endl;

    }

    return 0;

}


你可能感兴趣的:(poj)