zoukankan      html  css  js  c++  java
  • [网络流24题]试题库问题

    Description

    假设一个试题库中有$n$道试题,每道试题都标明了所属类别,同一道题可能有多个类别属性。现要从题库中抽取$m$道题组成试卷,并要求试卷包含指定类型的试题。求一个满足要求的组卷方案。

    Input

    第$1$行有$2$个正整数$n,k,k$表示题库中试题类型总数,$n$表示题库中试题总数。

    第$2$行有$k$个正整数,第$i$个正整数表示要选出的类型$i$的题数$a_i$。这$k$个数相加就是要选出的总题数$m$。

    接下来的$n$行给出了题库中每个试题的类型信息。每行的第$1$个正整数$p$表明该题可以属于$p$类,接着的$p$个数是该题所属的类型号。

    Output

    第$i$行输出$"i:"$后接类型$i$的题号。如果有多个满足要求的方案,只要输出$1$个方案。

    如果问题无解,则输出“$No;;Solution!$”

    Sample Input

    3 15
    3 3 4
    2 1 2
    1 3
    1 3
    1 3
    1 3
    3 1 2 3
    2 2 3
    2 1 3
    1 2
    1 2
    2 1 2
    2 1 3
    2 1 2
    1 1
    3 1 2 3

    Sample Output

    1: 1 6 8
    2: 7 9 10
    3: 2 3 4 5

    HINT

    $2;leq;k;leq;20,k;leq;n;leq;1000$

    Solution

    类别$i$为$x_i$,题$i$为$y_i$.

    从$s$向$x_i$连接一条容量为$a_i$的有向边,

    从$x_i$向$y_j$连接一条容量为$1$的有向边(题$jin$类别$i$),

    从$y_i$向$t$连接一条容量为$1$的有向边,

    求最大流.

    如果最大流$=m$,则存在解,否则无解。

    从集合$x$流向集合$y$的所有满流边为当前方案.

    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<stack>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define M 25
    #define N 1005
    using namespace std;
    struct graph{
        int nxt,to,f;
    }e[M*N<<1];
    int a[M],g[N+M],dep[N+M],n,m,s,t,sum,cnt=1;
    queue<int> q;
    inline void addedge(int x,int y,int f){
        e[++cnt].nxt=g[x];g[x]=cnt;e[cnt].to=y;e[cnt].f=f;
    }
    inline void adde(int x,int y,int f){
        addedge(x,y,f);addedge(y,x,0);
    }
    inline bool bfs(int u){
        memset(dep,0,sizeof(dep));
        q.push(u);dep[u]=1;
        while(!q.empty()){
            u=q.front();q.pop();
            for(int i=g[u];i;i=e[i].nxt)
                if(e[i].f>0&&!dep[e[i].to]){
                    q.push(e[i].to);
                    dep[e[i].to]=dep[u]+1;
                }
        }
        return dep[t];
    }
    inline int dfs(int u,int f){
        int ret=0;
        if(u==t) return f;
        for(int i=g[u],d;i&&f;i=e[i].nxt)
            if(e[i].f>0&&dep[e[i].to]>dep[u]){
                d=dfs(e[i].to,min(f,e[i].f));
                ret+=d;f-=d;e[i].f-=d;e[i^1].f+=d;
            }
        return ret;
    }
    inline int dinic(){
        int ret=0;
        while(true){
            if(!bfs(s)) return ret;
            ret+=dfs(s,M);
        }
    }
    inline void Aireen(){
        scanf("%d%d",&m,&n);
        s=m+n+1;t=s+1;
        for(int i=1;i<=m;++i){
            scanf("%d",&a[i]);
            adde(s,i,a[i]);sum+=a[i];
        }
        for(int i=1,j,k;i<=n;++i){
            scanf("%d",&k);
            while(k--){
                scanf("%d",&j);adde(j,i+m,1);
            }
            adde(i+m,t,1);
        }
        if(dinic()!=sum) puts("No Solution!");
        else{
            for(int i=1;i<=m;++i){
                printf("%d:",i);
                for(int j=g[i];j;j=e[j].nxt)
                    if(!e[j].f&&!(j&1))
                        printf("%d ",e[j].to-m);
                printf("
    ");
            }    
        }
    }
    int main(){
        freopen("data.in","r",stdin);
        freopen("data.out","w",stdout);
        Aireen();
        fclose(stdin);
        fclose(stdout);
        return 0;
    }
  • 相关阅读:
    Spring4
    Mysql索引优化、JMM内存模型
    深入浅出JVM
    ZGC垃圾搜集器
    Hibernate4基础
    红楼梦 各版本及资料
    JavaME环境配置
    5.1 Intent
    4.3 异步任务
    4.2.2 网络编程之Socket
  • 原文地址:https://www.cnblogs.com/AireenYe/p/6240873.html
Copyright © 2011-2022 走看看