POJ 1149 PIGS

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions:22402 Accepted: 10243

Description

Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come to the farm one after another. Each of them has keys to some pig-houses and wants to buy a certain number of pigs. 
All data concerning customers planning to visit the farm on that particular day are available to Mirko early in the morning so that he can make a sales-plan in order to maximize the number of pigs sold. 
More precisely, the procedure is as following: the customer arrives, opens all pig-houses to which he has the key, Mirko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remaining pigs across the unlocked pig-houses. 
An unlimited number of pigs can be placed in every pig-house. 
Write a program that will find the maximum number of pigs that he can sell on that day.

Input

The first line of input contains two integers M and N, 1 <= M <= 1000, 1 <= N <= 100, number of pighouses and number of customers. Pig houses are numbered from 1 to M and customers are numbered from 1 to N. 
The next line contains M integeres, for each pig-house initial number of pigs. The number of pigs in each pig-house is greater or equal to 0 and less or equal to 1000. 
The next N lines contains records about the customers in the following form ( record about the i-th customer is written in the (i+2)-th line): 
A K1 K2 ... KA B It means that this customer has key to the pig-houses marked with the numbers K1, K2, ..., KA (sorted nondecreasingly ) and that he wants to buy B pigs. Numbers A and B can be equal to 0.

Output

The first and only line of the output should contain the number of sold pigs.

Sample Input

3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;



const int MAX_D=100+5;
const int MAX_B=10000+5;
const int INF=1e9;
int v[105][105];
int vis[1000+5];
int w[1000+5];
int tot;

int head[MAX_D];
int iter[MAX_D];
int level[MAX_D];
struct node{
    int to;
    int cap;
    int rev;
    int next;
};
node edge[2*MAX_B];


void add_edge(int a,int b,int c){

    edge[tot].to=b;
    edge[tot].cap=c;
    edge[tot].rev=tot+1;
    edge[tot].next=head[a];
    head[a]=tot++;

    edge[tot].to=a;
    edge[tot].cap=0;
    edge[tot].rev=tot-1;
    edge[tot].next=head[b];
    head[b]=tot++;

}


void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        for(int i=head[v];i!=0;i=edge[i].next){
            node &e=edge[i];
            if(e.cap>0&&level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f){
    if(v==t)return f;
    for(int &i=iter[v];i!=0;i=edge[i].next){
        node &e = edge[i];
        if(e.cap>0&&level[v]<level[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                edge[e.rev].cap+=d;
                return d;
            }
        }
    }

    return 0;
}

int max_flow(int s,int t){
    int flow=0;
    while(1){
        bfs(s);
        if(level[t]<0)return flow;
        for(int i=0;i<MAX_D;i++){
            iter[i]=head[i];
        }
        int f;
        while((f=dfs(s,t,INF))>0){
            flow+=f;
        }
    }
}


void solve(){
    tot=1;
    memset(head,0,sizeof(head));
    memset(vis,0,sizeof(vis));
    int m,n;
    scanf("%d%d",&m,&n);
    for(int i=1;i<=m;i++){
        scanf("%d",&w[i]);
    }
//    cout<<endl;
//    for(int i=1;i<=m;i++){
//        cout<<w[i]<<" ";
//    }
//    cout<<endl;
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        int y=0;
        for(int j=1;j<=x;j++){
            int z;
            scanf("%d",&z);
            if(vis[z]==0){
                vis[z]=i+2;
                y+=w[z];
            }
            else{
                if(v[vis[z]][i+2]==0){
                    v[vis[z]][i+2]=1;
                    add_edge(vis[z],i+2,INF);
                    //cout<<"add"<<vis[z]<<" "<<i+2<<" "<<INF<<endl;
                }
            }
        }
        if(y>0){
            //cout<<"add"<<1<<" "<<i+2<<" "<<y<<endl;
            add_edge(1,i+2,y);
        }
        int e;
        scanf("%d",&e);
        add_edge(i+2,2,e);
        //cout<<"add"<<i+2<<" "<<2<<" "<<e<<endl;
    }

    printf("%d",max_flow(1,2));



}

int main()
{
    solve();

    return 0;
}

阅读更多
个人分类: 网络流
上一篇POJ 1459 Power Network
下一篇poj 3281 Dining
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭