裸题不多说,在网络流的练习题里,你甚至可以使用暴力。
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; template<typename T>inline char MIN(T&A,T B){return A<B?A=B,1:0;} template<typename T>inline char MAX(T&A,T B){return A>B?A=B,1:0;} template<typename T>inline T _min(T A,T B){return A<B?A:B;} template<typename T>inline T _max(T A,T B){return A>B?A:B;} template<typename T>inline T read(T&x){ x=0;char c;while(!isdigit(c=getchar()))if(isalpha(c))return x=(int)c; while(isdigit(c))x=(x<<1)+(x<<3)+(c^48),c=getchar();return x; } const int N=1000+7,INF=0x3f3f3f3f; int w[N<<1],v[N<<1],Next[N<<1],Head[N],cur[N],dis[N],tot=1; inline void Addedge(int x,int y,int z){ v[++tot]=y,Next[tot]=Head[x],Head[x]=tot,w[tot]=z; v[++tot]=x,Next[tot]=Head[y],Head[y]=tot,w[tot]=0; } #define y v[j] inline char bfs(){ queue<int> q;memset(dis,0,sizeof dis),dis['A']=1,q.push('A'); while(!q.empty()){ int x=q.front();q.pop(); for(register int j=Head[x];j;j=Next[j])if(w[j]&&!dis[y]){ dis[y]=dis[x]+1,q.push(y); if(y=='Z')return 1; } } return 0; } int dinic(int x,int flow){ if(x=='Z'||!flow)return flow; int rest=flow,k; for(register int j=cur[x];j&&rest;cur[x]=j,j=Next[j])if(w[j]&&dis[y]==dis[x]+1){ if(!(k=dinic(y,_min(rest,w[j]))))dis[y]=0; rest-=k,w[j]-=k,w[j^1]+=k; } return flow-rest; } #undef y int m,x,y,z,ans; int main(){//freopen("tmp.in","r",stdin);freopen("tmp.out","w",stdout); read(m); for(register int i=1;i<=m;++i)read(x),read(y),read(z),Addedge(x,y,z); while(bfs()){ for(register int i='A';i<='z';++i)cur[i]=Head[i]; ans+=dinic('A',INF); } printf("%d ",ans); return 0; }