网络流模板(模板题:POJ1273)
模板题:POJ1273
EK:
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,jy,map[305][305],c[305][305],a[305],p[305];
bool vis[305];
int M(int e)
{
int f=0;
queue<int>q;
while(1){
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
a[1]=0x3fffffff;vis[1]=1;q.push(1);
while(!q.empty()){
jy=q.front();q.pop();
for(int i=1;i<=e;i++){
if(!vis[i]&&map[jy][i]-c[jy][i]>0)
{
vis[i]=1;
a[i]=min(a[jy],map[jy][i]-c[jy][i]);
p[i]=jy;
q.push(i);
}
}
}
if(a[e]==0)break;
for(int i=e;i!=1;i=p[i]){
c[i][p[i]]-=a[e];
c[p[i]][i]+=a[e];
}
f+=a[e];
}
return f;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,0,sizeof(map));
memset(c,0,sizeof(c));
memset(p,0,sizeof(p));
for(int i=1;i<=n;i++)
{
register int xx,yy,ee;
scanf("%d%d%d",&xx,&yy,&ee);
map[xx][yy]+=ee;
}
printf("%d\n",M(m));
}
}
Dinic:
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 444
int tot,next[N],first[N],w[N],v[N],n,m,ch[N];
void add(int from,int to,int weight){
v[tot]=to;w[tot]=weight;
next[tot]=first[from];
first[from]=tot++;
}
bool tell(){
memset(ch,-1,sizeof(ch));
queue<int> q;
q.push(1);ch[1]=0;
while(!q.empty()){
int t=q.front();q.pop();
for(int i=first[t];~i;i=next[i])
if(w[i]&&ch[v[i]]==-1)
q.push(v[i]),ch[v[i]]=ch[t]+1;
}
return ch[n]!=-1;
}
int zeng(int a,int b){
if(a==n)return b;
int r=0;
for(int i=first[a];~i&&b>=r;i=next[i])
if(ch[a]+1==ch[v[i]]&&w[i]){
int t=zeng(v[i],min(b-r,w[i]));
w[i]-=t;w[i^1]+=t;r+=t;
}
if(!r)ch[a]=-1;
return r;
}
int dinic(){
int ans=0,jy;
while(tell())while(jy=zeng(1,0x3fffffff))ans+=jy;
return ans;
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF){
memset(first,-1,sizeof(first));
register int xx,yy,zz;
tot=0;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&xx,&yy,&zz);
add(xx,yy,zz);add(yy,xx,0);
}
printf("%d\n",dinic());
}
}
网络流模板(模板题:POJ1273)的更多相关文章
- 网络流——SAP模板
//网络流SAP模板,复杂度O(N^2*M) //使用前调用init(源点,汇点,图中点的个数),然后调用add_edge()加边 //调用getflow得出最大流 #define N 55 #def ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- C++面向对象编程之成员模板、模板特化、偏特化和模板模板参数
1.成员模板 理解起来就是类是模板类,他里面的成员函数又是个模板函数 上图例子:用派生类构造父类 2.模板特化 就是在类模板泛化后写特化的类,在template<>里不绑定类型,而在类后面 ...
- [C++基础]那些容易被混淆的概念:函数/数组指针-指针函数/数组,类/函数模板-模板类/函数
函数指针-指针函数 函数指针的重点是指针.表示的是一个指针,它指向的是一个函数.eg: int (*pf)(); 指针函数的重点是函数.表示的是一个函数,它的返回值是指针.eg: int* fun() ...
- ACM算法模板 · 一些常用的算法模板-模板合集(打比赛专用)
ACM算法模板 · 一些常用的算法模板-模板合集(打比赛专用)
- hdu 2987最大权闭合图模板类型题
/* 最大权闭合图模板类型的题,考验对知识概念的理解. 题意:如今要辞退一部分员工.辞退每个员工能够的到一部分利益(能够是负的),而且辞退员工,必须辞退他的下属.求最大利益和辞退的最小人数. 最大权闭 ...
- 最小费用最大流模板 poj 2159 模板水题
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15944 Accepted: 8167 Descr ...
- PKU 3281 Dining 网络流 (抄模板)
题意: 农夫约翰为他的牛准备了F种食物和D种饮料.每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛.最多能有多少头牛可以同时得到各自喜欢的食物和饮料? 思路: 用 s -> 食物 ...
- 经典网络流题目模板(P3376 + P2756 + P3381 : 最大流 + 二分图匹配 + 最小费用最大流)
题目来源 P3376 [模板]网络最大流 P2756 飞行员配对方案问题 P3381 [模板]最小费用最大流 最大流 最大流问题是网络流的经典类型之一,用处广泛,个人认为网络流问题最具特点的操作就是建 ...
随机推荐
- Altium Designer 2017 ActiveRoute使用以及其他技巧
ActiveRoute 点击右下角PCB->PCB ActiveRoute调出ActiveRoute面板 在设计电路时,有一堆细小的白色线,表示几个脚之间需要连接,按住键盘Alt + 鼠标左键, ...
- 学习csv
1.csv文件读取,csv文件是常用的数据存储格式之一,我们使用Python模块来处理csv文件,这是一个天气信息表 import csv from matplotlib import pyplot ...
- C# 通知机制 IObserver<T> 和 IObservable<T>
class Program { public static void Main() { // Define a provider and two observers. LocationTracker ...
- [luogu2148 SDOI2009] E&D (博弈论)
传送门 Solution 我们知道当SG不为0则先手必胜,然后就可以打表了 ̄▽ ̄ Code //By Menteur_Hxy #include <cmath> #include <c ...
- 五、Scrapy中Item Pipeline的用法
本文转载自以下链接: https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/item-pipeline.html https://doc.scra ...
- copy contents of file with variable number in Matlab
input : transient.case output: transient_1.case, transient_2.case, transient_3.case ... ************ ...
- 『REM』手机屏幕适配
function adapt(designWidth, rem2px){ var d = window.document.createElement('div'); d.style.width = ' ...
- 【codeforces 508A】Pasha and Pixels
[题目链接]:http://codeforces.com/contest/508/problem/A [题意] 让你在一个n*m的方格上给方格染色; 顺序给出染色的k个格子 如果在某一时刻 有一个2* ...
- sgu 194 无源汇有上下界的最大流(最大流模板dinic加优化)
模板类型的题具体参考国家集训队论文:http://wenku.baidu.com/view/0f3b691c59eef8c75fbfb35c.html 参考博客:http://blog.csdn.ne ...
- UVa - 11452 - Dancing the Cheeky-Cheeky
先上题目: F. Dancing the Cheeky-Cheeky Context The Cheeky-Cheeky is a new song. They dance it in Mula, ...