码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1149

时间:2015-06-09 15:36:43      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
#include<stdio.h>
#define MAXN 105
#include"queue"
#define inf 10000000
#include<stdio.h>
using namespace std;
int max_flow_ljz(int n,int * * mat,int source,int sink,int * * flow);
int main()
{
    //freopen("acm.acm","r",stdin);
    int n_pig;
    int n_cust;
    int n_key;
    int tem;
    int i;
    int j;
    int * pig_house;
    int * pig_h_c;
    int * * _m;
    int * * f;
    cin>>n_pig>>n_cust;
    _m = new int * [n_cust + 2];
    f = new int * [n_cust + 2];
    for(i = 0; i < n_cust + 2; ++ i)
    {
        _m[i] = new int[n_cust + 2];
        f[i] = new int[n_cust + 2];
        memset(_m[i],0,sizeof(int)*(n_cust + 2));
    }
    pig_house = new int[n_pig];
    pig_h_c = new int[n_pig];
    memset(pig_h_c,0,sizeof(int)*n_pig);
    for(i = 0; i < n_pig; ++ i)
    {
        cin>>tem;
        pig_house[i] = tem;
    }
    for(j = 1; j <= n_cust; ++ j)
    {
        cin>>n_key;
        for(i = 0; i < n_key; ++ i)
        {
            cin>>tem;
            if(pig_h_c[tem - 1] != 0)
                _m[pig_h_c[tem - 1]][j] = inf;
            else
            {
                _m[0][j] += pig_house[tem - 1];
                pig_h_c[tem - 1] = j;
            }
        }
        cin>>tem;
        _m[j][n_cust+1] = tem;
    }
    cout<<max_flow_ljz(n_cust+2,_m,0,n_cust+1,f);
}

int max_flow_ljz(int n,int * * mat,int source,int sink,int * * flow){
    int pre[MAXN],que[MAXN],d[MAXN],p,q,t,i,j;
    if (source==sink) return inf;
    for (i=0;i<n;i++)
        for (j=0;j<n;flow[i][j++]=0);
    for (;;){
        for (i=0;i<n;pre[i++]=0);
        pre[t=source]=source+1,d[t]=inf;
        for (p=q=0;p<=q&&!pre[sink];t=que[p++])
            for (i=0;i<n;i++)
                if (!pre[i]&&(j=mat[t][i]-flow[t][i]))
                    pre[que[q++]=i]=t+1,d[i]=d[t]<j?d[t]:j;
                else if (!pre[i]&&(j=flow[i][t]))
                    pre[que[q++]=i]=-t-1,d[i]=d[t]<j?d[t]:j;
        if (!pre[sink]) break;
        for (i=sink;i!=source;)
            if (pre[i]>0)
                flow[pre[i]-1][i]+=d[sink],i=pre[i]-1;
            else
                flow[i][-pre[i]-1]-=d[sink],i=-pre[i]-1;
    }
    for (j=i=0;i<n;j+=flow[source][i++]);
    return j;
}

 

POJ 1149

标签:

原文地址:http://www.cnblogs.com/gavinsp/p/4563310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!