h1532最大流

来源:互联网 发布:php extends 继承 编辑:程序博客网 时间:2024/06/11 12:44

居然一次就过咯

最基础的一道最大流


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int flow[300][300],a[300],pre[300];
int n,m,sum;
void Bflow()
{
    while(1)
    {
        memset(pre,-1,sizeof(pre));
        memset(a,0,sizeof(a));
        queue<int> q;
        q.push(1);
        a[1]=1000005;
        // printf("!");
        while(!q.empty())
        {


            int index=q.front();
            q.pop();
            if(index==m)
                break;
          //  printf("%d ",index);
            for(int i=1;i<=m;i++)
            {
                if(a[i]==0&&flow[index][i]>0)
                {
                    pre[i]=index;
                    q.push(i);
                    a[i]=min(a[index],flow[index][i]);
                }
            }
        }
        if(a[m]==0)
            break;
        int index=pre[m];
        int i=m;
       // printf("%d\n",a[m]);
        while(1)
        {
            flow[index][i]-=a[m];
            flow[i][index]+=a[m];
            if(index==1)
                break;
            i=index;
            index=pre[i];


        }
        //for(int i=1;i<=m;i++)
        //{
        //    for(int j=1;j<=m;j++)
        //       printf("%d ",flow[i][j]);
        //    printf("\n");
        //}
        sum+=a[m];
    }
}
int main()
{
    int a,b,c;
    while(~scanf("%d %d",&n,&m))
    {
        memset(flow,0,sizeof(flow));
        while(n--)
        {
            scanf("%d %d %d",&a,&b,&c);
            flow[a][b]+=c;
        }
        sum=0;
        Bflow();
        cout << sum << endl;
    }
    return 0;
}
/*
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
*/

0 0