poj1149PIGS(最大流+建图)

Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
13110572105      Log Out
Mail:2(1)
Login Log      Archive
Language:
PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16172 Accepted: 7249

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

Source

[Submit]   [Go Back]   [Status]    [Discuss]

Home Page   Go Back  To top


题目链接:

http://poj.org/problem?id=1149

题意:M个猪圈,N个顾客,每个顾客有一些的猪圈的钥匙,只能购买能打开的猪圈里的猪,而且要买一定数量的猪,每个猪圈有已知数量的猪,但是猪圈可以重新打开,将猪的个数,重新分配,但是只能将猪往当前打开状态的猪圈里赶,以达到卖出的猪的数量最多。

 

思路             :还是4部分,源点->猪圈->猪圈->汇点      

Accepted        976K63MSC++

     

能用EK水的,当然用EK水    

此题需要建图

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
#include <queue>
#define init(a) memset(a,0,sizeof(a))
#define PI acos(-1,0)
using namespace std;
const int maxn = 500;
const int maxm = 40000;
#define lson left, m, id<<1
#define rson m+1, right, id<<1|1
#define min(a,b) (a>b)?b:a
#define max(a,b) (a>b)?a:b
#define MAX INT_MAX    //忽视这些头文件即可

int c[1000][1000];
int re[1000];
int f[1000][1000];
int p[1000];
int n,m;

void initt()//初始化操作
{
    int i,j;
    for(i=0;i<=m+1;i++)
    {
        for(j=0;j<=m+1;j++)
        {
            c[i][j]=f[i][j]=0;
        }
        p[i]=0;
    }
}

void EK(int s,int t)//此题用EK算法水过
{
    queue<int >q;
    while(!q.empty())
        q.pop();
    int sum=0;
    while(1)//放入一个死循环中
    {
        memset(re,0,sizeof(re));
        q.push(s);
        re[s]=MAX;
        p[s]=-1;
        while(!q.empty())//BFS找到增广路(增广链)
        {
            int u=q.front();
            q.pop();
            for(int i=1;i<=m+1;i++)//找到新节点i
            {
                if(!re[i]&&f[u][i]<c[u][i])
                {
                    q.push(i);
                    p[i]=u;//记录i的父亲并且加入队列
                    re[i]=min(re[u],c[u][i]-f[u][i]);//找到s-t的最小残余流量
                }
            }
        }
        if(re[t]==0) break;//找不到,当前已经是最大流
        for(int st=t; st!=s ;st=p[st])//从汇点往回走
        {
            f[p[st]][st]+=re[t];//更新正向流量
            f[st][p[st]]-=re[t];//更新反向流量
        }
        sum+=re[t];//更新从s流出的总流量
    }
    printf("%d\n",sum);//打印出总的流量
}

int main()
{
    int pig[1001];
    int a,b,w;
    int i,j;
    while(~scanf("%d %d",&n,&m))
    {
        initt();
        for(i=1;i<=n;i++)
            scanf("%d",&pig[i]);
        for(i=1;i<=m;i++)
        {
            scanf("%d",&a);
            while(a--)
            {
                scanf("%d",&b);
                if(!p[b])//判断当前猪圈是否打开过
                {
                    c[p[b]][i]+=pig[b];
                    p[b]=i;
                }
                else
                {
                    c[p[b]][i]=MAX;//可以从其他猪圈流向本猪圈,流量可能为无限大
                    p[b]=i;
                }
            }
            scanf("%d",&w);
            c[i][m+1]+=w;
        }
        EK(0,m+1);
    }
    return 0;
}


 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值