一 . 一. 一. 网络流简述
二 . 二. 二. 最大流
E K : EK: EK:
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define lowbit(x) ((x)&-(x))
typedef unsigned long long ull;
#define freedom() ios::sync_with_stdio(false);
inline int read(){int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn = 5e3+5;
int head[maxn],tot;
struct Edge{
int to,next;
ll val;
}e[maxn<<2];
void add_edge(int u,int v,int w)
{
e[tot].to=v;
e[tot].next=head[u];
e[tot].val=w;
head[u]=tot++;
}
bool vis[maxn];
int dis[maxn]; // 一条增广路的最大可行流量
int pre[maxn]; // 记录边的前驱
bool bfs(int s,int t)
{
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(s),vis[s]=true;
dis[s]=INF;
while (!q.empty())
{
int x=q.front(); q.pop();
for(int i=head[x];i!=-1;i=e[i].next)
{
int y=e[i].to,w=e[i].val;
if(vis[y]||w==0) continue;
dis[y]=min(dis[x],w);
pre[y]=i;
if(y==t) return true;
q.push(y); vis[y]=true;
}
}
return false;
}
ll EK(int s,int t)
{
ll ans=0;
while (bfs(s,t))
{
ans+=dis[t];
for(int i=t;i!=s;i=e[pre[i]^1].to)
e[pre[i]].val-=dis[t],e[pre[i]^1].val+=dis[t]; // 正向边+,反向边-。
}
return ans;
}
int main()
{
int n=read(),m=read(),s=read(),t=read();
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++)
{
int u=read(),v=read(),w=read();
add_edge(u,v,w);
add_edge(v,u,0);
}
printf("%lld\n",EK(s,t));
return 0;
}
D i n i c : Dinic: Dinic:
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define lowbit(x) ((x)&-(x))
typedef unsigned long long ull;
#define freedom() ios::sync_with_stdio(false);
inline int read(){int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn = 5e3+5;
int head[maxn],tot;
struct Edge{
int to,next;
ll val;
}e[maxn<<2];
void add_edge(int u,int v,int w)
{
e[tot].to=v;
e[tot].next=head[u];
e[tot].val=w;
head[u]=tot++;
}
bool vis[maxn];
int deep[maxn]; // 记录每个点的层
bool bfs(int s,int t) // 对图进行分层
{
memset(deep,0,sizeof(deep));
queue<int>q;
q.push(s);
deep[s]=1;
while (!q.empty())
{
int x=q.front(); q.pop();
for(int i=head[x];i!=-1;i=e[i].next)
{
int y=e[i].to;
ll w=e[i].val;
if(deep[y]||!w) continue;
deep[y]=deep[x]+1;
q.push(y);
}
}
return deep[t];
}
ll dfs(int x,int t,ll In)
{
// In:流入该点的流量,Out:流出该点的流量
if(x==t) return In;
ll Out=0;
for(int i=head[x];i!=-1;i=e[i].next)
{
int y=e[i].to;
if(e[i].val&&deep[y]==deep[x]+1)
{
ll res=dfs(y,t,min(e[i].val,In));
e[i].val-=res;
e[i^1].val+=res;
In-=res;
Out+=res;
}
}
if(In!=Out) deep[x]=0;
return Out;
}
ll Dinic(int s,int t)
{
ll ans=0;
while (bfs(s,t))
ans+=dfs(s,t,1e18);
return ans;
}
int main()
{
int n=read(),m=read(),s=read(),t=read();
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++)
{
int u=read(),v=read(),w=read();
add_edge(u,v,w);
add_edge(v,u,0);
}
printf("%lld\n",Dinic(s,t));
return 0;
}
三 . 三. 三. 费用流
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
//#define INF 0x3f3f3f3f
typedef unsigned long long ull;
inline int read(){int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn = 1e5+5;
const ll INF=1e18;
int tot,head[maxn];
struct Edge{
int to,next;
int val,w;
}edge[maxn];
void add_edge(int u,int v,int w,int c)
{
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].val=w;
edge[tot].w=c;
head[u]=tot++;
}
ll dis[maxn],di[maxn];
int pre[maxn];
bool vis[maxn];
bool spfa(int s,int t)
{
memset(vis,false,sizeof(vis));
for(int i=0;i<1e5;i++) dis[i]=INF;
queue<int>q;
q.push(s);
vis[s]=true;
dis[s]=0;
di[s]=INF;
while (!q.empty())
{
int x=q.front(); q.pop();
vis[x]=false;
for(int i=head[x];i!=-1;i=edge[i].next)
{
if(edge[i].val==0) continue;
int y=edge[i].to;
if(dis[y]>dis[x]+edge[i].w)
{
dis[y]=dis[x]+edge[i].w;
di[y]=min(di[x],(ll)edge[i].val);
pre[y]=i;
if(!vis[y]) q.push(y),vis[y]=true;
}
}
}
if(dis[t]==INF) return false;
return true;
}
ll EK(int s,int t)
{
ll Flow=0,W=0;
while (spfa(s,t))
{
Flow+=di[t];
for(int i=t;i!=s;i=edge[pre[i]^1].to)
edge[pre[i]].val-=di[t],edge[pre[i]^1].val+=di[t];
W+=di[t]*dis[t];
}
printf("%lld ",Flow);
return W;
}
int main()
{
int n=read(),m=read(),s=read(),t=read();
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++)
{
int u=read(),v=read(),w=read(),c=read();
add_edge(u,v,w,c);
add_edge(v,u,0,-c);
}
printf("%lld\n",EK(s,t));
return 0;
}
分
析
:
使
∏
i
=
1
k
w
i
最
小
,
取
对
数
后
为
∑
i
=
1
k
w
i
分析:使\prod\limits_{i=1}^{k}{w}_{i}最小 ,取对数后为\sum\limits_{i=1}^{k}{w}_{i}
分析:使i=1∏kwi最小,取对数后为i=1∑kwi
这
样
就
转
化
成
了
求
最
小
割
集
这样就转化成了求最小割集
这样就转化成了求最小割集
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
typedef unsigned long long ull;
inline int read(){int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn = 1e5+5;
int tot,head[maxn];
struct Edge{
int u,to,next;
double val;
int ww;
}edge[maxn];
void add_edge(int u,int v,int w)
{
double val=log(w);
edge[tot]={u,v,head[u],val,w};
head[u]=tot++;
edge[tot]={v,u,head[v],val,w};
head[v]=tot++;
}
int dep[maxn];
bool bfs(int s,int t)
{
memset(dep,0,sizeof(dep));
queue<int>q;
q.push(s);
dep[s]=1;
while (!q.empty())
{
int x=q.front(); q.pop();
for(int i=head[x];i!=-1;i=edge[i].next)
{
int y=edge[i].to;
if(dep[y]||edge[i].val==0) continue;
dep[y]=dep[x]+1;
q.push(y);
}
}
return dep[t];
}
double dfs(int s,int t,double In)
{
if(s==t) return In;
double Out=0;
for(int i=head[s];i!=-1;i=edge[i].next)
{
int y=edge[i].to;
if(edge[i].val==0||dep[y]!=dep[s]+1) continue;
double res=dfs(y,t,min(edge[i].val,In));
edge[i].val-=res;
edge[i^1].val+=res;
In-=res;
Out+=res;
}
if(In!=Out) dep[s]=0;
return Out;
}
double Dinic(int s,int t)
{
double res=0;
while (bfs(s,t))
res+=dfs(s,t,1e9);
return res;
}
int Cs[maxn];
void ColS(int x)
{
Cs[x]=1;
for(int i=head[x];i!=-1;i=edge[i].next)
{
int y=edge[i].to;
if(!Cs[y]&&edge[i].val) ColS(y);
}
}
int main()
{
int n=read(),m=read();
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++)
{
int u=read(),v=read(),w=read();
add_edge(u,v,w);
}
double Flow_max=Dinic(1,n);
// printf("%lld\n",Flow_max);
ColS(1);
ll mod=1e9+7;
ll ans=1;
bool flag=false;
for(int i=0;i<=tot;i+=2)
if(Cs[edge[i].u]+Cs[edge[i].to]==1) flag=true,ans=ans*edge[i].ww%mod;
if(!flag) puts("0");
else printf("%lld\n",ans);
return 0;
}
/*
6 7
1 2 3
2 3 2
3 6 3
1 4 3
4 5 2
2 5 3
5 6 3
6
*/