POJ 1149 PIGS | 最大流问题
参考了这个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 | 最大流问题的更多相关文章
- poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新
题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...
- POJ 1149 PIGS(Dinic最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20738 Accepted: 9481 Description ...
- POJ 1149 PIGS
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20579 Accepted: 9387 Description ...
- poj 1149 PIGS【最大流经典建图】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18727 Accepted: 8508 Description ...
- 网络流(最大流):POJ 1149 PIGS
PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) ...
- POJ 1149 - PIGS - [最大流构图]
Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...
- POJ 1149 PIGS(最大流)
Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock an ...
- POJ 1149 PIGS (AC这道题很不容易啊)网络流
PIGS Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlo ...
- POJ 1149 PIGS 【网络流】
题意: m n //有m个猪圈,n个人卖猪. a1...am //编号为i的猪圈里有ai头猪. b1 c1...cb1 d1 //第i个人有bi把钥匙,分别是ci猪圈的,其它猪圈里的猪都 ...
随机推荐
- MyString类的实现--基础中的基础C语言
MyString 类是学习 C++ 的过程中一个很重要的例子,涉及到面向对象的封装.堆内存申请和释放.函数的重载以及 C++ 的 “Big Three”.本例子重点在于复习和理解上述的 C++ 特性, ...
- 谈谈两种标准库类型---string和vector
两种最重要的标准库---string和vector string和vector是两种最重要的标准库类型,string表示可变长的字符序列,vector存放的是某种给定类型对象的可变长序列. 一.标准库 ...
- 【A* 网络流】codechef Chef and Cut
高嘉煊讲的杂题:A*和网络流的练手题 题目大意 https://s3.amazonaws.com/codechef_shared/download/translated/SEPT16/mandarin ...
- C#基础-面向对象-继承
继承 新建一个基类 对Person类3个字段进行重构->封装字段 效果如下: public string Name { get => name; set => name = valu ...
- 记一次samba排错 Failed to start Samba SMB Daemon.
记录一次服务出错排错的过程,很多新手出了点错不百度直接巴拉巴拉的问,一般老手根据经验可以给出一点建议,但是由于个体环境的差异并不适用,反而埋怨起来.这种真的无F**K可说,所以要培养自己的排错能 ...
- 39.VUE学习--组件,子组件中data数据的使用,x-template模板的使用,改变for循环里的某条数据里的值
多处引用相同组件时,操作data不会相互影响 <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- E - Nature Reserve CodeForces - 1059D
传送门 There is a forest that we model as a plane and live nn rare animals. Animal number iihas its lai ...
- Card Hand Sorting 18中南多校第一场C题
一.题意 随机给你一堆牌(标准扑克牌),之后让你按照: 第一优先规则:所有相同花色的在一起 第二优先规则:所有相同花色的必须按照升序或者降序排列 问,你最少要拿出多少张牌插入到其他的地方以维持这个状况 ...
- 笔记-爬虫-去重/bloomfilter
笔记-爬虫-去重/bloomfilter 1. 去重 为什么要去重? 页面重复:爬的多了,总会有重复的页面,对已爬过的页面肯定不愿意再爬一次. 页面更新:很多页面是会更新的,爬取这种页面时就 ...
- Android Kotlin 连接 http
由于近期网上搜索了很多Android连接到http的方法, 可是2013年以前的方法现在都不能用了,要么报错,要么被遗弃,岁月留下来的东西只能自己整理了. 其实很简单,就一个HttpUtil通用类.可 ...