码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1149 PIGS

时间:2016-12-31 11:29:21      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:cep   contains   miss   inline   eve   plm   blog   return   0ms   

PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20582   Accepted: 9389

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

题意:

有m个猪圈,n个人,每个人可以打开一些猪圈,从这些猪圈里领猪,当前开着的猪圈中的猪可以随意走动到开着的猪圈中,每个客人走之后猪圈会再次关上,每个客人有期望数量,问最多可以领走多少小猪^(* ̄(oo) ̄)^

分析:

“这水题写什么写”---来自YSQ的嘲讽TAT...

确实没什么好写的...

不过有一点思想很重要就是可以用+∞的边表示流量传递...

对于每个猪圈我们从S向每个猪圈连一条容量为猪数量的边,从每个顾客到T连一条容量为期望领养数量的边,对于每一个客人,如果这个客人是第一次打开这个猪圈的客人,那么猪圈向这个客人连+∞的边,否则上一个打开这个猪圈的客人向当前客人连一条+∞的边...

代码:

技术分享
 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 //by NeighThorn
 6 #define inf 0x3f3f3f3f
 7 using namespace std;
 8 //大鹏一日同风起,扶摇直上九万里
 9 
10 const int maxn=2000+5,maxm=800000+5;
11 
12 int n,m,S,T,cnt,hd[maxn],to[maxm],fl[maxm],nxt[maxm],pre[maxn],pos[maxn],num[maxn];
13 
14 inline void add(int s,int x,int y){
15     fl[cnt]=s;to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++;
16     fl[cnt]=0;to[cnt]=x;nxt[cnt]=hd[y];hd[y]=cnt++;
17 }
18 
19 inline bool bfs(void){
20     memset(pos,-1,sizeof(pos));
21     int head=0,tail=0,q[maxn];
22     q[0]=S;pos[S]=0;
23     while(head<=tail){
24         int top=q[head++];
25         for(int i=hd[top];i!=-1;i=nxt[i])
26             if(pos[to[i]]==-1&&fl[i])
27                 pos[to[i]]=pos[top]+1,q[++tail]=to[i];
28     }
29     return pos[T]!=-1;
30 }
31 
32 inline int find(int v,int f){
33     if(v==T)
34         return f;
35     int res=0,t;
36     for(int i=hd[v];i!=-1&&f>res;i=nxt[i])
37         if(pos[to[i]]==pos[v]+1&&fl[i])
38             t=find(to[i],min(fl[i],f-res)),res+=t,fl[i]-=t,fl[i^1]+=t;
39     if(!res)
40         pos[v]=-1;
41     return res;
42 }
43 
44 inline int dinic(void){
45     int res=0,t;
46     while(bfs())
47         while(t=find(S,inf))
48             res+=t;
49     return res;
50 }
51 
52 signed main(void){
53     scanf("%d%d",&m,&n);cnt=0;
54     S=0,T=n+m+1;memset(hd,-1,sizeof(hd));
55     for(int i=1;i<=m;i++)
56         scanf("%d",&num[i]),pre[i]=i,add(num[i],S,i);
57     for(int i=1;i<=n;i++){
58         int x;scanf("%d",&x);
59         for(int j=1,y;j<=x;j++)
60             scanf("%d",&y),add(inf,pre[y],i+m),pre[y]=i+m;
61         scanf("%d",&x);add(x,i+m,T);
62     }
63     printf("%d\n",dinic());
64     return 0;
65 }//Cap ou pas cap. Cap;
View Code

By NeighThorn

POJ 1149 PIGS

标签:cep   contains   miss   inline   eve   plm   blog   return   0ms   

原文地址:http://www.cnblogs.com/neighthorn/p/6238899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!