【最大流】hihocoder 1369 : 网络流一·Ford-Fulkerson算法
http://hihocoder.com/problemset/problem/1369?sid=1328132
参考 https://blog.csdn.net/a1799342217/article/details/73195243
https://blog.csdn.net/a519781181/article/details/51908303
【AC1】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
int n,m;
const int maxn=5e2+;
const int maxm=2e4+;
const int inf=0x3f3f3f3f;
int maxflow;
struct edge{
int to;
int nxt;
int w;
}e[*maxm];
int head[maxn];
int tot;
int fa[maxn];
int mp[maxn][maxn];
bool vis[maxn];
void init(){
memset(mp,-,sizeof(mp));
memset(head,-,sizeof(head));
tot=;
maxflow=;
}
void add(int u,int v){
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
}
bool bfs(int s,int t){
memset(vis,false,sizeof(vis));
memset(fa,-,sizeof(fa));
queue<int> Q;
Q.push(s);
while(!Q.empty()){
int u=Q.front();
Q.pop();
if(u==t) return true;
if(vis[u]) continue;
vis[u]=true;
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
if(!vis[v]&&mp[u][v]){
fa[v]=u;
Q.push(v);
}
}
}
return false;
}
int max_flow(int s,int t){
int flow=;
// int cnt=0;
while(bfs(s,t)){
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++){
// cout<<mp[i][j]<<" ";
// }
// cout<<endl;
// }
// cout<<"**********************"<<endl;
// cnt++;
int u=t;
int delta=inf;
while(fa[u]!=-){
// cout<<u<<" ";
delta=min(delta,mp[fa[u]][u]);
u=fa[u];
}
// cout<<endl;
// cout<<delta<<endl;
flow+=delta;
u=t;
while(fa[u]!=-){
mp[fa[u]][u]-=delta;
mp[u][fa[u]]+=delta;
u=fa[u];
} }
return flow;
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
int u,v,c;
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&c);
if(mp[u][v]==-){
add(u,v);
add(v,u);
mp[u][v]=c;
mp[v][u]=;
}else{
mp[u][v]+=c;
}
}
int ans=max_flow(,n);
printf("%d\n",ans);
}
return ;
}
【AC2】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
int n,m;
const int maxn=5e2+;
const int maxm=2e4+;
const int inf=0x3f3f3f3f;
struct edge{
int to;
int nxt;
int w;
}e[maxm<<];
int head[maxn];
struct node{
int x;
int e;
}fa[maxn]; bool vis[maxn];
int tot;
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v,int c){
e[tot].to=v;
e[tot].w=c;
e[tot].nxt=head[u];
head[u]=tot++;
}
bool bfs(int s,int t){
memset(vis,false,sizeof(vis));
queue<int> Q;
Q.push(s);
vis[s]=true;
while(!Q.empty()){
int u=Q.front();
Q.pop();
if(u==t) return true;
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
int w=e[i].w;
if(!vis[v]&&w){
Q.push(v);
vis[v]=true;
fa[v].x=u;
fa[v].e=i;
}
}
}
return false;
}
int work(){
int s=,t=n;
for(int i=;i<=n;i++){
fa[i].e=-;
fa[i].x=-;
}
int ans=;
while(bfs(s,t)){
int delta=inf;
int u=t;
while(fa[u].x!=-){
delta=min(delta,e[fa[u].e].w);
u=fa[u].x;
}
ans+=delta;
u=t;
while(fa[u].x!=-){
int i=fa[u].e;
e[i].w-=delta;
e[i^].w+=delta;
u=fa[u].x;
}
for(int i=;i<=n;i++){
fa[i].e=-;
fa[i].x=-;
}
}
return ans;
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
int u,v,c;
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&c);
add(u,v,c);
add(v,u,);
}
int ans=work();
printf("%d\n",ans);
}
return ;
}
【最大流】hihocoder 1369 : 网络流一·Ford-Fulkerson算法的更多相关文章
- ACM/ICPC 之 网络流入门-Ford Fulkerson与SAP算法(POJ1149-POJ1273)
第一题:按顾客访问猪圈的顺序依次构图(顾客为结点),汇点->第一个顾客->第二个顾客->...->汇点 //第一道网络流 //Ford-Fulkerson //Time:47M ...
- 网络流-最大流问题 ISAP 算法解释(转自Renfei Song's Blog)
网络流-最大流问题 ISAP 算法解释 August 7, 2013 / 编程指南 ISAP 是图论求最大流的算法之一,它很好的平衡了运行时间和程序复杂度之间的关系,因此非常常用. 约定 我们使用邻接 ...
- HDU3549 Flow Problem(网络流增广路算法)
题目链接. 分析: 网络流增广路算法模板题.http://www.cnblogs.com/tanhehe/p/3234248.html AC代码: #include <iostream> ...
- Hihocoder 太阁最新面经算法竞赛18
Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...
- hihoCoder太阁最新面经算法竞赛15
hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...
- hihocoder Round #c1(hihoCoder太阁最新面经算法竞赛1 )
Test链接:https://cn.vjudge.net/contest/231849 选自hihoCoder太阁最新面经算法竞赛1 更多Test:传送门 A:区间求差 给一组区间集合A和区间集合B, ...
- 【hihocoder 1369】网络流一·Ford-Fulkerson算法
[Link]:http://hihocoder.com/problemset/problem/1369 [Description] [Solution] 最大流模板题 [NumberOf WA] [R ...
- #1369 : 网络流一·Ford-Fulkerson算法 模板题
http://hihocoder.com/problemset/problem/1369?sid=1108721 别人都说先学网络流再学二分图,但是我先学了二分图的,感觉网络流好高端啊. 首先对于原图 ...
- hihoCoder 1393 网络流三·二分图多重匹配(Dinic求二分图最大多重匹配)
#1393 : 网络流三·二分图多重匹配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小H ...
随机推荐
- 文件系统inodes使用率过高问题处理
运维过程中经常碰见文件系统inodes使用率过高导致文件系统不可写的问题,常见场景如下 .Oracle产生的审计文件,特别是DG备库或者审计设置为OS时 .crontab产生大量邮件,导致/var/s ...
- Lepus天兔数据库监控系统安装配置
[root@redis01 ~]# cd /opt/[root@redis01 opt]# wget https://www.apachefriends.org/xampp-files/5.6.36/ ...
- 1005: [HNOI2008]明明的烦恼
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 6539 Solved: 2558[Submit][Status][Discuss] Descripti ...
- Spring Framework(框架)整体架构 变迁
Spring Framework(框架)整体架构 2018年04月24日 11:16:41 阅读数:1444 标签: Spring框架架构 更多 个人分类: Spring框架 版权声明:本文为博主 ...
- html下拉菜单栏代码
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 牛课第二次多校I
链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 题目描述 White Cloud has a square of n*n from (1,1) ...
- c++实验5
设计并实现一个机器宠物类MachinePets #include <iostream> #include <string> using namespace std; class ...
- SparkSQL查询程序的两种方法,及其对比
import包: import org.apache.spark.{SparkConf, SparkContext}import org.apache.spark.rdd.RDDimport org. ...
- OpenResty安装与hello world
安装环境:CentOS 7.0 1. 安装编译工具.依赖库 yum -y install readline-devel pcre-devel openssl-devel gcc 2. 下载openre ...
- Eclipse 读取config目录下文件
最近在一个项目,在项目下新建了一个config配置文件夹,添加一个配置文件config.properties. 使用classpath:config.properties方式加载配置文件, 具体实现代 ...