poj 1149 最大流
题目链接:http://poj.org/problem?id=1149
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector> #define maxn 1050
#define maxe 200000
using namespace std; const int INF = 0x3f3f3f; struct Edge{
int u,v,flow,cap;
int next;
Edge(int u=,int v=,int flow=,int cap=,int next=):
u(u),v(v),flow(flow),cap(cap),next(next) { }
}; struct Dinic{
int s,t;
int d[maxn];
int cur[maxn];
bool vis[maxn];
Edge edges[maxe];
int head[maxn],cnt; void init(){
memset(head,-,sizeof(head));
cnt = ;
} void addedge(int u,int v,int cap){
edges[cnt] = Edge(u,v,,cap,head[u]);
head[u] = cnt++;
edges[cnt] = Edge(v,u,,,head[v]);
head[v] = cnt++;
} bool bfs(){
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(s);
vis[s] = true;
d[s] = ;
while(!Q.empty()){
int u = Q.front(); Q.pop();
for(int i=head[u];i!=-;i=edges[i].next){
Edge& e = edges[i];
if(!vis[e.v] && e.cap > e.flow){
vis[e.v] = true;
d[e.v] = d[e.u] + ;
Q.push(e.v);
}
}
}
return vis[t];
} int dfs(int u,int res){
if(u == t || res == ) return res; //res 残流大小;
int flow = ,f;
for(int& i=cur[u];i!=-;i=edges[i].next){
Edge& e = edges[i];
if(d[e.v] == d[e.u] + && (f = dfs(e.v,min(res,e.cap-e.flow))) > ){
e.flow += f;
edges[i^].flow -= f;
flow += f;
res -= f;
if(res == ) break;
}
}
return flow;
} int MaxFlow(int s_,int t_){
s = s_; t = t_;
int flow = ;
while(bfs()){
for(int i=s;i<=t;i++) cur[i] = head[i];
flow += dfs(s,INF);
}
return flow;
}
}solver; int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
solver.init();
int N,M;
cin>>M>>N;
int s = , t = N+M+;
for(int i=;i<=M;i++){
int a;
scanf("%d",&a);
solver.addedge(s,i,a);
}
vector<int> G[];
for(int i=;i<=N;i++){
int a;
scanf("%d",&a);
for(int j=;j<=a;j++){
int temp;
scanf("%d",&temp);
solver.addedge(temp,M+i,INF);
for(int k=;k<i;k++){
bool flag = false;
for(int m=;m<G[k].size();m++)
if(G[k][m] == temp){
flag = true;
break;
}
if(flag) solver.addedge(M+k,M+i,INF);
}
G[i].push_back(temp);
}
int b;
scanf("%d",&b);
solver.addedge(M+i,t,b);
}
printf("%d\n",solver.MaxFlow(s,t));
}
poj 1149 最大流的更多相关文章
- 网络流 A - PIGS POJ - 1149 最大流
A - PIGS POJ - 1149 这个题目我开始感觉很难,然后去看了一份题解,写的很好 https://wenku.baidu.com/view/0ad00abec77da26925c5b01c ...
- POJ 1149 PIGS(Dinic最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20738 Accepted: 9481 Description ...
- poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新
题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...
- AC日记——pigs poj 1149
POJ - 1149 思路: 最大流: 代码: #include <cstdio> #include <cstring> #include <iostream> # ...
- poj 3281 最大流+建图
很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很 ...
- 网络流(最大流):POJ 1149 PIGS
PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) ...
- poj 1149 pigs ---- 最大流
题意以及分析:http://ycool.com/post/zhhrrm6#rule3 主要是建图,简化图,然后在套最大流的模板. #include <iostream> #include& ...
- poj 1149 PIGS(最大流经典构图)
题目描述:迈克在一个养猪场工作,养猪场里有M 个猪圈,每个猪圈都上了锁.由于迈克没有钥匙,所以他不能打开任何一个猪圈.要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的 ...
- POJ 1149 - PIGS - [最大流构图]
Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...
随机推荐
- PreferenceFragment界面透明问题
PreferenceFragment界面默认是透明的 而其布局代码框架为 <PreferenceScreen> ... </PreferenceScreen>,背景色及透明度属 ...
- CSRF 攻击的应对之道
转载自imb文库 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在 ...
- VS2010 Cstring to int
今天遇到一个将Cstring转化为int的问题,用atoi(),发现不可以,找了一下原因. 原来因为在VS2015(2010)的版本中是UNICODE ,请使用这个函数 _ttoi() . CStri ...
- 启动scala的方法
1.从官网 http://www.scala-lang.org/download/ 下载scala二进制通用版本以后,在终端命令行添加下载解压包的bin目录到环境变量: export PATH=/Us ...
- Builder 模式
Builder 模式和 AbstractFactory 模式在功能上很相似,因为都是用来创建大的复杂的对象,它们的区别是:Builder 模式强调的是一步步创建对象,并通过相同的创建过程可以获得不同的 ...
- pthread_rwlock_t读写锁函数说明
读写锁 索引: 初始化一个读写锁pthread_rwlock_init 读锁定读写锁 pthread_rwlock_rdlock 非阻塞读锁定 pthread_rwlock_tryrdloc ...
- SGU 188.Factory guard
模拟 code #include <iostream> #include <cstdio> #define LEN 1000 using namespace std; int ...
- MVC埰坑日记 文件权限
public static void DownLoadFile(string FileFullPath) { if (!string.IsNullOrEmpty(FileFullPath) & ...
- 关于考虑浏览器兼容性时间的工具demo
//支持跨浏览器的添加事件. var btn = document.getElementById("btn"); function showMes() { alert(" ...
- dedecms 修改标题长度可以修改数据库
数据表为dede__archives 字段为title 首先要在 a.系统->系统基本参数->其它选项->文章标题长度 b.系统->SQL命令行工具 alter table # ...