题解:

双倍经验

dinic模板题

然后ff过不了。。。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=405;
int q[N*N],n,m,sum,dis[N],x,y,ans,f[N],z,a[N][N];
int bfs()
{
    memset(dis,0xff,sizeof dis);
    dis[1]=0;
    int l=0,r=1;
    q[1]=1;
    while (l<r)
     {
         int j=q[++l];
         for (int i=1;i<=n;i++)
          if (dis[i]<0&&a[j][i]>0)
           {
               dis[i]=dis[j]+1;
               q[++r]=i;
           }
     }
    if (dis[n]>0)return 1;
    return 0; 
}
int find(int x,int low)
{
    int b=0;
    if (x==n)return low;
    for (int i=1;i<=n;i++)
     if (a[x][i]>0&&dis[i]==dis[x]+1&&(b=find(i,min(low,a[x][i]))))
      {
          a[x][i]-=b;
          a[i][x]+=b;
          return b;
      }
    return 0;  
}
int main()
{
    while (~scanf("%d%d",&m,&n))
     {
         memset(a,0,sizeof a);
        while (m--)
          {
             scanf("%d%d%d",&x,&y,&z);
             a[x][y]+=z;
         }
        ans=0;int t; 
        while (bfs())
         while (t=find(1,0x7fffffff))
          ans+=t;
        printf("%d\n",ans); 
     }
}

 

posted on 2018-01-21 19:28  宣毅鸣  阅读(102)  评论(0编辑  收藏  举报