poj1273Drainage Ditches
- #include<iostream>
- /*
- 题意:就是寻找从源点到汇点的最大流!
- 要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来
- 就是这两个点之间的流量了!
- 思路:建图之后直接套用最大流算法(EK, 或者是Dinic算法) 图解Dinic算法流程!
- */
- #include<queue>
- #include<cstring>
- #include<cstdio>
- #include<algorithm>
- #define INF 0x3f3f3f3f3f3f3f3f
- #define N 205
- using namespace std;
- typedef long long LL;
- LL cap[N][N];
- int m, n;
- LL maxFlow;
- int d[N];
- queue<int>q;
- bool bfs(){
- q.push();
- memset(d, , sizeof(d));
- d[]=;
- while(!q.empty()){
- int u=q.front();
- q.pop();
- for(int v=; v<=n; ++v)
- if(!d[v] && cap[u][v]>){
- d[v]=d[u]+;
- q.push(v);
- }
- }
- if(!d[n]) return false;
- return true;
- }
- LL dfs(int u, LL flow){
- if(u==n) return flow;
- for(int v=; v<=n; ++v)
- if(d[v]==d[u]+ && cap[u][v]>){
- LL a=dfs(v, min(flow, cap[u][v]));
- if(a==) continue;//如果a==0 说明没有找到从起点到汇点的增广路, 然后换其他路接着寻找!
- cap[u][v]-=a;
- cap[v][u]+=a;
- return a;
- }
- return ;
- }
- void Dinic(){
- LL flow;
- while(bfs()){//利用bfs构造好层次图,这样dfs在寻找阻塞流的时候,就不会盲目的寻找了!
- while(flow=dfs(, INF)) maxFlow+=flow;//利用构造好的层次图不断的寻找阻塞流!
- }
- }
- int main(){
- while(scanf("%d%d", &m, &n)!=EOF){
- memset(cap, , sizeof(cap));
- while(m--){
- int u, v;
- LL w;
- scanf("%d%d%lld", &u, &v, &w);
- cap[u][v]+=w;
- }
- maxFlow=;
- Dinic();
- printf("%lld\n", maxFlow);
- }
- return ;
- }
- //EK算法同样搞定
- #include<iostream>
- #include<queue>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #define INF 0x3f3f3f3f
- using namespace std;
- typedef __int64 LL;
- LL cap[][];
- int pre[];
- LL a[];
- int m, n;
- queue<int>q;
- LL maxFlow;
- bool spfa(){
- while(!q.empty()) q.pop();
- memset(a, , sizeof(a));
- q.push();
- a[]=INF;
- while(!q.empty()){
- int u=q.front();
- q.pop();
- for(int v=; v<=n; ++v)
- if(!a[v] && cap[u][v]>){
- pre[v]=u;
- a[v]=min(a[u], cap[u][v]);
- q.push(v);
- }
- if(a[n]) break;
- }
- if(!a[n]) return false;
- return true;
- }
- void EK(){
- maxFlow=;
- while(spfa()){
- int u=n;
- maxFlow+=a[n];
- while(u!=){
- cap[pre[u]][u]-=a[n];
- cap[u][pre[u]]+=a[n];
- u=pre[u];
- }
- }
- }
- int main(){
- while(scanf("%d%d", &m, &n)!=EOF){
- memset(cap, , sizeof(cap));
- while(m--){
- int u, v;
- LL w;
- scanf("%d%d%I64d", &u, &v, &w);
- cap[u][v]+=w;
- }
- EK();
- printf("%I64d\n", maxFlow);
- }
- return ;
- }
poj1273Drainage Ditches的更多相关文章
- POJ1273Drainage Ditches[最大流]
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 71559 Accepted: 2784 ...
- POJ-1273-Drainage Ditches(网络流之最大流)
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This ...
- POJ-1273-Drainage Ditches 朴素增广路
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 70588 Accepted: 2743 ...
- 图论-网络流-最大流--POJ1273Drainage Ditches(Dinic)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 91585 Accepted: 3549 ...
- POJ-1273Drainage Ditches(网络流入门题,最大流)
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This ...
- poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)
最大流模板题 大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表. #include <iostream> #include <cstdio> #inclu ...
- 【最大流Dinic模板】HDU1532&POJ1273-Drainage Ditches(16/3/6更正)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- POJ 1273 Drainage Ditches题解——S.B.S.
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67823 Accepted: 2620 ...
- I:trainage Ditches
总时间限制: 1000ms 内存限制: 65536kB描述Every time it rains on Farmer John's fields, a pond forms over Bessie's ...
随机推荐
- HttpServletRequest的Attribute和Parameter区别
HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下的组件通过getParameter()方法来获得请求参数,例如假定we ...
- [原] XAF How to bind a stored procedure to a ListView in XAF
First, I suggest that you review the following topic to learn how to show a custom set of objects in ...
- Python-变量
1.Python的变量是什么 变量是用来存储计算机程序中的信息,唯一的目的是将数据存储在内存中. 2.Python变量的组成 变量由字母.数字.下划线组成: 变量的第一位不能是数字,可以是字母或下划线 ...
- 关于Apache日志的统计
统计apache日志文件里访问量前十的ip并按从多到少排列 五月 31, 2012 by FandLR Filed under Linux Leave a comment 解法1: cat acc ...
- spring 3.1 配置 JCR 303 Bean Validation
A) 导入Hibernate-Validator 要使用JSR303 校验框架, 需要加入框架的具体实现Hibernate-Validator, 在soureforge上下载最新的Hibernate ...
- c一些关键字
register:这个关键字请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率.注意是尽可能,不是绝对. extern:可以置于变量或者函数前,以标示变量或者函数的定义 ...
- Quartz.net2.2初体验
简介:Quartz.net是一个开源的作用调度框架,非常强大,能够通过简单的配置帮助我们定时具体的操作.相对于我们用的线程里面while(true)然后sleep来执行某个操作,应该算的上是高端,大气 ...
- C# 调用restful服务开源库
.NET环境下我们想调用其它开放平台的服务接口,不需要自己去实现底层,开源的库用起来会很方便 hammock http://www.cnblogs.com/shanyou/archive/2012/0 ...
- Xamarin.IOS之多视图
欢迎大家加入以下开源社区 Xamarin-Cn:https://github.com/Xamarin-Cn Mvvmcross-Cn:https://github.com/Mvvmcross-Cn ...
- 我的ORM之十三 -- 性能参数
我的ORM索引 测试环境 台式机: 主板:映泰Z77 CPU:i5 3470(3.2GHz) 内存:DDR3 1600 8G(单条) 硬盘:创见 SSD 256G ORM从过程上,可以分两个大的部分: ...