网络流模板题。
==========================================================================================================
#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 ;
}