zoukankan      html  css  js  c++  java
  • luogu P2763 试题库问题

    本题可以用最大流也可以用最大匹配(本质一样),用dinic最大流好建图,但码量大,匈牙利码量小,建图费点劲。

    最大流:依旧是设一个源点一个汇点,对于每一个种类,连一条到汇点的边,capacity为需要的量,对于每一个试题,从源点连一条capacity为1的边到他,从他对每一个其所属的编号种类连一条capacity为1的边,求最大流即可,再找出最小割即可

    #include<bits/stdc++.h>
    using namespace std;
    #define lowbit(x) ((x)&(-x))
    typedef long long LL;
    
    const int maxm = 1e5+5;
    const int maxn = 1e3+33;
    const int INF = 0x3f3f3f3f;
    
    struct edge{
        int u, v, cap, flow, nex;
    } edges[maxm];
    
    int head[maxm], cur[maxm], cnt, level[maxn], capacity[23];
    vector<int> ans[23];
    
    void init() {
        memset(head, -1, sizeof(head));
    }
    
    void addedge(int u, int v, int cap) {
        edges[cnt] = edge{u, v, cap, 0, head[u]};
        head[u] = cnt++;
    }
    
    void bfs(int s) {
        memset(level, -1, sizeof(level));
        queue<int> q;
        level[s] = 0;
        q.push(s);
        while(!q.empty()) {
            int u = q.front();
            q.pop();
            for(int i = head[u]; i != -1; i = edges[i].nex) {
                edge& now = edges[i];
                if(now.cap > now.flow && level[now.v] < 0) {
                    level[now.v] = level[u] + 1;
                    q.push(now.v);
                }
            }
        }
    }
    
    int dfs(int u, int t, int f) {
        if(u == t) return f;
        for(int& i = cur[u]; i != -1; i = edges[i].nex) {
            edge& now = edges[i];
            if(now.cap > now.flow && level[u] < level[now.v]) {
                int d = dfs(now.v, t, min(f, now.cap - now.flow));
                if(d > 0) {
                    now.flow += d;
                    edges[i^1].flow -= d;
                    return d;
                }
    
            }
        }
        return 0;
    }
    
    int dinic(int s, int t) {
        int maxflow = 0;
        for(;;) {
            bfs(s);
            if(level[t] < 0) break;
            memcpy(cur, head, sizeof(head));
            int f;
            while((f = dfs(s, t, INF)) > 0)
                maxflow += f;
        }
        return maxflow;
    }
    
    void run_case() {
        int k, n, p, u, sum = 0;
        init();
        cin >> k >> n;
        int s = 0, t = n+k+1;
        for(int i = 1; i <= k; ++i) {
            cin >> capacity[i];
            sum += capacity[i];
            addedge(n+i, t, capacity[i]), addedge(t, n+i, 0);
        }
        for(int i = 1; i <= n; ++i) {
            cin >> p;
            addedge(s, i, 1), addedge(i, s, 0);
            while(p--) {
                cin >> u;            
                addedge(i, u+n, 1), addedge(u+n, i, 0);
            }
        }
        if(dinic(s, t) != sum) {cout << "No Solution!"; return;}
        for(int i = 1; i <= n; ++i) {
            for(int j = head[i]; j != -1; j = edges[j].nex) {
                if(edges[j].flow) {
                    ans[edges[j].v - n].push_back(i);
                    break;
                }
            }
        }
        for(int i = 1; i <= k; ++i) {
            cout << i << ":";
            for(auto j : ans[i])
                cout << " " << j;
            cout << "
    ";
        }
    }
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(0);
        run_case();
        //cout.flush();
        return 0;
    }
    Dinic

    匈牙利:对于每一个种类都连一条边到其所属的试卷,求最大匹配即可(无代码)

  • 相关阅读:
    myeclipse中如何自动生成hibernate得POJO和hbm.xml文件
    解决“List is a raw type. References to generic type List”提示的问题
    Struts2文件目录结构
    DropDownlist 选中
    js获取网站根路径(站点及虚拟目录)
    HTML 特殊字符全集
    抽象类和接口的区别
    有关onpropertychange事件
    JS 括号使用分析
    Field.Store ,ANALYZED备忘
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12274919.html
Copyright © 2011-2022 走看看