題目描述 Description
假設一個試題庫中有 n 道試題。每道試題都标明了所屬類别。同一道題可能有多個類别屬性。現要從題庫中抽取 m 道題組成試卷。并要求試卷包含指定類型的試題。試設計一個滿足要求的組卷算法。
對于給定的組卷要求,計算滿足要求的組卷方案。
輸入描述 Input Description
第1行有2個正整數n和k (2 <=k<= 20, k<=n<= 1000) k 表示題庫中試題類型總數,n 表示題庫中試題總數。第 2 行有 k 個正整數,第 i 個正整數表示要選出的類型 i 的題數。這 k 個數相加就是要選出的總題數 m。接下來的 n 行給出了題庫中每個試題的類型資訊。每行的第 1 個正整數 p 表明該題可以屬于 p 類,接着的 p 個數是該題所屬的類型号。
輸出描述 Output Description
第 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
題目可以再這裡送出:http://wikioi.com/homework/23/
分析:二分多重比對,跑完最大流後,從X集合出發,終點在Y集合,且滿流的邊就是一個可行解。
代碼:
//Isap算法,複雜度O(n^2m)
#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll; //記得必要的時候改成無符号
const int maxn=505;
const int maxm=1000005;
const int INF=1000000000;
struct EdgeNode
{
int from;
int to;
int cost;
int next;
}edge[maxm];
int head[maxn],cnt;
void add(int x,int y,int z)
{
edge[cnt].from=x;edge[cnt].to=y;edge[cnt].cost=z;edge[cnt].next=head[x];head[x]=cnt++;
edge[cnt].from=y;edge[cnt].to=x;edge[cnt].cost=0;edge[cnt].next=head[y];head[y]=cnt++;
}
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
int S,T,n,m;
int d[maxn],gap[maxn],curedge[maxn],pre[maxn];
//curedge[]為目前弧數組,pre為前驅數組
int sap(int S,int T,int n) //n為點數
{
int cur_flow,flow_ans=0,u,tmp,neck,i;
memset(d,0,sizeof(d));
memset(gap,0,sizeof(gap));
memset(pre,-1,sizeof(pre));
for(i=0;i<=n;i++)curedge[i]=head[i]; //初始化目前弧為第一條鄰接表
gap[0]=n;
u=S;
while(d[S]<n) //當d[S]>=n時,網絡中肯定出現了斷層
{
if(u==T)
{
cur_flow=INF;
for(i=S;i!=T;i=edge[curedge[i]].to)
{ //增廣成功,尋找瓶頸邊
if(cur_flow>edge[curedge[i]].cost)
{
neck=i;
cur_flow=edge[curedge[i]].cost;
}
}
for(i=S;i!=T;i=edge[curedge[i]].to)
{ //修改路徑上的邊容量
tmp=curedge[i];
edge[tmp].cost-=cur_flow;
edge[tmp^1].cost+=cur_flow;
}
flow_ans+=cur_flow;
u=neck; //下次增廣從瓶頸邊開始
}
for(i=curedge[u];i!=-1;i=edge[i].next)
if(edge[i].cost&&d[u]==d[edge[i].to]+1)
break;
if(i!=-1)
{
curedge[u]=i;
pre[edge[i].to]=u;
u=edge[i].to;
}
else
{
if(0==--gap[d[u]])break; //gap優化
curedge[u]=head[u];
for(tmp=n,i=head[u];i!=-1;i=edge[i].next)
if(edge[i].cost)
tmp=min(tmp,d[edge[i].to]);
d[u]=tmp+1;
++gap[d[u]];
if(u!=S)u=pre[u]; //重标号并且從目前點前驅重新增廣
}
}
return flow_ans;
}
vector<int>V[maxn];
int main()
{
int x,y,i,j,sum;
while(~scanf("%d%d",&n,&m))
{
init(); S=0; T=n+m+1; sum=0;
for(i=1;i<=n;i++){
scanf("%d",&x); sum+=x;
add(m+i,T,x);
}
for(i=1;i<=m;i++){
add(S,i,1);
scanf("%d",&x);
while(x--){
scanf("%d",&y);
add(i,y+m,1);
}
}
if(sum!=sap(S,T,T+1))printf("No Solution!\n");
else{
for(i=1;i<=n;i++)V[i].clear();
for(i=0;i<cnt;i++){
x=edge[i].from; y=edge[i].to;
if(x>=1&&x<=m&&y>m&&y<=n+m&&edge[i].cost==0)
V[y-m].push_back(x);
}
for(i=1;i<=n;i++){
printf("%d:",i);
for(j=0;j<V[i].size();j++){
printf(" %d",V[i][j]);
}
printf("\n");
}
}
}
return 0;
}