poj 1273 Drainage Ditches (网络流 最大流)
网络流模板题。
==========================================================================================================
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const LL INF = 1e9+;
const LL maxn = ;
const LL MOD = 1e9+;
int n, m, Head[maxn], k;
int Pre[maxn];
bool vis[maxn];
struct Edge
{
int from, to, w;
int next;
}edge[maxn*]; void Init()
{
memset(Head, -, sizeof(Head));
memset(Pre, -, sizeof(Pre));
}
void AddEdge(int s,int e,int w)
{
edge[k].from = s;
edge[k].to = e;
edge[k].w = w;
edge[k].next = Head[s];
Head[s] = k ++;
}
bool BFS(int s,int e)///从源点到汇点找到一条路径
{
memset(vis, false, sizeof(vis));
queue<int> Q;
Q.push(s);
Pre[s] = -;
vis[s] = true;
while( Q.size() )
{
int v = Q.front();
Q.pop();
if(v == e) return true; for(int i=Head[v]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if( !vis[to] && edge[i].w )
{
vis[to] = true;
Pre[to] = i;
Q.push(to);
}
}
}
return false;
} int Karp(int s,int e)
{
int ans = ;
while( BFS(s, e) )///如果能找到路径就一直找
{
int MinFlow = INF, Cur = Pre[e]; while(Cur != -)
{
MinFlow = min(MinFlow, edge[Cur].w);
Cur = Pre[edge[Cur].from];
}
ans += MinFlow;
Cur = Pre[e]; while(Cur != -)
{
edge[Cur].w -= MinFlow;
edge[Cur^].w += MinFlow;
Cur = Pre[edge[Cur].from];
}
}
return ans;
} int main()
{
while(cin >> m >> n)
{
int s, e, w;
Init();
for(int i=; i<m; i++)
{
scanf("%d %d %d", &s, &e, &w);
AddEdge(s, e, w);
AddEdge(e, s, );///增加的反向边
} printf("%d\n", Karp(, n) );
}
return ;
}
==========================================================================================================
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const LL INF = 1e9+;
const LL maxn = ;
const LL MOD = 1e9+;
int n, m, Head[maxn], k;
int Depth[maxn];
bool vis[maxn];
struct node
{
int s, e, flow;
int next;
}edge[maxn*]; void AddEdge(int s,int e,int flow)
{
edge[k].s = s;
edge[k].e = e;
edge[k].flow = flow;
edge[k].next = Head[s];
Head[s] = k ++;
} bool BfsDepth(int Star,int End)
{
memset(Depth, , sizeof(Depth) );
queue<int> Q;
Depth[Star] = ;
Q.push(Star); while( Q.size() )
{
int s = Q.front();
Q.pop();
if(s == End) return true; for(int i=Head[s]; i != -; i=edge[i].next)
{
int e = edge[i].e;
if(!Depth[e] && edge[i].flow)
{
Q.push(e);
Depth[e] = Depth[s] + ;
}
}
}
return false;
} int DFS(int s,int MaxFlow)///从s点发出的最大流量是MaxFlow
{
if(s == n) return MaxFlow;
int sFlow = ;///sFlow 从 for(int i=Head[s]; i != -; i = edge[i].next)
{
int e = edge[i].e, flow = edge[i].flow; if(Depth[s]+ == Depth[e] && flow)///到达下一层
{
flow = min(MaxFlow-sFlow, flow);
flow = DFS(e, flow);
edge[i].flow -= flow;
edge[i^].flow += flow;
sFlow += flow;
if(sFlow == MaxFlow)
break;
}
}
if(sFlow == )
Depth[s] = ;
return sFlow;
} int Dinic(int s,int e)
{
int ans = ;
while(BfsDepth(s,e) == true)
{
ans += DFS(s, INF);
}
return ans;
} int main()
{
while(scanf("%d %d",&m, &n) != EOF)
{
int s, e, w;
memset(Head, -, sizeof(Head));
k = ;
for(int i=; i<m; i++)
{
scanf("%d %d %d", &s, &e, &w);
AddEdge(s, e, w);
AddEdge(e, s, );///添加反向边
}
printf("%d\n", Dinic(, n) );
}
return ;
}
poj 1273 Drainage Ditches (网络流 最大流)的更多相关文章
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- poj 1273 Drainage Ditches【最大流入门】
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63924 Accepted: 2467 ...
- poj 1273 Drainage Ditches(最大流,E-K算法)
一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- POJ 1273 Drainage Ditches 网络流 FF
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74480 Accepted: 2895 ...
- POJ 1273 Drainage Ditches【最大流】
题意:给出起点是一个池塘,M条沟渠,给出这M条沟渠的最大流量,再给出终点是一条河流,问从起点通过沟渠最多能够排多少水到河流里面去 看的紫书的最大流,还不是很理解,照着敲了一遍 #include< ...
- POJ 1273 Drainage Ditches【最大流模版】
题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量 Dinic #include<iost ...
随机推荐
- Android4.0的Alertdialog对话框,设置点击其他位置不消失
Android4.0以上AlertDialog,包括其他自定义的dialog,在触摸对话框边缘外部,对话框消失. 可以设置这么一条属性,当然必须先AlertDialog.Builder.create( ...
- js request
比如你要获取aaa.aspx?id=2 使用方法为:var id= request('id');
- html input 文本框的一些操作(限制输入...)
1.取消按钮按下时的虚线框 在input里添加属性值 hideFocus 或者 HideFocus=true 2.只读文本框内容 在input里添加属性值 readonly 3.防止退后清空的TEXT ...
- MySQL性能测试工具之mysqlslap使用详解
mysqlslap是mysql自带的基准测试工具,优点:查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新,给出了性能测试数据而且提供了多种引擎的性能比较.msq ...
- C# 异步操作
在程序中,普通的方法是单线程的.但中途如果有大型的操作,比如读取大文件,大批量操作数据库,网络传输等,都会导致程序阻塞,表现在界面上就是程序卡或者死掉,界面元素不动了,不响应了.C#异步调用很好的解决 ...
- hdoj 2040
#include<stdio.h>int i,j,s1,s2;int cha(int a,int b){ s1=0; s2=0; for(i=1;i<a;i++) { ...
- 【POJ2752】【KMP】Seek the Name, Seek the Fame
Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...
- AJAX编程模板
AJAX一直以来没怎么接触,主要是做JSON数据在服务器和客户端之间传递的时候,被玩坏了,对它莫名的不可爱,最近心理阴影小了,于是又来看看它....... AJAX即“Asynchronous Jav ...
- ecshop--加载初始化文件
define('IN_ECS', true);require(dirname(__FILE__) . '/../../includes/init.php'); 在开头要加入这两句文件才可以访问数据库以 ...
- Linux系统分区
在Linux系统里面,"分区",被称作"挂载点" 挂载点 意思就是把一部分硬盘容量,分成一个文件夹的形式,用来做某些事情,这个文件夹的名字,就叫做:挂载点 (如 ...