poj3281—Dining(最大流)
来源:互联网 发布:linux 第二个mysql5.7 编辑:程序博客网 时间:2024/06/04 21:52
题目链接:传送门
Description
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.
Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
Input
Lines 2..N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.
Output
Sample Input
4 3 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 1 1 3 3
Sample Output
3
Hint
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.
解题思路:建图s—食物—牛—牛—饮料—t,权值为1,相同的牛之间建一条边确保了每头牛只选择一种搭配,然后在图上跑个最大流。
#include <cstdio>#include <algorithm>#include <queue>#include <iostream>using namespace std;const int N = 420;const int M = 20900;//按s——食物——牛——牛——饮料——t建图,边权都为1//每头牛都只能选一种搭配,所以同一头牛间建条边//边的结构struct edge_t{ int node; int c;//c为容量 edge_t* next; edge_t* redge;//指向反向边}Edge[M*2];int ECnt;//图的邻接表edge_t* Ver[N];void init(){ ECnt = 0; fill(Ver,Ver+N,(edge_t*)0);}//生成双向边void mkEdge(int a,int b,int c){ int t1 = ECnt++; int t2 = ECnt++; Edge[t1].node = b; Edge[t1].c = c; Edge[t1].next = Ver[a]; Edge[t1].redge = Edge + t2; Ver[a] = Edge + t1; Edge[t2].node = a; Edge[t2].c = 0; Edge[t2].next = Ver[b]; Edge[t2].redge = Edge + t1; Ver[b] = Edge + t2;}int L[N];//层次图//建立残留网络从源s到汇t的层次图bool bfs(int s,int t){ fill(L,L+N,-1); queue<int>q; q.push(s); L[s] = 0; while( !q.empty() ){ int u = q.front(); q.pop(); //寻找还有残量的边 for(edge_t*p=Ver[u];p;p=p->next){ if ( p->c <= 0 ) continue; int v = p->node; if ( -1 != L[v] ) continue; q.push(v); L[v] = L[u] + 1; } } return -1 != L[t];}//在层次图上搜索增广路径,本质上就是搜索可以增广的流量//这个流量是各层之间流量的最小值//u为当前节点,cf为当前层的最小流,t为汇点int dfs(int u,int e,int cf){ if ( u == e ) return cf; int tf = 0; //tf记录u往下一层的总可行流量 for(edge_t*p=Ver[u];p;p=p->next){ int v = p->node; int c = p->c; if ( L[u] + 1 == L[v] && c > 0 && cf > tf ){ int f = dfs(v,e,min(c,cf-tf)); if ( 0 == f ) continue; p->c -= f;//正向边减去可行流量 p->redge->c += f;//反向边加上 tf += f; } } if ( 0 == tf ) L[u] = -1;//修改层次图 return tf;}//Dinic算法,s为源,t为汇int Dinic(int s,int t){ int ret = 0; while( bfs(s,t) ){//第一步建立分层图 int ans; //第二步在分层图上查找一条增广路径的可行流量 while( ans = dfs(s,t,INT_MAX) ) ret += ans; } return ret;}//n头牛,f中食物,d种喝的void Build( int n , int f , int d ){ int a,b,c; //源点和食物建边 for( int i = 1 ; i <= f ; ++i ) mkEdge(0,i+2*n,1); //饮料喝汇点建边 for( int i = 1 ; i <= d ; ++i ) mkEdge(2*n+f+i,2*n+f+d+1,1); for( int i = 1 ; i <= n ; ++i ){ scanf("%d%d",&a,&b); //食物和牛建边 for( int j = 0 ; j < a ; ++j){ scanf("%d",&c); mkEdge(c+2*n,i,1); } //牛和牛建边 mkEdge(i,n+i,1); //牛和饮料建边 for( int j = 0 ; j < b ; ++j ){ scanf("%d",&c); mkEdge(n+i,2*n+f+c,1); } }}int main(){ int n,f,d; while( ~scanf("%d%d%d",&n,&f,&d) ){ init(); Build(n,f,d); printf("%d\n",Dinic(0,2*n+f+d+1)); } return 0;}
- poj3281——Dining(最大流)
- poj3281—Dining(最大流)
- POJ3281--Dining(最大流)
- poj3281 Dining(最大流)
- POJ3281:Dining(最大流)
- 【POJ3281 Dining 】最大流
- 【poj3281】【最大流】Dining
- POJ3281 Dining(最大流)
- 【poj3281】Dining 最大流
- POJ3281 Dining 最大流
- 【POJ3281】Dining【最大流】
- poj3281 Dining 最大流
- poj3281 Dining 最大流
- Dining POJ3281 最大流
- POJ3281 dining——最大流(建图是重点)
- POJ3281-Dining(最大流Dinic)
- POJ3281 Dining [最大流应用]
- poj3281-Dining ,最大流,建图
- mysql存储过程游标嵌套示例
- 同时启动2个tomcat注意
- leetcode 268. Missing Number
- java微信公众号支付授权
- 多维数组的传递
- poj3281—Dining(最大流)
- RxJava2_2:流程及关键对象的理解
- 基于协同过滤的推荐系统
- node.js快速创建http服务器
- 重载,重写(覆盖),隐藏
- python爬虫学习笔记(1)-爬取糗事百科
- 常用SQL查询语句
- 使用SecureCRT等工具时如何让Kibana一直后台运行
- GitHub滑稽入门指南