COGS732. [Network Flow 24 questions] questions library

Source: Internet
Author: User

«Problem Description:
Suppose there is an n-question in a question bank. Each question is marked with the category. There may be multiple category properties for the same topic. Now from the question bank to extract the M-track composition test paper. The test paper is required to contain the specified type of question. Try to design a group volume algorithm that satisfies the requirement.
«Programming Tasks:
For a given set of volume requirements, calculate the group volume scheme that meets the requirements.
«Data Entry:
The input data is provided by the file testlib.in. The 1th line of the file has 2 positive integers k and n (2 <=k<=, k<=n<=) k indicates the total number of questions in the question bank, and N indicates the total number of questions in the question. The 2nd line has k positive integers, and the I positive integers represent the number of the type I to select. The sum of the K numbers is the total number of questions m to be selected. The next n lines give the type information of each question in the bank. The 1th positive integer p in each row indicates that the problem can belong to the P class, and then the number of P is the type number to which the problem belongs.
«Result Output:
Output the group volume scheme to file Testlib.out at the end of the program run. The first line of the file outputs "I:" followed by the number of type I. If there are multiple scenarios that meet the requirements, just output 1 scenarios. If the problem is not solved, output "nosolution!".
Input File Example
Testlib.in
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

Output File Example

Testlib.out

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

Maximum flow of network streams

If the maximum flow is equal to M, then there is a solution that iterates through all the sides of the question, and records the answer.

1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <vector>8#include <queue>9 using namespacestd;Ten Const intinf=1e9; One Const intmxn=1050; A intRead () { -     intx=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} the      while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -     returnx*F; - } - structedge{ +     intv,nxt,f; -}e[mxn* the]; + inthd[mxn],mct=1; A voidAdd_edge (intUintVintf) { ate[++mct].v=v;e[mct].nxt=hd[u];e[mct].f=f;hd[u]=MCT; -   -     return; - } - voidInsertintUintVintf) { -Add_edge (u,v,f); Add_edge (V,u,0);return; in } - intn,k,s,t; to intD[MXN]; + BOOLBFS () { -memset (D,0,sizeofd); thequeue<int>Q; * Q.push (S); $d[s]=1;Panax Notoginseng      while(!Q.empty ()) { -         intu=Q.front (); Q.pop (); the          for(intI=hd[u];i;i=e[i].nxt) { +             intv=e[i].v; A             if(!d[v] &&e[i].f) { thed[v]=d[u]+1; + Q.push (v); -             } $         } $     } -     returnD[t]; - } the intDFS (intUintLim) { -     if(u==t)returnLim;Wuyi     inttmp,f=0; the      for(intI=hd[u];i;i=e[i].nxt) { -         intv=e[i].v; Wu         if(d[v]==d[u]+1&&e[i].f) { -tmp=DFS (V,min (LIM,E[I].F)); Aboute[i].f-=tmp; $e[i^1].f+=tmp; -lim-=tmp; -f+=tmp; -             if(!lim)returnF; A         } +     } thed[u]=0; -     returnF; $ } the intDinic () { the     intres=0; the      while(BFS ()) res+=DFS (s,inf); the     returnRes; - } invector<int>Q[MXN]; the voidPrint () { the     inti,j; About      for(i=1; i<=k;i++){ the          for(j=hd[i];j;j=e[j].nxt) { the //printf ("u:%d v:%d f:%d\n", i,e[j].v,e[j].f); the             if(e[j^1].F) Q[i].push_back (e[j].v-k); +         } -     } the      for(i=1; i<=k;i++){Bayiprintf"%d:", i); the          for(j=0; J<q[i].size (); j + +){ theprintf"%d", Q[i][j]); -         } -printf"\ n"); the     } the     return; the } the intm=0; - intMain () { theFreopen ("testlib.in","R", stdin); theFreopen ("Testlib.out","W", stdout); the     inti,j;94K=read (); n=read (); thes=0; t=k+n+1; the     intx, y; the      for(i=1; i<=k;i++){98x=read (); About Insert (s,i,x); -m+=x;101     }102      for(i=1; i<=n;i++){103y=read ();104          for(j=1; j<=y;j++){ thex=read ();106Insert (X,i+k,1);107         }108     }109      for(i=1; i<=n;i++) Insert (i+k,t,inf); the     intans=dinic ();111     if(ans==m) Print (); the     Elseprintf"nosolution!\n");113     return 0; the}

COGS732. [Network Flow 24 questions] questions library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.