思路:
最大流模板。
1 #include<queue> 2 #include<cstdio> 3 #include<cctype> 4 #include<vector> 5 #include<cstring> 6 inline int getint() { 7 char ch; 8 while(!isdigit(ch=getchar())); 9 int x=ch^'0'; 10 while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0'); 11 return x; 12 } 13 const int N=501,M=20000,inf=0x7fffffff; 14 struct Edge { 15 int from,to,remain; 16 }; 17 Edge e[M<<1]; 18 std::vector<int> g[N]; 19 int sz=0; 20 inline void add_edge(const int u,const int v,const int w) { 21 e[sz]=(Edge){u,v,w}; 22 g[u].push_back(sz); 23 sz++; 24 } 25 int s,t; 26 int a[N],p[N]; 27 inline int Augment() { 28 memset(a,0,sizeof a); 29 a[s]=inf; 30 std::queue<int> q; 31 q.push(s); 32 while(!q.empty()&&!a[t]) { 33 int x=q.front(); 34 q.pop(); 35 for(unsigned i=0;i<g[x].size();i++) { 36 Edge &y=e[g[x][i]]; 37 if(!a[y.to]&&y.remain) { 38 p[y.to]=g[x][i]; 39 a[y.to]=std::min(a[x],y.remain); 40 q.push(y.to); 41 } 42 } 43 } 44 return a[t]; 45 } 46 inline int EdmondsKarp() { 47 int maxflow=0; 48 while(int flow=Augment()) { 49 for(int i=t;i!=s;i=e[p[i]].from) { 50 e[p[i]].remain-=flow; 51 e[p[i]^1].remain+=flow; 52 } 53 maxflow+=flow; 54 } 55 return maxflow; 56 } 57 int main() { 58 int n=getint(),m=getint(); 59 s=1,t=n; 60 while(m--) { 61 int u=getint(),v=getint(),w=getint(); 62 add_edge(u,v,w); 63 add_edge(v,u,0); 64 } 65 printf("%d ",EdmondsKarp()); 66 return 0; 67 }