zoukankan      html  css  js  c++  java
  • 网络流24题(07)试题库问题(二分图多重匹配 + 最大流)

    题意:

    假设一个试题库中有 n 道试题。每道试题都标明了所属类别。同一道题可能有多个类别属性。

    现要从题库中抽取 m 道题组成试卷。并要求试卷包含指定类型的试题。试设计一个满足要求的组卷算法。

    思路:

    1. 试卷和属性分别定义为 X, Y 集。每个试卷有多重属性,则由试卷分别向属性引弧,容量为 1,s 向 X 引弧容量为 1,Y 向 t 引弧,容量为需要的数量;

    2. 求上面二分图的最大流即可。如果最大流等于需要选择出来的总试题数 m,则存在方案,输出即可;否而输出 No solution.

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <vector>
    using namespace std;
    
    const int MAXN = 1050;
    const int INFS = 0x3FFFFFFF;
    
    struct edge {
        int from, to, cap, flow;
        edge(int _from, int _to, int _cap, int _flow) 
            : from(_from), to(_to), cap(_cap), flow(_flow) {}
    };
    
    class Dinic {
    public:
        void initdata(int n, int s, int t) {
            this->n = n, this->s = s, this->t = t;
            edges.clear();
            for (int i = 0; i < n; i++)
                G[i].clear();
        }
        void addedge(int u, int v, int cap) {
            edges.push_back(edge(u, v, cap, 0));
            edges.push_back(edge(v, u, 0, 0));
            G[u].push_back(edges.size() - 2);
            G[v].push_back(edges.size() - 1);
        }
        bool BFS() {
            for (int i = 0; i < n; i++)
                vis[i] = false, d[i] = 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 = edges[G[x][i]];
                    if (e.cap > e.flow && !vis[e.to]) {
                        vis[e.to] = true;
                        d[e.to] = d[x] + 1;
                        Q.push(e.to);
                    }
                }
            }
            return vis[t];
        }
        int DFS(int x, int aug) {
            if (x == t || aug == 0) return aug;
            int flow = 0;
            for (int i = 0; i < G[x].size(); i++) {
                edge& e = edges[G[x][i]];
                if (d[e.to] == d[x] + 1) {
                    int f = DFS(e.to, min(aug, e.cap - e.flow));
                    if (f <= 0) continue;
                    e.flow += f;
                    edges[G[x][i]^1].flow -= f;
                    flow += f;
                    aug -= f;
                    if (aug == 0) break;
                } 
            }
            return flow;
        }
        int maxflow() {
            int flow = 0;
            while (BFS()) {
                flow += DFS(s, INFS);
            }
            return flow;
        }
        void getans(int p, vector<int>& ans) {
            ans.clear();
            for (int i = 0; i < G[p].size(); i++) {
                edge& e = edges[G[p][i]^1];
                if (e.to == p && e.flow == 1)
                    ans.push_back(e.from);
            }
            sort(ans.begin(), ans.end());
        }
    private:
        vector<edge> edges;
        vector<int> G[MAXN];
        int n, s, t, d[MAXN];
        bool vis[MAXN];
    };
    
    Dinic dc;
    
    int main() {
        int k, n;
        scanf("%d%d", &k, &n);
        
        int s = 0, t = k + n + 1;
        dc.initdata(t + 1, s, t);
    
        int sum = 0;
        for (int i = 1; i <= k; i++) {
            int a;
            scanf("%d", &a);
            sum += a;
            dc.addedge(i + n, t, a);
        }
        for (int i = 1; i <= n; i++) {
            int p, a;
            scanf("%d", &p);
            for (int j = 0; j < p; j++) {
                scanf("%d", &a);
                dc.addedge(i, a + n, 1);
            }
            dc.addedge(s, i, 1);
        }
        int flow = dc.maxflow();
        if (flow == sum) {
            vector<int> ans;
            for (int i = 1; i <= k; i++) {
                dc.getans(i + n, ans);
                printf("%d:", i);
                for (int j = 0; j < ans.size(); j++)
                    printf(" %d", ans[j]);
                printf("\n");
            }
        } else  printf("No Solution!\n");
    
        return 0;
    }
  • 相关阅读:
    高斯消元
    Luogu P2068 统计和
    Luogu P1892 [BOI2003]团伙
    Luogu P2866 [USACO06NOV]糟糕的一天Bad Hair Day
    Luogu P3916 图的遍历
    Luogu P1041 [2003NOIP提高组]传染病控制
    Luogu P3901 数列找不同
    Luogu 2951 捉迷藏Hide and Seek
    Luogu P1550 打井Watering Hole
    洛谷——P1044 栈
  • 原文地址:https://www.cnblogs.com/kedebug/p/3031131.html
Copyright © 2011-2022 走看看