poj 1149 PIGS(最大流)
来源:互联网 发布:卡iphone7plus在线软件 编辑:程序博客网 时间:2024/05/29 07:18
Language:
PIGS
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 16815 Accepted: 7551
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.
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.
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 33 1 102 1 2 22 1 3 31 2 6
Sample Output
7
题意就是卖猪 有m个猪圈 每个猪圈里面有一些猪
有顾客过来买猪 顾客拥有猪圈的钥匙 可以打开猪圈 然后买该猪圈里面的猪
猪可以在从一个猪圈移到另外一个猪圈
卖猪的想要卖出最多的猪 求做多猪的数量
建图:
设立一个源点和一个汇点
将源点与每个猪圈的第一个顾客之间建边 权值为该猪圈中猪的数量 如果出现重边 权值相加
将每个顾客与汇点之间建边 权值为顾客买猪的数量
对于同一猪圈 上一个顾客与下一个顾客之间建边 权值为INF
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define eps 1e-8#define op operator#define MOD 10009#define MAXN 1100#define INF 30000000#define MEM(a,x) memset(a,x,sizeof a)#define ll __int64using namespace std;struct Dinic{ struct Edge { int from,to,cap,flow; Edge(){} Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){} }; vector<Edge> edges; vector<int> G[MAXN]; bool vis[MAXN]; int d[MAXN]; int cur[MAXN]; int n,m,s,t,maxflow; void init(int n) { this->n=n; for(int i=0;i<=n;i++) G[i].clear(); edges.clear(); } void addedge(int from,int to,int cap) { edges.push_back(Edge(from,to,cap,0)); edges.push_back(Edge(to,from,0,0)); m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool bfs() { MEM(vis,0); MEM(d,-1); queue<int> q; q.push(s); d[s]=maxflow=0; vis[s]=1; while(!q.empty()) { int u=q.front(); q.pop(); int sz=G[u].size(); for(int i=0;i<sz;i++) { Edge e=edges[G[u][i]]; if(!vis[e.to]&&e.cap>e.flow) { d[e.to]=d[u]+1; vis[e.to]=1; q.push(e.to); } } } return vis[t]; } int dfs(int u,int a) { if(u==t||a==0) return a; int sz=G[u].size(); int flow=0,f; for(int &i=cur[u];i<sz;i++) { Edge &e=edges[G[u][i]]; if(d[u]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0) { e.flow+=f; edges[G[u][i]^1].flow-=f; flow+=f; a-=f; if(a==0) break; } } return flow; } int Maxflow(int s,int t) { this->s=s; this->t=t; int flow=0; while(bfs()) { MEM(cur,0); flow+=dfs(s,INF); } return flow; }}Dic;int main(){//freopen("ceshi.txt","r",stdin); int n,m; while(scanf("%d%d",&m,&n)!=EOF) { int c[MAXN];//各个猪圈的容量 int last[MAXN];//各个猪圈上一次打开的人 MEM(last,0); for(int i=1;i<=m;i++) scanf("%d",&c[i]); Dic.init(n+1); for(int i=1;i<=n;i++) { int num; scanf("%d",&num); for(int j=0;j<num;j++) { int x; scanf("%d",&x); if(last[x]==0) { last[x]=i; Dic.addedge(0,i,c[x]); } else {// last[x]=i; Dic.addedge(last[x],i,INF); last[x]=i; } } int y; scanf("%d",&y); Dic.addedge(i,n+1,y); } printf("%d\n",Dic.Maxflow(0,n+1)); } return 0;}
//以上的代码很好的解决重边问题 出现重边 直接建边
0 0
- POJ 1149 PIGS(最大流)
- poj 1149 PIGS(最大流)
- poj 1149 PIGS(最大流)
- 【最大流(EK)】poj 1149 PIGS
- POJ 1149 PIGS(最大流)
- poj 1149 PIGS(最大流)
- POJ 1149 PIGS(最大流)
- POJ 1149 PIGS(最大流)
- POJ 1149 PIGS(最大流-Dinic)
- POJ-1149-PIGS(最大流)
- poj 1149 PIGS(最大流)
- POJ 1149 PIGS(最大流)
- 【POJ 1149】 Pigs 最大流
- [最大流] poj 1149 pigs
- POJ 1149 最大流 PIGS
- POJ 1149 PIGS (最大流)
- POJ 1149 PIGS 最大流
- poj 1149 PIGS 最大流
- nodejs内存控制
- C++处理txt文件
- Find Minimum in Rotated Sorted Array II
- C++11-the new ISO C++ Standard
- 用网络机顶盒板卡做广告机的主机
- poj 1149 PIGS(最大流)
- POJ 1611 解题报告
- ubuntu14下setfacl使用
- POJ 1552 解题报告
- Mac Terminal 命令自动补全
- ray ban sunglasses jcwhm ruykq hhpme
- Jump Game
- windows 7+vs2012+cocos2d-x 3.2环境搭建以及安卓移植成功案例(亲测)
- 使用VS打开已有项目Dialog视图