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

POJ 3281 Dining 最大流

时间:2014-05-08 14:26:15      阅读:423      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

饮料->牛->食物。

牛拆成两点。

bubuko.com,布布扣
bubuko.com,布布扣
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c)
{
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c)
{
    return max(max(a,b),max(a,c));
}
void debug()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in","r",stdin);
    // freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch()
{
    int ch;
    while((ch=getchar())!=EOF)
    {
        if(ch!= &&ch!=\n)return ch;
    }
    return EOF;
}

struct Edge
{
    int to,cap;
};
const int maxn = 110 * 4;
vector<int> g[maxn];
vector<Edge> edge;
int n,s,t;
int N, F, D;
int d[maxn];
int vis[maxn];
int cur[maxn];

void add(int u,int v,int cap)
{
    //printf("%d -> %d : %d\n", u, v, cap);
    edge.push_back((Edge){v, cap});
    g[u].push_back(edge.size() - 1);
    edge.push_back((Edge){u, 0});
    g[v].push_back(edge.size() - 1);
}
void build()
{
    for(int i = 1; i <= n; i++)
        g[i].clear();
    edge.clear();
    for(int i = 1; i <= F; i++)
        add(s,i,1);
    for(int i = F + 1; i <= F + N; i++)
        add(i, i + N, 1);
    for(int i = 1 + F + 2*N; i < 1 + F + 2*N + D; i++)
        add(i, t, 1);
    for(int i = 1; i <= N; i++)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        for(int j = 1; j <= a; j++)
        {
            int id;
            scanf("%d", &id);
            add(id, i + F, 1);
        }
        for(int j = 1; j <= b; j++)
        {
            int id;
            scanf("%d", &id);
            add(i + N + F, id + N*2 + F, 1);
        }
    }
}

bool bfs()
{
    memset(vis, 0, sizeof(vis));
    d[s] = 0;
    queue<int> q;
    q.push(s);
    vis[s] = true;
    while(!q.empty())
    {
        int x = q.front(); q.pop();
        for (int i = 0; i < g[x].size(); i++)
        {
            Edge &e = edge[g[x][i]];
            if(e.cap>0 && !vis[e.to])
            {
                vis[e.to] = true;
                q.push(e.to);
                d[e.to] = d[x] + 1;
            }
        }
    }
    return vis[t];
}

int dfs(int u,int f)
{
    if(u==t || f==0) return f;
    for(int &i = cur[u]; i < g[u].size(); i++)
    {
        Edge &e = edge[g[u][i]];
        if(e.cap>0 && d[e.to] == d[u] + 1)
        {
            int d = dfs(e.to, min(f,e.cap));
            if(d>0)
            {
                e.cap -= d;
                edge[g[u][i]^1].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow()
{
    int res = 0;
    while(bfs())
    {
        memset(cur, 0, sizeof(cur));
        int d;
        while(d=dfs(s,INF))
        {
            res += d;
        }
    }
    return res;
}
int main()
{
    debug();
    while(scanf("%d%d%d", &N, &F, &D) != EOF)
    {
        n = N*2 + F + D + 2;
        s = n-1; t = n;
        build();
        printf("%d\n", max_flow());
    }
    return 0;
}
View Code
bubuko.com,布布扣

 

POJ 3281 Dining 最大流,布布扣,bubuko.com

POJ 3281 Dining 最大流

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/BMan/p/3713695.html

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