POJ 1149 PIGS 網絡流——【論建圖的重要性QAQ】

border="0" width="330" height="86" src="http://music.163.com/outchain/player?type=2&id=400161588&auto=1&height=66">

先附贈原題解

http://pan.baidu.com/s/1gea9eJh

傳到網盤上了喵~

**

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

**


**

Solution

**
好久沒有寫題解了【捂臉】【不是故意的 像我這麼弱寫的題解肯定都是A+B類型的吧……【蒟蒻的本質暴露無疑QAQ】】

來紀念一下把這道題寫出來了……QAQ

以前一直不敢寫……大神的題解都寫了四頁QAQ

今天發現寫起來還是很短的

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn=1005,maxm=maxn*maxn,inf=100000000;
int head[maxn],cnt=1,ans;
struct data{int to,next,w;}e[maxm];
void ins(int u,int v,int w){cnt++;e[cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].w=w;}
void insert(int u,int v,int w){ins(u,v,w);ins(v,u,0);}
int q[maxn],ft,ed,n,m,S,T,dis[maxn],a[maxn],L[maxn*10];
bool it[maxn];
bool bfs()
{
    for(int i=1;i<=T;i++)dis[i]=-1;
    dis[S]=0;q[0]=S;ft=0;ed=1;
    while(ft<ed)
    {
        int x=q[ft++];
        for(int i=head[x];i;i=e[i].next)
            if(e[i].w&&dis[e[i].to]==-1)
            {dis[e[i].to]=dis[x]+1;q[ed++]=e[i].to;}
    }
    return dis[T]!=-1;
}
int dfs(int x,int f)
{
    if(x==T)return f;
    int used=0,w;
    for(int i=head[x];i;i=e[i].next)
        if(e[i].w&&dis[e[i].to]==dis[x]+1)
        {
            w=dfs(e[i].to,min(e[i].w,f-used));
            e[i].w-=w;e[i^1].w+=w;
            used+=w;if(used==f)return f;
        }
    if(!used)dis[x]=-1;
    return used;
}
int main()
{
    scanf("%d%d",&m,&n);
    S=n+1,T=n+2;
    for(int i=1;i<=m;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
    {
        int x,y,res=0;
        scanf("%d",&x);
        while(x--)
        {
            scanf("%d",&y);
            if(!L[y])res+=a[y];
            it[L[y]]=1;
            L[y]=i;
        }
        if(it[0])insert(S,i,res),it[0]=0;
        for(int j=1;j<=n;j++)if(it[j])insert(j,i,inf),it[j]=0;
        scanf("%d",&x);insert(i,T,x);
    }
    while(bfs())
        ans+=dfs(S,inf);
    printf("%d\n",ans);
    return 0;
}


——All depends how much you want to be!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章