1742: 试题库问题——二分图多重匹配

网友投稿 906 2022-09-04

1742: 试题库问题——二分图多重匹配

1742: 试题库问题——二分图多重匹配

题意:假设一个试题库中有n道试题。每道试题都标明了所属类别。同一道题可能有多个类别 属性。现要从题库中抽取m 道题组成试卷。并要求试卷包含指定类型的试题。试设计一个 满足要求的组卷算法。 编程任务: 对于给定的组卷要求,计算满足要求的组卷方案。

思路:建图直接跑就行,水题

#include #include #include #include #include #include #include using namespace std;const int maxn = 1000;const int INF = 0x3f3f3f3f;struct Edge { int from, to, cap, flow, flag;};struct Dinic { int s, t; vector edges; vector G[maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn]; void init() { edges.clear(); for (int i = 0; i < maxn; i++) G[i].clear(); } void addedge(int from, int to, int cap, int flag) { edges.push_back(Edge{from, to, cap, 0, flag}); edges.push_back(Edge{to, from, 0, 0, flag}); int m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool bfs() { memset(vis, 0, sizeof(vis)); queue q; q.push(s); d[s] = 0; vis[s] = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < G[x].size(); i++) { Edge &e = edges[G[x][i]]; if (!vis[e.to] && e.cap > e.flow) { vis[e.to] = 1; d[e.to] = d[x] + 1; q.push(e.to); } } } return vis[t]; } int dfs(int x, int a) { if (x == t || a == 0) return a; int flow = 0, f; for (int &i = cur[x]; i < G[x].size(); i++) { Edge &e = edges[G[x][i]]; if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[G[x][i]^1].flow -= f; flow += f; a -= f; if (a == 0) break; } } return flow; } int maxflow(int s, int t) { this->s = s, this->t = t; int flow = 0; while (bfs()) { memset(cur, 0, sizeof(cur)); flow += dfs(s, INF); } return flow; }}ac;vector G[maxn];int main() { int k, n; scanf("%d%d", &k, &n); int s = 0, t = k + n + 1; int x, sum = 0; for (int i = 1; i <= k; i++) { scanf("%d", &x); sum += x; ac.addedge(s, i, x, 0); } int p; for (int i = 1; i <= n; i++) { scanf("%d", &p); int t; for (int j = 1; j <= p; j++) { scanf("%d", &t); ac.addedge(t, i+k, 1, 1); } } for (int i = 1; i <= n; i++) { ac.addedge(i+k, t, 1, 0); } int ans = ac.maxflow(s, t); if (ans == sum) { for (int i = 0; i < ac.edges.size(); i++) { if (ac.edges[i].flag == 0 || ac.edges[i].flow == 0) continue; int u = ac.edges[i].from, v = ac.edges[i].to; G[u].push_back(v-k); } for (int i = 1; i <= k; i++) { printf("%d:", i); for (int j = 0; j < G[i].size(); j++) printf(" %d", G[i][j]); printf("\n"); } } else { printf("No Solution!\n"); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Nginx+PHP+FastCGI加速模式(nginx和fastcgi)
下一篇:POJ 2528 Mayor's posters——线段树+离散化
相关文章

 发表评论

暂时没有评论,来抢沙发吧~