参考了这个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. C#基础学习笔记(个人整理)

    学习笔记 第一章:c#基础 一.程序设计语言的发展及历史 1.程序设计语言? 通俗也叫编程语言,实现人与机器交互的工具 2.历史 1)机器语言 : 0,1 2)汇编语言 : 包含一些机器语言,同时增加 ...

  2. 8-2 开发接口 (入参是json格式)

    1.开发入参事json格式的接口 import json import tools import flask from .check_session import check_session serv ...

  3. python__基础 : 类的__new__方法与实现一个单例

    __new__ : 这个方法的作用主要是创建一个实例,在创建实例时首先会调用 __new__方法 ,然后调用__init__对实例进行初始化, 如果想修改 __new__ 这个方法,那么最后要 ret ...

  4. 微信小程序推广方案

    拥有小程序只是基础,能玩转小程序运营才是关键.本文将会简单讲述十种最实用的小程序推广策略,结合具体案例阐述商家企业如何在拥有小程序后玩转小程序,快速实现小程序的推广. 一. 公众号+小程序 小程序可以 ...

  5. C语言字符篇(一)字符串转换函数

      #include <stdlib.h>   double atof(const char *nptr);  将字符串转换成双精度浮点数 int atoi(const char *npt ...

  6. 深度学习 GPU环境 Ubuntu 16.04 + Nvidia GTX 1080 + Python 3.6 + CUDA 9.0 + cuDNN 7.1 + TensorFlow 1.6 环境配置

    本节详细说明一下深度学习环境配置,Ubuntu 16.04 + Nvidia GTX 1080 + Python 3.6 + CUDA 9.0 + cuDNN 7.1 + TensorFlow 1.6 ...

  7. struts2官方 中文教程 系列五:处理表单

    先贴个本帖的地址,免得其它网站被爬去了struts2教程 官方系列五:处理表单  即 http://www.cnblogs.com/linghaoxinpian/p/6906298.html 下载本章 ...

  8. win32 signal

    Remarks   The signal function enables a process to choose one of several ways to handle an interrupt ...

  9. Redmine部署到Windows Azure

    有幸,今天可以尝试将Redmine部署到Windows Azure中,记下点滴,方便大家查阅 步骤一:Windows Azure中安装Ubuntu VM 遇到的问题,创建VM时会提示云服务.云存储订阅 ...

  10. 剑指Offer - 九度1511 - 从尾到头打印链表

    剑指Offer - 九度1511 - 从尾到头打印链表2013-11-29 21:08 题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例.每一组测试案例 ...