zoukankan      html  css  js  c++  java
  • luogu P1231 教辅的组成

    二次联通门 : luogu P1231 教辅的组成

    /*
        luogu P1231 教辅的组成
        
        拆点 + 最大流
        
        把书拆点后与答案与资料连边
        跑最大流 
    */
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    
    #define Max 50005
    #define INF 1e7
    
    using namespace std;
    
    void read (int &now)
    {
        now = 0;
        char word = getchar ();
        while (word > '9' || word < '0')
            word = getchar ();
        while (word >= '0' && word <= '9')
        {
            now = now * 10 + word - '0';
            word = getchar ();
        }
    }
    
    struct Edge
    {
        int to;
        int next;
        int flow;
    }edge[Max << 3];
    
    int Edge_Count = 1;
    int edge_list[Max];
    
    inline void AddEdge (int from, int to)
    {
        Edge_Count++;
        edge[Edge_Count].flow = 1;
        edge[Edge_Count].to = to;
        edge[Edge_Count].next = edge_list[from];
        edge_list[from] = Edge_Count;
        Edge_Count++;
        edge[Edge_Count].to = from;
        edge[Edge_Count].flow = 0;
        edge[Edge_Count].next = edge_list[to];
        edge_list[to] = Edge_Count;
    }
    
    int N, M, Q;
    int E;
    int S, T;
    int deep[Max];
    int Answer;
    
    int Flowing (int now, int flow)
    {
        if (flow <= 0 || now == T)
            return flow;
        int pos = 0, res = 0;
        for (int i = edge_list[now]; i; i = edge[i].next)
        {
            if (deep[edge[i].to] != deep[now] + 1 || edge[i].flow <= 0)
                continue;
            pos = Flowing (edge[i].to, min (edge[i].flow, flow));
            flow -= pos;
            res += pos;
            edge[i].flow -= pos;
            edge[i ^ 1].flow += pos;
            if (flow <= 0)
                return res;
        }
        return res;
    }
    
    int main (int argc, char *argv[])
    {
        read (N);
        read (M);
        read (Q);
        read (E);
        M += N;
        Q += M;
        S = Max - 2;
        T = Max - 3;
        int x, y;
        for (int i = 1; i <= N; i++)
            AddEdge (i, i + Q);
        for (int i = N + 1; i <= M; i++)
            AddEdge (S, i);
        for (int i = M + 1; i <= Q; i++)
            AddEdge (i, T);
        for (int i = 1; i <= E; i++)
        {
            read (x);
            read (y);
            AddEdge (y + N, x);
        }
        read (E);
        for (int i = 1; i <= E; i++)
        {
            read (x);
            read (y);
            AddEdge (x + Q, y + M);
        }
        while (true)
        {
            bool flag = false;
            memset (deep, -1, sizeof deep);
            queue <int> Queue;
            Queue.push (S);
            deep[S] = 0;
            int now;
            while (!Queue.empty ())
            {
                now = Queue.front ();
                Queue.pop ();
                for (int i = edge_list[now]; i; i = edge[i].next)
                    if (deep[edge[i].to] < 0 && edge[i].flow)
                    {
                        deep[edge[i].to] = deep[now] + 1;
                        if (edge[i].to == T)
                        {
                            flag = true;
                            break;
                        }
                        Queue.push (edge[i].to); 
                    }
                if (flag)
                    break;
            }
            if (deep[T] < 0)
                break;
            Answer += Flowing (S, INF);
        }
        printf ("%d", Answer);
        return 0;
    }
  • 相关阅读:
    php操作zip压缩文件
    php图像处理(thinkphp框架有相对强大的图像处理功能)
    php实现希尔排序(总结)
    PHP glob() 函数详解
    php资源类型变量
    dump var_dump print print_r的区别
    php实现遍历文件目录
    php常用函数
    php全局变量的使用
    php函数按地址传递参数(php引用)
  • 原文地址:https://www.cnblogs.com/ZlycerQan/p/6882689.html
Copyright © 2011-2022 走看看