首页 > 其他 > 详细

洛谷P2763 试题库问题(最大流)

时间:2018-07-25 15:39:10      阅读:193      评论:0      收藏:0      [点我收藏+]

题意

$n$道试题,每道题有多种类别属性

抽取$m$道题组成试卷,要求包含指定的类型

输出方案

Sol

又是一道zz网络流

我的构图长这样,$k_i$表示第$i$道试题需要的数量

技术分享图片

 

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN = 1e5 + 10, INF = 1e9 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < 0 || c > 9) {if(c == -) f = -1; c = getchar();}
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = getchar();
    return x * f;
}
int K, N, S, T;
int need[MAXN];
vector<int>ans[MAXN];
struct Edge {
    int u, v, f, nxt;
}E[MAXN];
int head[MAXN], cur[MAXN], num;
inline void add_edge(int x, int y, int f) {
    E[num] = (Edge){x, y, f, head[x]};
    head[x] = num++;
}
inline void AddEdge(int x, int y, int z) {
    add_edge(x, y, z);
    add_edge(y, x, 0);
}
int sum = 0, deep[MAXN];
bool BFS() {
    queue<int> q; q.push(S);
    memset(deep, 0, sizeof(deep)); deep[S] = 1;
    while(!q.empty()) {
        int p = q.front(); q.pop();
        for(int i = head[p]; i != -1; i = E[i].nxt) {
            int to = E[i].v;
            if(!deep[to] && E[i].f) {
                deep[to] = deep[p] + 1;
                q.push(to);
            }
        }
    }
    return deep[T] > 0;
}
int DFS(int x, int flow) {
    if(x == T) return flow;
    int ansflow = 0;
    for(int &i = cur[x]; i != -1; i = E[i].nxt) {
        int to = E[i].v;
        if(deep[to] == deep[x] + 1 && E[i].f) {
            int nowflow = DFS(to, min(flow, E[i].f));
            E[i].f -= nowflow; E[i ^ 1].f += nowflow;
            ansflow += nowflow; flow -= nowflow;
            if(flow <= 0) break;
        }
    }
    return ansflow;
}
int Dinic() {
    int ans = 0;
    while(BFS()) {
        memcpy(cur, head, sizeof(head));
        ans += DFS(S, INF);
    }
    return ans;
}
int main() {
    memset(head, -1, sizeof(head));
    K = read(); N = read(); S = 0; T = N + K + 1;
    int sum = 0;
    for(int i = 1; i <= K; i++) {
        int x = read(); sum += x;
        AddEdge(N + i, T, x);
    }
    for(int i = 1; i <= N; i++) {
        int n = read();
        AddEdge(S, i, 1);
        for(int j = 1; j <= n; j++) {
            AddEdge(i, read() + N, 1);
        }
    }
    if(Dinic() < sum) {puts("No Solution!"); return 0;}
    for(int x = 1; x <= N; x++) 
        for(int i = head[x]; i != -1; i = E[i].nxt) 
            if(E[i].f == 0) 
                ans[E[i].v - N].push_back(x);
    for(int i = 1; i <= K; i++) {
        printf("%d: ", i);
        for(int j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j]);
        puts("");
    }
    return 0;
}

 

洛谷P2763 试题库问题(最大流)

原文:https://www.cnblogs.com/zwfymqz/p/9366061.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!