参考了这个PDF

第一道网络流啊!感动

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 100000000
#define N 500
#define M 1100
using namespace std;
struct adj
{
int u,v,w;
}e[M*M];
int head[N],n,m,S,T,a[M],k,ecnt=,x,flw[N],tmp,lev[N],ans,last[M];
vector <int> buy[M];
queue <int> q;
void add(int u,int v,int w)
{
e[++ecnt].v=v;
e[ecnt].w=w;
e[ecnt].u=head[u];
head[u]=ecnt;
}
int bfs()
{
memset(lev,-,sizeof(lev));
lev[S]=;
q.push(S);
while (!q.empty())
{
int x=q.front();
for (int i=head[x];i;i=e[i].u)
{
int v=e[i].v;
if (lev[v]==- && e[i].w>)
lev[v]=lev[x]+,q.push(v);
}
q.pop();
}
return lev[T]!=-;
}
int dfs(int x,int flw)
{
if (x==T) return flw;
for (int i=head[x];i;i=e[i].u)
{
int v=e[i].v,tmp;
if (lev[v]==lev[x]+ && e[i].w> && (tmp=dfs(v,min(flw,e[i].w)))>)
{
e[i].w-=tmp;
e[i^].w+=tmp;
return tmp;
}
}
return ;
}
int main()
{
scanf("%d%d",&m,&n);
S=n+,T=n+;
for (int i=;i<=m;i++)
scanf("%d",&a[i]);
for (int i=;i<=n;i++)
{
scanf("%d",&k);
for (int j=;j<=k;j++)
{
scanf("%d",&x);
buy[i].push_back(x);
}
scanf("%d",&x);
add(i,T,x),add(T,i,);
}
for (int i=;i<=n;i++)
for (int j=;j<buy[i].size();j++)
{
int v=buy[i][j];
if (!last[v])
last[v]=i,add(S,i,a[v]),add(i,S,);
else
add(last[v],i,INF),add(i,last[v],),last[v]=i;
}
while (bfs())
while ((tmp=dfs(S,INF))>) ans+=tmp;
printf("%d",ans);
return ;
}

POJ 1149 PIGS | 最大流问题的更多相关文章

  1. poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新

    题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...

  2. POJ 1149 PIGS(Dinic最大流)

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20738   Accepted: 9481 Description ...

  3. POJ 1149 PIGS

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20579   Accepted: 9387 Description ...

  4. poj 1149 PIGS【最大流经典建图】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18727   Accepted: 8508 Description ...

  5. 网络流(最大流):POJ 1149 PIGS

    PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) ...

  6. POJ 1149 - PIGS - [最大流构图]

    Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...

  7. POJ 1149 PIGS(最大流)

    Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock an ...

  8. POJ 1149 PIGS (AC这道题很不容易啊)网络流

    PIGS Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlo ...

  9. POJ 1149 PIGS 【网络流】

    题意: m n   //有m个猪圈,n个人卖猪. a1...am    //编号为i的猪圈里有ai头猪. b1 c1...cb1 d1   //第i个人有bi把钥匙,分别是ci猪圈的,其它猪圈里的猪都 ...

随机推荐

  1. 深入浅出:了解JavaScript的ES6、ES7新特性

    参照阮一峰博客:http://es6.ruanyifeng.com/#README es6常见题:https://blog.csdn.net/qq_39207948/article/details/8 ...

  2. 制作linux系统U盘并使用U盘安装CentOS7.6系统

    目录   一.制作linux启动盘     1.1. 准备工作     1.2. 制作linux系统U盘   二.使用U盘安装Centos7.6     2.1. 使用U盘启动     2.2. 更改 ...

  3. python数据类型的转换

  4. 判断移动端和pc端最简单的方法

    <!DOCTYPE html><html><head> <title></title> <script type="text ...

  5. paramiko基本操作

    paramiko 是一个用作远程控制的模块,它遵循SSH2协议,支持以加密和认证的方式来进行远程服务器的连接.操作.上传和下载. 官方文档地址:http://docs.paramiko.org/ pa ...

  6. [BZOJ1009][HNOI2008]GT考试(KMP+DP)

    [不稳定的传送门 Solution dp[i][j]表示前i个字符当前匹配到不吉利串的第j个,即当前方案的后缀等于不吉利串前缀 然而由于n过大,不能直接转移,用矩阵优化 Code #include & ...

  7. Snowflake Snow Snowflakes【Poj3349】

    Description You may have heard that no two snowflakes are alike. Your task is to write a program to ...

  8. Git-Git里程碑

    里程碑即Tag,是人为对提交进行的命名.这和Git的提交ID是否太长无关,使用任何数字版本号无论长短,都没有使用一个直观的表意的字符串来得方便.例如:用里程碑名称"v2.1"对应于 ...

  9. 9,Linux下的python3,virtualenv,Mysql、nginx、redis安装配置

    常用服务安装部署   学了前面的Linux基础,想必童鞋们是不是更感兴趣了?接下来就学习常用服务部署吧! 安装环境: centos7 + vmware + xshell MYSQL(mariadb) ...

  10. 5. css定位 居中

    1.准备工作 (1)添加背景图片 background: url('images/grass.png') (2)背景图片格式 background-size:contain; #完全限制在方框 #co ...