最大流模版题,弄了好多天也没有明白..
今天好不容易根据记忆写了个..
/* xzppp */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <bitset>
#include <iomanip>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int > pii;
typedef pair<LL,LL> pll;
typedef pair<double,double > pdd;
typedef pair<double,int > pdi;
const int MAXN = 1e5+17;
const int MAXM = 1e6+17;
const int MAXV = 2*1e3+17;
const int BIT = 15+3;
const int INF = 0x7fffffff;
const LL INFF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 998244353;
int vis[MAXN];
struct edge
{
int to,cap,rev;
edge(int a,int b,int c):to(a),cap(b),rev(c){};
};
vector<edge > G[MAXN];
void adeg(int from,int to,int cap)
{
G[from].push_back(edge(to,cap,G[to].size()));
G[to].push_back(edge(from,0,G[from].size()-1));
}
int dfs(int p,int t,int f)
{
if(p==t) return f;
vis[p] = 1;
for (int i = 0; i < G[p].size(); ++i)
{
int next = G[p][i].to;
if(!vis[next]&&G[p][i].cap>0)
{
int d = dfs(next,t,min(G[p][i].cap,f));
if(d>0)
{
G[p][i].cap -= d;
G[next][G[p][i].rev].cap += d;
return d;
}
}
}
return 0;
}
int maxflow(int s,int t)
{
int res = 0;
while(1)
{
memset(vis, 0, sizeof(vis));
int f = dfs(s,t,INF);
if(f==0) return res;
res += f;
}
}
int main()
{
#ifndef ONLINE_JUDGE
FFF
#endif
int m,n;
while(cin>>m>>n)
{
for (int i = 0; i < n; ++i)
{
G[i].clear();
}
for (int i = 0; i < m; ++i)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
u--;v--;
adeg(u, v, c);
}
int ans = maxflow(0, n-1);
cout<<ans<<endl;
}
return 0;
}