参考了这个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. react的 react-router使用

    官方API:https://reacttraining.com/react-router/web/api/BrowserRouter; React Router 安装命令如下. 使用时,路由器Rout ...

  2. Java分享笔记:FileInputStream流的 read()方法 和 read(byte[] b)方法

    /*------------------------ FileInputStream: ....//输入流,字节流 ....//从硬盘中存在的一个文件中读取内容,读取到程序中 ....//read() ...

  3. Linux下 tar 命令详解

    tar 是 unix/linux下的打包器 [解压] 输入命令: # tar  -zxvf  filename.tar.gz 参数解释: z :表示 tar 包是被 gzip 压缩过的 (后缀是.ta ...

  4. python3 井字棋 GUI - 人机对战、机器对战 (threading、tkinter库)

    python3 井字棋 GUI - 人机对战.机器对战 功能 GUI界面 人机对战(可选择机器先走) 机器对战(50局) 流程图 内核 棋盘 [0][1][2] [3][4][5] [6][7][8] ...

  5. 微信小程序真机定位问题技巧

    小程序导航 https://wq.xmaht.top 开发者在开发小程序的时候可能会碰到一些这样的问题: 问题1  开发者工具上看效果没问题,但是在真机上测试不行? 问题2  有用户遇到小程序功能无法 ...

  6. [BZOJ3631][JLOI2014]松鼠的新家(树链剖分)

    [BZOJ3631] 树剖模板题了, Code #include <cstdio> #include <algorithm> #define MID int mid=(l+r) ...

  7. CentOS网络设置 couldn't resolve host 'mirrorlist.centos.org问题解决

    在VMWare上安装好centos后,使用yum安装nodejs报错:can not resolve host 'mirritlist.centos.org', 百度上很多都说在/etc/resolv ...

  8. Maven学习 (三) 使用m2eclipse创建web项目

    1.首先确认你的eclipse已经安装好m2eclipse的环境,可以参照上两篇Maven学习内容 2.新建一个maven的项目 3.下一步默认配置,使用默认的工作空间,或者你可以自己选择其他的空间 ...

  9. json对象和java对象的相互转换方法(json-lib、jackson、fastjson、gson)

    一 使用json-lib包下的JSONObject和JSONArray转换 代码如下: package com.test.test; import java.util.ArrayList; impor ...

  10. 批量上传图片(jQuery-File-Upload使用)

    jQuery-File-Upload jQuery-File-Upload是一个jquery下的ajax文件上传插件,支持批量上传,github地址:https://github.com/blueim ...