poj 1273.PIG (最大流)
网络流
关键是建图,思路在代码里
/*
最大流SAP
邻接表
思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧。
优化:
1、当前弧优化(重要)。
1、每找到以条增广路回退到断点(常数优化)。
2、层次出现断层,无法得到新流(重要)。
时间复杂度(m*n^2)
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
const int INF = ;
int G[INF][INF];
struct node {
int v, c, next;
} edge[INF*INF*];
int pHead[INF*INF], SS, ST, nCnt;
void addEdge (int u, int v, int c) {
edge[++nCnt].v = v; edge[nCnt].c = c, edge[nCnt].next = pHead[u]; pHead[u] = nCnt;
edge[++nCnt].v = u; edge[nCnt].c = , edge[nCnt].next = pHead[v]; pHead[v] = nCnt;
}
int SAP (int pStart, int pEnd, int N) {
int numh[INF], h[INF], curEdge[INF], pre[INF];
int cur_flow, flow_ans = , u, neck, i, tmp;
ms (h, ); ms (numh, ); ms (pre, -);
for (i = ; i <= N; i++) curEdge[i] = pHead[i];
numh[] = N;
u = pStart;
while (h[pStart] <= N) {
if (u == pEnd) {
cur_flow = 1e9;
for (i = pStart; i != pEnd; i = edge[curEdge[i]].v)
if (cur_flow > edge[curEdge[i]].c) neck = i, cur_flow = edge[curEdge[i]].c;
for (i = pStart; i != pEnd; i = edge[curEdge[i]].v) {
tmp = curEdge[i];
edge[tmp].c -= cur_flow, edge[tmp ^ ].c += cur_flow;
}
flow_ans += cur_flow;
u = neck;
}
for ( i = curEdge[u]; i != ; i = edge[i].next)
if (edge[i].c && h[u] == h[edge[i].v] + ) break;
if (i != ) {
curEdge[u] = i, pre[edge[i].v] = u;
u = edge[i].v;
}
else {
if ( == --numh[h[u]]) continue;
curEdge[u] = pHead[u];
for (tmp = N, i = pHead[u]; i != ; i = edge[i].next)
if (edge[i].c) tmp = min (tmp, h[edge[i].v]);
h[u] = tmp + ;
++numh[h[u]];
if (u != pStart) u = pre[u];
}
}
return flow_ans;
}
/*
poj1149 最大流
建图:
每个顾客为一个节点,从源点到每个猪圈的第一个顾客连接一条容量为猪圈猪数目的边
从第一个顾客到同一个猪圈的其它顾客连一条容量无限的边
每个顾客到汇点的边的容量为他最大买的数量 */
int k, c, m, n,x,y,z;
int vis[INF],sum[INF];
void solve(){
nCnt=;
for(int i=;i<=ST;i++)
for(int j=;j<=ST;j++){
if(G[i][j]) addEdge(i,j,G[i][j]);
}
int ans=SAP(SS,ST,ST);
printf("%d\n",ans);
}
int main() {
/*
先用邻接矩阵统计容量,再用前向星存边,表头在pHead[],初始化nCnt=1
SS,ST分别为源点和汇点
*/
scanf("%d %d",&m,&n);
SS=n+,ST=n+;
for(int i=;i<=m;i++)
scanf("%d",&sum[i]);
for(int i=;i<=n;i++){
scanf("%d",&k);
for(int j=;j<=k;j++){
scanf("%d",&x);
if(!vis[x]){
G[SS][i]+=sum[x];
vis[x]=i;
}
else{
G[vis[x]][i]=0xffff;
}
}
scanf("%d",&k);
G[i][ST]=k;
}
solve();
return ;
}
poj 1273.PIG (最大流)的更多相关文章
- POJ 1273 网络流(最大流)模板
http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...
- Poj(1273),最大流,EK
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 69355 Accepted: 2687 ...
- POJ 1273 (基础最大流) Drainage Ditches
虽然算法还没有理解透,但以及迫不及待地想要A道题了. 非常裸的最大流,试试lrj的模板练练手. #include <cstdio> #include <cstring> #in ...
- UVA 820 --- POJ 1273 最大流
找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- poj 1273 最大流
题目链接:http://poj.org/problem?id=1273 a.EK算法:(Edmond-Karp): 用BFS不断找增广路径,当找不到增广路径时当前流量即为最大流. b.dinic算法: ...
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
随机推荐
- (转载)PHP isset()函数作用
(转载)http://www.cnblogs.com/neve/archive/2011/03/21/1990165.html isset函数是检测变量是否设置. 格式:bool isset ( mi ...
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Mr. Frog's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- Win32中常用消息
一 .WM_PAINT消息 1 WM_PAINT的产生 由于窗口的互相覆盖等,产生需要绘制的区域,那么会产生WM_PAINT消息. 一般情况下,不直接发送WM_PAINT消息,通过API声明需要绘 ...
- checkbox操作
小小示例:自己备份顺便粘出来共享. 引入头部文件:<script src="../js/jQuery1.7.2.js"></script> HTML代码: ...
- hdu 4403 枚举
#include<cstdio> #include<cstring> #include<iostream> #include<cmath> #inclu ...
- MySQL5.5 所支持的存储引擎
本博文的主要内容有 .存储引擎的概念 .MySQL5.5 所支持的存储引擎 .操作默认存储引擎 .选择存储引擎 与其他的数据库软件不同,MySQL数据库软件提供了一个名为存储引擎的概念,由于存储引擎是 ...
- kafka leader 服务器均衡。
Whenever a broker stops or crashes leadership for that broker's partitions transfers to other replic ...
- 推荐一个网站——聚合了微软的文件的Knowledge Base下载地址
Microsoft Files是一个微软的文件数据库,从这里可以很方便的找到各个文件版本对应的下载链接. 比如今天debug需要找一个特定版本的sos.dll,从这个网站就很方便的给出了这个sos.d ...
- smarty、thinkphp中的html加载其他的html文件的方式
1.smarty 在模板文件中,使用定界符 {include file="header.html"} 不可以省略.html 2.thinkphp的html文件中 <incl ...
- HTML5 File api 实现断点续传
目前市场上大多数的网站的断点上传都是需要安装浏览器插件的,本文就针对高级浏览器的环境下,通过HTML5 File api实现断点上传进行说明 一.实现文件多选 HTML5的<input>新 ...