poj1273
来源:互联网 发布:淘宝大学电商运营pdf 编辑:程序博客网 时间:2024/06/09 16:39
http://www.cnblogs.com/jackge/archive/2013/04/10/3012182.html
EK:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int VM=220;
const int INF=0x3f3f3f3f;
int n,m,max_flow; //max_flow是最大流
int map[VM][VM],flow[VM][VM]; // map[i][j]是每条边的容量,flow[i][j]是每条边的流量
int res[VM],pre[VM]; //res[]是每个点的剩余流量,pre[]是每个点的父亲
int EK(int src,int des){
max_flow=0;
queue<int> q;
while(!q.empty())
q.pop();
memset(flow,0,sizeof(flow)); //最开始每条边的流量都是0
while(1){
memset(res,0,sizeof(res)); //残余流量得变0,一开始所有点都没流入对吧
res[src]=INF; //源点嘛,剩余流量无限是必须的...
q.push(src); //从源点开始进行BFS找增广路
int u,v;
while(!q.empty()){
u=q.front();
q.pop();
for(v=1;v<=m;v++) //遍历所有点,找可行边
if(!res[v] && map[u][v]>flow[u][v]){ //该点剩余流量为0 且 容量大于流量,也就是找到了新的结点
pre[v]=u; //找到新结点,父节点得记录一下吧
q.push(v);
res[v]=min(res[u],map[u][v]-flow[u][v]); //如果u的剩余流量能填满uv就填满,不能的话就把u这点的流量全部流向uv
}
}
if(res[des]==0) //如果当前已经是最大流,汇点没有残余流量
return max_flow;
for(u=des;u!=src;u=pre[u]){ //如果还能增广,那么回溯,从汇点往回更新每条走过的边的流量
flow[pre[u]][u]+=res[des]; //更新正向流量 (注意这里更新的是流量,而不是容量)
flow[u][pre[u]]-=res[des]; //更新反向流量
}
max_flow+=res[des];
}
}
int main(){
//freopen("input.txt","r",stdin);
while(~scanf("%d%d",&n,&m)){
memset(map,0,sizeof(map));
memset(pre,0,sizeof(pre));
int u,v,w;
while(n--){
scanf("%d%d%d",&u,&v,&w);
map[u][v]+=w; //有重边
}
printf("%d\n",EK(1,m));
}
return 0;
}
Dinic
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int VM=220;
const int INF=0x3f3f3f3f;
int n,m,src,des;
int map[VM][VM],dep[VM]; //dep[i]表示当前点到起点src的层数
int BFS(){ // 重新建图(按层数建图)
queue<int> q;
while(!q.empty())
q.pop();
memset(dep,-1,sizeof(dep));
dep[src]=0;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
for(int v=1;v<=m;v++)
if(map[u][v]>0 && dep[v]==-1){ // 如果可以到达且还没有访问
dep[v]=dep[u]+1;
q.push(v);
}
}
return dep[des]!=-1;
}
int DFS(int u,int minx){ // 查找路径上的最小的流量
if(u==des)
return minx;
int tmp;
for(int v=1;v<=m;v++)
if(map[u][v]>0 && dep[v]==dep[u]+1 && (tmp=DFS(v,min(minx,map[u][v])))){
map[u][v]-=tmp; //正向减少
map[v][u]+=tmp; //反向增加
return tmp;
}
return 0;
}
int Dinic(){
int ans=0,tmp;
while(BFS()){
while(1){
tmp=DFS(1,INF);
if(tmp==0)
break;
ans+=tmp;
}
}
return ans;
}
int main(){
//freopen("input.txt","r",stdin);
while(~scanf("%d%d",&n,&m)){
memset(map,0,sizeof(map));
int u,v,w;
for(int i=0;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
map[u][v]+=w; //防止有重边
}
src=1, des=m;
printf("%d\n",Dinic());
}
return 0;
}
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- POJ1273
- poj1273
- POJ1273
- poj1273 EK
- POJ1273 Drainage Ditches
- POJ1273(最大流)
- poj1273 Drainage Ditches
- A1.Android开发环境配置
- Cocos2d-x LoadingBar的使用,及定时加载进度
- 安卓开发——requestFeature() must be called before adding content
- 《Android源码设计模式》读书笔记 (4) 第4章 原型模式
- 活动对象demo_me
- poj1273
- 个人笔记 html 07 textarea 限制字数
- 学习IM软件开发
- hdu2222&hdu3065 AC自动机模板题
- 【Java】Java中this关键字的几种用法
- 个人笔记 js 16 js删除表格行
- C++ map的基本操作和使用
- cookie、localstroage与sessionstroage的一些优缺点
- 个人笔记 js 17 IE7不支持document.getElementById解决办法