poj 1149 PIGS

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20966 Accepted: 9587
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


【分析】
一道最大流建立模型的思路题…额 思路如下↓

  1. 源点向每个鸡舍连边,容量为该鸡圈的熟练。
  2. 每个鸡舍向第一个打开它的顾客连边,容量为正无穷。
  3. 如果两个顾客打开的鸡舍有交集,那么它们之间连边,容量为正无穷。
  4. 每个顾客向汇点连边,容量为买鸡的数量。
  5. 最大流即为最多卖出去的鸡的数量。

嗯…我是反着建图的所以大家凑乎看吧


【代码】

//poj 1149 pigs
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 1e9+7
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
queue <int> q;
const int mxn=100005;
int n,m,cnt,ans,t=10000;
int who[105][1005];
int head[mxn],dis[mxn],num[mxn],much[mxn];
struct node {int to,next,flow;} f[mxn<<2]; 
inline void add(int u,int v,int flow)
{
    f[++cnt].to=v,f[cnt].next=head[u],f[cnt].flow=flow,head[u]=cnt;
}
inline bool bfs()
{
    int i,j,u,v,flow;
    memset(dis,-1,sizeof dis);
    q.push(0);
    dis[0]=0;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(i=head[u];i;i=f[i].next)
        {
            v=f[i].to,flow=f[i].flow;
            if(dis[v]==-1 && flow>0)
              dis[v]=dis[u]+1,q.push(v);
        }
    }
    if(dis[t]>0) return 1;
    return 0;
}
inline int find(int u,int low)
{
    int v,i,j,a=0,sum=0,flow;
    if(u==t) return low;
    for(i=head[u];i;i=f[i].next)
    {
        v=f[i].to,flow=f[i].flow;
        if(flow>0&& dis[v]==dis[u]+1 && (a=find(v,min(low-sum,flow))))
        {
            f[i].flow-=a;
            if(i&1) f[i+1].flow+=a;
            else f[i-1].flow+=a;
            sum+=a;
        }
    }
    if(!sum) dis[u]=-1;
    return sum;
}
inline void init()
{
    int i,j,k1,k2,u,v;
    fo(i,1,n)
      fo(j,i+1,n)
      {
          fo(k1,1,who[i][0])
          {
              bool flag=0;
              fo(k2,1,who[j][0])
                if(who[i][k1]==who[j][k2])
                  {flag=1;break;}
              if(flag) {add(j,i,inf),add(i,j,0);break;}  //人连人 
          }
      }
}
int main()
{
    int i,j,u,v,x,tans;
    scanf("%d%d",&m,&n);  //m个鸡圈,n个人买 
    fo(i,1,m)
    {
        scanf("%d",&x);
        add(i+100,t,x),add(t,i+100,0);
    }
    fo(i,1,n)
    {
        scanf("%d",&who[i][0]);
        fo(j,1,who[i][0])
        {
            scanf("%d",&who[i][j]);
            add(i,who[i][j]+100,inf),add(who[i][j]+100,i,0);  //人连鸡 
        }
        scanf("%d",&x);
        add(0,i,x),add(i,0,0);  //源连人 
    }
    init();
    while(bfs())
      while(tans=find(0,0x7f7f))
        ans+=tans;
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值