#include <cstdio>
#include <cstring>
#include <queue>
#define _clr(x, y) memset(x, y, sizeof(x))
#define Min(x, y) (x < y ? x : y)
#define INF 0x3f3f3f3f
#define N 150
#define M 1005
using namespace std; int resi[N][N], h[N], ef[N];
int dist[N], pre[N];
int Maxf, S, T;
bool used[N];
queue<int> Q; // 一般预流推进算法 --47ms
void Push(int x)
{
for(int i=; i<=T; i++)
{
int tmp = Min(ef[x], resi[x][i]);
if(tmp> && (x==S || h[x]==h[i]+))
{
resi[x][i] -= tmp, resi[i][x] += tmp;
ef[x] -= tmp, ef[i] += tmp;
if(i==T) Maxf += tmp;
if(i!=S && i!=T) Q.push(i);
}
}
} void Push_Relabel(int n)
{
Maxf = ;
_clr(ef, );
_clr(h, );
h[S] = n, ef[S]=INF, ef[T]=-INF;
Q.push(S);
while(!Q.empty())
{
int x = Q.front();
Q.pop();
Push(x);
if(x!=S && x!=T && ef[x]>)
{
h[x]++;
Q.push(x);
}
}
printf("%d\n",Maxf);
} void Init(int m, int n)
{
int pig[M], last[M];
int num, k;
S=, T=n+;
_clr(last, );
_clr(resi, );
for(int i=; i<=m; i++)
scanf("%d", pig+i);
for(int i=; i<=n; i++)
{
scanf("%d", &num);
for(int j=; j<num; j++)
{
scanf("%d", &k);
if(last[k]==)
resi[S][i] += pig[k];
else
resi[last[k]][i] = INF;
last[k] = i;
}
scanf("%d", resi[i]+T);
}
} // 连续最短曾广路算法 --17ms
bool bfs_dinic()
{
_clr(dist, -);
dist[S] = ;
Q.push(S);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i=; i<=T; i++)
{
if(dist[i]< && resi[u][i])
{
dist[i] = dist[u]+;
Q.push(i);
}
}
}
return dist[T]> ? : ;
} int dfs(int x, int f)
{
int a=;
if(x==T) return f;
for(int i=; i<=T; i++)
{
if(resi[x][i] && dist[i]==dist[x]+ && (a=dfs(i, Min(f, resi[x][i]))))
{
resi[x][i] -= a, resi[i][x] += a;
return a;
}
}
return ;
} void Dinic()
{
int ans=, a;
while(bfs_dinic())
while(a=dfs(, INF)) ans+= a;
printf("%d\n", ans);
} // EK算法 --0ms
bool bfs()
{
_clr(used, );
_clr(pre, -);
int Sta[N], top=;
used[S] = true;
Sta[top++] = S;
while(top)
{
int u = Sta[--top];
for(int i=; i<=T; i++)
{
if(resi[u][i] && !used[i])
{
used[i] = true;
pre[i] = u;
if(i==T) return true;
Sta[top++] = i;
}
}
}
return false;
}
void EK()
{
int maxf=, d;
while(bfs())
{
d = INF;
for(int i=T; i!=S; i=pre[i])
d = Min(d, resi[pre[i]][i]);
for(int i=T; i!=S; i=pre[i])
{
resi[pre[i]][i] -= d;
resi[i][pre[i]] += d;
}
maxf += d;
}
printf("%d\n", maxf);
}
int main()
{
int n, m;
while(~scanf("%d%d", &m, &n))
{
while(!Q.empty()) Q.pop();
Init(m, n);
EK();
//Push_Relabel(n);
//Dinic();
}
return ;
}

poj 1149的更多相关文章

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

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

  2. AC日记——pigs poj 1149

    POJ - 1149 思路: 最大流: 代码: #include <cstdio> #include <cstring> #include <iostream> # ...

  3. POJ 1149 PIGS(Dinic最大流)

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

  4. 网络流 A - PIGS POJ - 1149 最大流

    A - PIGS POJ - 1149 这个题目我开始感觉很难,然后去看了一份题解,写的很好 https://wenku.baidu.com/view/0ad00abec77da26925c5b01c ...

  5. poj 1149 最大流

    题目链接:http://poj.org/problem?id=1149 #include <cstdio> #include <cmath> #include <algo ...

  6. 网络流相关知识点以及题目//POJ1273 POJ 3436 POJ2112 POJ 1149

    首先来认识一下网络流中最大流的问题 给定一个有向图G=(V,E),把图中的边看做成管道,边权看做成每根管道能通过的最大流量(容量),给定源点s和汇点t,在源点有一个水源,在汇点有一个蓄水池,问s-t的 ...

  7. POJ 1149 PIGS

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

  8. POJ 1149 PIGS 【网络流】

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

  9. POJ 1149 PIGS ★(经典网络流构图)

    [题意] 有M个猪圈,每个猪圈里初始时有若干头猪.一开始所有猪圈都是关闭的.依 次来了N个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪.每 个顾客分别都有他能够买的数量的上限.每个顾客走后, ...

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

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

随机推荐

  1. BZOJ 1003 物流运输 (动态规划 SPFA 最短路)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...

  2. (原)torch中显示nn.Sequential()网络的详细情况

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6065526.html 本部分多试几次就可以弄得清每一层具体怎么访问了. step1. 网络定义如下: ...

  3. thinkPHP中省市级联下拉列表

    公共函数放置位置common文件夹下common.php文件(此段代码也可放置在要使用的控制器中) 封装的下拉列表函数代码: /** * 根据列表拼装成一个下拉列表 ADD BY CK * @para ...

  4. curl 基本使用简介

    curl是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作.看一个简单的例子: #include <curl/curl.h> #include <std ...

  5. Common Subsequence--poj1458(最长公共子序列)

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43211   Accepted: 17 ...

  6. C语言float型数据在内存中的储存方式

  7. itunes一进store就提示已停止工作该怎么解决

    改兼容,换盘重装什么的都试过了没用,可以听歌更新软件更改账号,但是就是进不了store,每次都是这个进度就卡死了. 问题事件名称: APPCRASH 应用程序名: iTunes.exe 应用程序版本: ...

  8. MySQL导出数据文件

    SELECT * INTO OUTFILE '/root/a.txt' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' FROM t_log_in ...

  9. HDU 4009 Transfer water

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...

  10. Magento How To Display Product Custom Option On list.phtml

    Some time we need to display custom option of product on category list page to achive this task we o ...