1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
   | #include<cstdio> #include<queue> #include<algorithm> using namespace std; typedef long long ll; int n,m,p=1,t[10001],f[10001]; struct str {     int m,q;     ll w; }a[10000001]; void road(int x,int y,ll w) {     a[++p].m=y;     a[p].w=w;     a[p].q=t[x];     t[x]=p; } bool bfs() {     queue<int> q;     q.push(1);     for(int i=1;i<=n;++i) f[i]=0;     f[1]=1;     while(!q.empty())     {         int k=q.front();         q.pop();         for(int i=t[k];i!=0;i=a[i].q)         {             if(f[a[i].m]==0&&a[i].w>0)             {                 f[a[i].m]=f[k]+1;                 q.push(a[i].m);             }         }     }     if(f[n]!=0) return true;     return false; } ll dfs(int x,ll w) {     if(x==n) return w;     ll v=0;     for(int i=t[x];i!=0&&w!=0;i=a[i].q)     {         if(f[a[i].m]==f[x]+1&&a[i].w>0)         {             ll z=dfs(a[i].m,min(w,a[i].w));             a[i].w-=z;             a[i^1].w+=z;             w-=z;             v+=z;         }     }     if(v==0) f[x]=0;     return v; } int main() {     scanf("%d%d",&m,&n);     for(int i=1;i<=m;++i)     {         int x,y;         ll w;         scanf("%d%d%lld",&x,&y,&w);         road(x,y,w);         road(y,x,0);     }     ll w=0;     while(bfs()==true)     {         w+=dfs(1,1e18);     }     printf("%lld",w);     return 0; }
   |