#include <bits/stdc++.h> using namespace std; const int INF=0x3f3f3f3f; typedef long long LL; #define PI(A) printf("%d\n",A) #define SI(N) scanf("%d",&(N)) #define SII(N,M) scanf("%d%d",&(N),&(M)) #define cle(a,val) memset(a,(val),sizeof(a)) #define rep(i,b) for(int i=0;i<(b);i++) #define Rep(i,a,b) for(int i=(a);i<=(b);i++) #define reRep(i,a,b) for(int i=(a);i>=(b);i--) const double EPS= 1e-9 ; /* ///////////////////////// C o d i n g S p a c e ///////////////////////// */ const int MAX_V= 1000 + 5 ; struct edge{int to,cap,rev;}; vector<edge> G[MAX_V]; int level[MAX_V]; int iter[MAX_V]; void add_edge(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}); } void bfs(int s) { memset(level,-1,sizeof(level)); queue<int> que; level[s]=0; que.push(s); while(!que.empty()) { int v=que.front();que.pop(); for (int i=0;i<G[v].size();i++) { edge& e=G[v][i]; if (e.cap>0&&level[e.to]<0) { level[e.to]=level[v]+1; que.push(e.to); } } } } int dfs(int v,int t,int f) { if (v==t) return f; for (int i=iter[v];i<G[v].size();i++) { edge& e=G[v][i]; if (e.cap>0&&level[v]<level[e.to]) { int d=dfs(e.to,t,min(f,e.cap)); if (d>0) { e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0; } int max_flow(int s,int t) { int flow=0; for (;;) { bfs(s); if (level[t]<0) return flow; memset(iter,0,sizeof(iter)); int f; while((f=dfs(s,t,INF))>0) { flow+=f; } } } int main() { #ifndef ONLINE_JUDGE freopen("C:\\Users\\Zmy\\Desktop\\in.txt","r",stdin); // freopen("C:\\Users\\Zmy\\Desktop\\out.txt","w",stdout); #endif iostream::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n,m; while(cin>>n>>m) { int ans=0; for (int i=0;i<n;i++) { int u,v,w; cin>>u>>v>>w; add_edge(u,v,w); ans+=max_flow(1,m); } printf("%d\n",ans); } return 0; }
时间: 2024-10-12 17:37:05