poj 1149 PIGS(最大流)
2016-06-02 16:42
603 查看
PIGS
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
Sample Output
具体可见:点击打开链接
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 1100
#define M 110
#define INF 0x3f3f3f3f
struct Edge
{
int to,next,cap;
}edge[3*(N*M+2*M)];
int cnt;
int pig_num
,vis
;
int ma[M],head
,d
;
void init()
{
memset(vis,0,sizeof(vis));
memset(ma,0,sizeof(ma));
memset(head,-1,sizeof(head));
cnt=0;
}
void addedge(int from,int to,int cap)
{
edge[cnt].to=to;edge[cnt].cap=cap;
edge[cnt].next=head[from];head[from]=cnt++;
edge[cnt].to=from;edge[cnt].cap=0;
edge[cnt].next=head[to];head[to]=cnt++;
}
int bfs(int s,int t)
{
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==-1&&edge[i].cap>0)
{
d[v]=d[u]+1;
q.push(v);
}
}
}
return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
if(s==t||f==0) return f;
int flow=0;
for(int i=head[s];i!=-1&&flow<f;i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==d[s]+1&&edge[i].cap>0)
{
int x=min(f-flow,edge[i].cap);
x=dfs(v,t,x);
flow+=x;
edge[i].cap-=x;
edge[i^1].cap+=x;
if(f==0) break;
}
}
if(!flow) d[s]=-2;
return flow;
}
int Dinic(int s,int t)///起点s终点t
{
int flow=0,f;
while(bfs(s,t))
{
while(f=dfs(s,t,INF))
flow+=f;
}
return flow;
}
int main()
{
int n,m,t,num;
while(~scanf("%d %d",&n,&m))
{
int S=0,T=m+1;
init();
for(int i=1;i<=n;i++)
scanf("%d",&pig_num[i]);
for(int i=1;i<=m;i++)
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&num);
if(!vis[num])
{
vis[num]=i;
ma[i]+=pig_num[num];
}
else addedge(vis[num],i,INF);
}
scanf("%d",&t);
addedge(i,T,t);
}
for(int i=1;i<=m;i++)
if(ma[i])
addedge(S,i,ma[i]);
int ans=Dinic(S,T);
printf("%d\n",ans);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 19617 | Accepted: 8973 |
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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 1100
#define M 110
#define INF 0x3f3f3f3f
struct Edge
{
int to,next,cap;
}edge[3*(N*M+2*M)];
int cnt;
int pig_num
,vis
;
int ma[M],head
,d
;
void init()
{
memset(vis,0,sizeof(vis));
memset(ma,0,sizeof(ma));
memset(head,-1,sizeof(head));
cnt=0;
}
void addedge(int from,int to,int cap)
{
edge[cnt].to=to;edge[cnt].cap=cap;
edge[cnt].next=head[from];head[from]=cnt++;
edge[cnt].to=from;edge[cnt].cap=0;
edge[cnt].next=head[to];head[to]=cnt++;
}
int bfs(int s,int t)
{
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==-1&&edge[i].cap>0)
{
d[v]=d[u]+1;
q.push(v);
}
}
}
return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
if(s==t||f==0) return f;
int flow=0;
for(int i=head[s];i!=-1&&flow<f;i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==d[s]+1&&edge[i].cap>0)
{
int x=min(f-flow,edge[i].cap);
x=dfs(v,t,x);
flow+=x;
edge[i].cap-=x;
edge[i^1].cap+=x;
if(f==0) break;
}
}
if(!flow) d[s]=-2;
return flow;
}
int Dinic(int s,int t)///起点s终点t
{
int flow=0,f;
while(bfs(s,t))
{
while(f=dfs(s,t,INF))
flow+=f;
}
return flow;
}
int main()
{
int n,m,t,num;
while(~scanf("%d %d",&n,&m))
{
int S=0,T=m+1;
init();
for(int i=1;i<=n;i++)
scanf("%d",&pig_num[i]);
for(int i=1;i<=m;i++)
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&num);
if(!vis[num])
{
vis[num]=i;
ma[i]+=pig_num[num];
}
else addedge(vis[num],i,INF);
}
scanf("%d",&t);
addedge(i,T,t);
}
for(int i=1;i<=m;i++)
if(ma[i])
addedge(S,i,ma[i]);
int ans=Dinic(S,T);
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- Android之布局优化include、merge 、ViewStub
- java-对象的理解-对象在内存的存储
- iOS tableViewscell的动画效果
- 知识总结系列1
- 电器组装行业ERP解决方案
- 获取<img src />正则表达式
- Dom4j中String 、Document 、文件类型的xml之间的相互转换
- “服务器推”技术之使用HTTP长轮询的Comet
- ProgressDialog
- 常见的PKMS问题分析
- 通过蓝牙sniffer抓BLE数据
- 乐学成语实现之二显示主界面
- Phinecos(洞庭散人) 专注于开源技术的研究与应用 TinyXML:一个优秀的C++ XML解析器
- php 打印小票模板
- 解析漏洞总结
- 新博客 lizhaowei.me
- postgresql之distinct用法
- 基本布局(固定布局)
- Active Record 基础
- HTML Button.onclick 事件汇总