POJ 3281 — Dining
来源:互联网 发布:推荐好的网络公开课 编辑:程序博客网 时间:2024/05/19 07:27
原题:http://poj.org/problem?id=3281
题意:有N头牛,F种食物,D种饮料;每种食物或者饮料只能供一头牛享用,且每头牛只享受一种食物和一种饮料;
接下来有N行,前面两个数f, d分别表示第i头牛喜欢的食物有f种,饮料有d种,接下来f个数和d个数为具体喜欢种类;
问最多能满足几头牛的需求;
思路:
将N头牛拆点为N1和N2,先让源点和食物相连,再让食物和N1相连,N1和N2相连,N2再和饮料相连;
建图 ——
源点s(编号0),F种食物(编号2*N+1到2*N+F), N1(编号1到N),
N2(编号1+N到N+N), D种饮料(编号2*N+F+1到2*N+F+D), 汇点t(编号2*N+F+D+1);
源点和食物之间的边为(s, 2*N+i, 1);食物和N1之间(2*N+x, i, 1);
N1和N2之间的边为(i, i+N, 1);N2和饮料之间(i+N, 2*N+F+x, 1);
饮料和汇点之间的边为(2*N+F+i, t,, 1);
#include<stdio.h>#include<string.h>#include<vector>#include<queue>#include<iostream>#include<algorithm>using namespace std;#define inf 1e9const int maxn = 1100;struct Edge{ int from, to, cap, flow; Edge(){} Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}};int head[maxn], edgenum;struct Dinic{ int n, m, s, t; vector<Edge>edges; vector<int>G[maxn]; bool vis[maxn]; int cur[maxn]; int d[maxn]; void init(int n, int s, int t) { this->n = n, this->s = s, this->t = t; edges.clear(); for(int i = 0;i<n;i++) G[i].clear(); } void add(int from, int to, int cap) { edges.push_back( Edge(from, to, cap, 0) ); edges.push_back( Edge(to, from, 0, 0) ); m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BFS() { queue<int>q; memset(vis, false, sizeof(vis)); vis[s] = true; d[s] = 0; q.push(s); 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] = true; 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[e.to] == d[x]+1 && (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 ans = 0; while(BFS()) { memset(cur, 0, sizeof(cur)); ans+=DFS(s, inf); } return ans; }}DC;int main(){int N, F, D;while(scanf("%d%d%d", &N, &F, &D)!=EOF){memset(head, -1, sizeof(head));edgenum = 0;int s = 0, t = 2*N+F+D+1;DC.init(2*N+F+D+2, s, t);for(int i = 1;i<=F;i++)DC.add(s, 2*N+i, 1);for(int i = 1;i<=D;i++)DC.add(2*N+F+i, t, 1);for(int i = 1;i<=N;i++)DC.add(i, i+N, 1);for(int i = 1;i<=N;i++){int f, d;scanf("%d%d", &f, &d);while(f--){int x;scanf("%d", &x);DC.add(2*N+x, i, 1);}while(d--){int x;scanf("%d", &x);DC.add(i+N, 2*N+F+x, 1);}}printf("%d\n", DC.maxflow());}return 0;}
0 0
- POJ 3281 — Dining
- POJ 3281 —— Dining
- poj 3281 Dining Maxflow
- POJ 3281 Dining
- poj 3281 Dining //SAP
- POJ 3281 Dining
- poj 3281 Dining
- poj 3281 Dining
- POJ 3281 Dining
- POJ 3281 Dining
- poj 3281 Dining
- POJ-3281-Dining
- poj(3281)Dining
- POJ 3281 Dining
- poj 3281 Dining
- poj 3281 Dining
- POJ 3281 Dining
- POJ 3281 Dining
- 【使用JBoss 7开发Java EE 6】EJB引用第三方包
- Java知识(009)--奥特曼打小怪兽
- thinkphp下的RABC 实现——2015/5/7
- 第三章第二十六题
- 编程思想之消息机制
- POJ 3281 — Dining
- window.event对象详细介绍
- linux 高级IO 磁盘映射IO
- springMVC一路总结(一)
- Linux 进程的 Uninterruptible sleep(D) 状态
- linux 线程 pthread
- ADT 下编译cocos2dx项目自己创建的项目报extra qualification错误
- 清明过了又五一,太匆匆
- Maximum Product Subarray