题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量

Dinic

 #include<iostream>
#include<cstring>
#include<algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int N = ;
const int INF = 0x3f3f3f3f;
int m,n,tot;
int head[N],level[N];
struct node
{
int t,next,w;
}edge[N<<]; void init()
{
tot=-;
memset(head,-, sizeof(head));
} void add(int s,int t,int w)
{
edge[++tot].t=t,edge[tot].w=w,edge[tot].next=head[s],head[s]=tot;
edge[++tot].t=s,edge[tot].w=,edge[tot].next=head[t],head[t]=tot;
} bool bfs()
{
memset(level,,sizeof(level));
queue<int> q;
while(!q.empty())q.pop();
q.push();
level[]=;
while(q.size())
{
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=edge[i].next)
{
if(edge[i].w>&&level[edge[i].t]==)
{
q.push(edge[i].t);
level[edge[i].t]=level[u]+;
}
if(level[m]!=)return ;
}
}
return ;
} int dfs(int s,int t, int flow)
{
if(s==t)return flow;
for(int i=head[s];i!=-;i=edge[i].next)
{
if(edge[i].w>&&level[edge[i].t]==level[s]+)
{
int k = dfs(edge[i].t,t,min(flow,edge[i].w));
if(k>)
{
edge[i].w-=k;
edge[i^].w+=k;
return k;
}
}
}
return ;
}
//
void dinic()
{
int flow=;
while(bfs()) // 寻找最短增广路,分层图
{
int f=;
while((f=dfs(,m,INF))>)flow+=f; // 在分层图上增广
}
cout<<flow<<endl;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
while(n--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
dinic();
}
return ;
}

建图的另一种方法,上面有两种邻接表的方法,一种是单纯用数组模拟,可以运用异或操作反向边;另一个是vector模拟,反向边做特别记录

 struct edge
{
int to,cap,rev;// 反向边
};
vector<edge> g[maxn];
int level[maxn]; void add_edge(int from,int to,int cap)
{
//最后一个元素是反向边的索引,便于快速查找
g[from].push_back((edge){to,cap,g[to].size()});
g[to].push_back((edge){from,,g[from].size()-});
}

Ford_Fulkerson

POJ 1273 Drainage Ditches【最大流模版】的更多相关文章

  1. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  2. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  3. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  4. POJ 1273 Drainage Ditches 最大流

    这道题用dinic会超时 用E_K就没问题 注意输入数据有重边.POJ1273 dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的. 然而dinic主要依靠 ...

  5. poj 1273 Drainage Ditches_最大流模版

    #include <iostream> #include<cstdio> #include<queue> #include<cstring> using ...

  6. POJ 1273 Drainage Ditches | 最大流模板

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  7. POJ 1273 Drainage Ditches(最大流Dinic 模板)

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...

  8. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  9. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  10. poj 1273 Drainage Ditches【最大流入门】

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63924   Accepted: 2467 ...

随机推荐

  1. MySQL log_slave_updates 参数【转】

    说明:最近部署了mysql的集群环境,详细如下M01和M02为主主复制,M01和R01为主从复制:在测试的过程中发现了以下问题: 1.M01和M02的主主复制是没有问题的(从M01写入数据能同步到M0 ...

  2. 鼠标事件event和坐标

    鼠标事件(e=e||window.event) event.clientX.event.clientY 鼠标相对于浏览器窗口可视区域的X,Y坐标(窗口坐标),可视区域不包括工具栏和滚动条.IE事件和标 ...

  3. springboot:集成fastjson(教训)

    网上有很多介绍,主要有两种. 1.在启动类中注入bean /** * 集成fastjson * * @return */ @Bean public HttpMessageConverters fast ...

  4. ASP.NET MVC5高级编程 之 模型

    1. 为MVC Music Store建模 Models文件夹(右击) --> 添加 --> 类 为类添加对应的属性: public class Album { public virtua ...

  5. django.db.utils.OperationalError: (1045, "Access denied for user 'ODBC'@'localhost' (using password)

    错误描述: 从SQLLITE数据库换为MYSQL数据库,执行 python manage.py migrate 命令时,报错:django.db.utils.OperationalError: (10 ...

  6. spring aop -包的问题

    Caused by: java.lang.NoSuchMethodError: org.springframework.aop.framework.AopProxyUtils.getSingleton ...

  7. mybatis 按in 函数参数顺序排序

    使用 FIELD()函数 SELECT *  FROM   user  WHERE id IN (72, 80, 69)  ORDER BY FIELD(id, 72, 80, 69)

  8. swift 实践- 04 -- UIButton

    import UIKit class ViewController: UIViewController { // 按钮的创建 // UIButtonType.system: 前面不带图标, 默认文字为 ...

  9. swift 实践- 01 -- UItableView的简单使用

    import UIKit class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{ over ...

  10. 解决get方法提交参数中文乱码问题:

    解决get方法提交参数中文乱码问题: 1找到你们的tomcat的目录 2在这个目录下面\tomcat61-32\tomcat61\conf 3找到server.xml ,用notepad打开(没有就下 ...