参考了这个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. .NET向WebService传值为decimal、double、int、DateTime等非string类型属性时,服务器端接收不到数据的问题

    最近在做CRM项目时,使用C#调用SAP PI发布的WebService服务时遇到的问题: 向WebService传值为decimal.double.int.DateTime等非string类型数据时 ...

  2. Spring Security 简介

    本文引自:https://blog.csdn.net/xlecho/article/details/80026527 在 Web 应用开发中,安全一直是非常重要的一个方面.安全虽然属于应用的非功能性需 ...

  3. python--re(匹配字符串)

    \d 匹配任何十进制数:它相当于类 [0-9]. \D 匹配任何非数字字符:它相当于类 [^0-9]. \s 匹配任何空白字符:它相当于类 [ fv]. \S 匹配任何非空白字符:它相当于类 [^ f ...

  4. 第三课:PHP 语法

    PHP 脚本在服务器上执行,然后向浏览器发送回纯 HTML 结果. 基础 PHP 语法 PHP 脚本可放置于文档中的任何位置. PHP 脚本以 <?php 开头,以 ?> 结尾: < ...

  5. Python全栈day 04

    Python全栈day 04 一.解释器/编译器 补充:编译型语言和解释型语言? # 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行. c c++,c# ,java # 解释型: ...

  6. Python元组,列表,字典,集合

    1.元组 元组是有序的,只有index和count两种方法,一看到元组,就提醒是不可更改的 names = ('wll', 'ly', 'jxx', 'syq') (1)index方法 print(n ...

  7. Leetcode 701. 二叉搜索树中的插入操作

    题目链接 https://leetcode.com/problems/insert-into-a-binary-search-tree/description/ 题目描述 给定二叉搜索树(BST)的根 ...

  8. Python数据挖掘-航空公司客户价值分析

    出处:http://www.ithao123.cn/content-11127869.html 航空公司客户价值分析 目标:企业针对不同价值的客户制定个性化的服务,将有限的资源集中于高价值客户. 1. ...

  9. 9,K-近邻算法(KNN)

    导引: 如何进行电影分类 众所周知,电影可以按照题材分类,然而题材本身是如何定义的?由谁来判定某部电影属于哪 个题材?也就是说同一题材的电影具有哪些公共特征?这些都是在进行电影分类时必须要考虑的问 题 ...

  10. [bzoj3450]Tyvj1952Easy

    瓜皮期望真是弱成渣.. 完全不理解的感觉qwq...题面%了一发千古神犇WJMZBMR哈~ Description 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(我们来简化 ...