POJ-3821-Dining (拆点网络流)
这题为什么不能用 左边放食物,中间放牛,后面放水?
原因很简单,假设一头牛喜欢两个食物AB和两种水AB。
此时可以从一个食物A,走到牛A,再走到水A。
但是还可以有另一条路,从另一个食物B,走到该牛A,并走到该牛喜欢的另一个水B。
这是错误的原因之一,其实也跟反边有关。
所以对于牛,拆开,中间建立一条边,说明该牛已经使用过,这样也避免了从其它食物C走到牛A,此时我们希望它走反边,即牛A到食物A,再从食物A走向其它牛等等,但是却在到达牛A的时候,走向了水B的情况。
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
int n,f,d,fi,di;
const int INF=0x3f3f3f3f;
const int maxn=110;
struct Edge {
int to,cap,flow;
};
struct Dinic {
int s,t,cnt;
Edge edge[maxn*maxn*2];
int head[maxn*4];
int next[maxn*maxn*2];
int deep[maxn*4];
int cur[maxn*4];
bool vis[maxn*4];
void addEdge(int u,int v,int w)
{
edge[cnt].to=v;
edge[cnt].cap=w;
edge[cnt].flow=0;
next[cnt]=head[u];
head[u]=cnt++;
edge[cnt].to=u;
edge[cnt].flow=0;
edge[cnt].cap=0;
next[cnt]=head[v];
head[v]=cnt++;
}
void init()
{
s=0;
t=401;
cnt=0;
memset(head,-1,sizeof(head));
for (int i=1;i<=100;i++) {
addEdge(s,i,1);
}
for (int i=301;i<=400;i++) {
addEdge(i,t,1);
}
}
bool bfs()
{
memset(deep,0,sizeof(deep));
memset(vis,0,sizeof(vis));
queue<int> q;
vis[s]=true;
deep[s]=0;
q.push(s);
while (!q.empty()) {
int u=q.front();
q.pop();
for (int i=head[u];i!=-1;i=next[i]) {
Edge &e=edge[i];
if (!vis[e.to]&&e.cap>e.flow) {
deep[e.to]=deep[u]+1;
vis[e.to]=true;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int u,int in)
{
if (u==t||in==0) {
return in;
}
int out=0,f=0;
for (int &i=cur[u];i!=-1;i=next[i]) {
Edge &e=edge[i];
if (deep[e.to]==deep[u]+1&&(f=dfs(e.to,min(e.cap-e.flow,in)))>0) {
in-=f;
out+=f;
edge[i].flow+=f;
edge[i^1].flow-=f;
if (in==0) {
break;
}
}
}
return out;
}
int maxflow()
{
int ans=0;
while (bfs()) {
for (int i=0;i<4*maxn;i++) {
cur[i]=head[i];
}
ans+=dfs(s,INF);
}
return ans;
}
void print()
{
for (int i=0;i<=400;i++) {
if (head[i]!=-1) {
printf("%d:",i);
for (int j=head[i];j!=-1;j=next[j]) {
printf("%d %d %d ",edge[j].to,edge[j].cap,edge[j].flow);
}
printf("\n");
}
}
}
}DC;
int main()
{
int food,drink;
scanf("%d%d%d",&n,&f,&d);
DC.init();
for (int i=1;i<=n;i++) {
scanf("%d%d",&fi,&di);
for (int j=0;j<fi;j++) {
scanf("%d",&food);
DC.addEdge(food,100+i,1);
}
DC.addEdge(100+i,200+i,1);
for (int j=0;j<di;j++) {
scanf("%d",&drink);
DC.addEdge(i+200,drink+300,1);
}
}
//DC.print();
printf("%d\n",DC.maxflow());
return 0;
}
/*
2 2 2
1 1 2 1
2 1 2 1 2
*/
POJ-3821-Dining (拆点网络流)的更多相关文章
- POJ 3281 Dining (拆点)【最大流】
<题目链接> 题目大意: 有N头牛,F种食物,D种饮料,每一头牛都有自己喜欢的食物和饮料,且每一种食物和饮料都只有一份,让你分配这些食物和饮料,问最多能使多少头牛同时获得自己喜欢的食物和饮 ...
- poj 3281 Dining 拆点 最大流
题目链接 题意 有\(N\)头牛,\(F\)个食物和\(D\)个饮料.每头牛都有自己偏好的食物和饮料列表. 问该如何分配食物和饮料,使得尽量多的牛能够既获得自己喜欢的食物又获得自己喜欢的饮料. 建图 ...
- poj 3281 Dining(网络流+拆点)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20052 Accepted: 8915 Descripti ...
- POJ 3281 Dining (网络流)
POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...
- POJ 3281 Dining(最大流)
POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
- POJ 3281 Dining(网络流拆点)
[题目链接] http://poj.org/problem?id=3281 [题目大意] 给出一些食物,一些饮料,每头牛只喜欢一些种类的食物和饮料, 但是每头牛最多只能得到一种饮料和食物,问可以最多满 ...
- POJ - 3281 Dining(拆点+最大网络流)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18230 Accepted: 8132 Descripti ...
- 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)
Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...
随机推荐
- AcWing 794. 高精度除法
https://www.acwing.com/problem/content/796/ #include<bits/stdc++.h> using namespace std; // A/ ...
- 新的存储网站,和存储单元dropbox
新的存储网站,和存储单元dropbox 待办 https://www.dropbox.com/home google 登陆 google邮箱 密码 521google 但是免费存储量只有2G goog ...
- codeforces 597div2 F. Daniel and Spring Cleaning(数位dp+二维容斥)
题目链接:https://codeforces.com/contest/1245/problem/F 题意:给定一个区间(L,R),a.b两个数都是属于区间内的数,求满足 a + b = a ^ b ...
- poj 1611 :The Suspects经典的并查集题目
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- 6_11 四分树(UVa297)<四分树>
一幅图有1024个点, 可以对图平均分成4块, 并且子图也可以再往下分, 直到一个子图表示一个点. f表示这块子图填满, p表示它还有4个子图, e表示没有子图(当然啦, 它也没有填满). 给定两个字 ...
- Spring Security技术栈开发企业级认证与授权(一)环境搭建
本项目是基于慕课网的Spring Security技术栈开发企业级认证与授权,采用IDEA开发,本文章用来记录该项目的学习过程. 慕课网视频:https://coding.imooc.com/clas ...
- bash_profile文件
bash_profile文件的作用 如何填写 如何生效
- 转载:AAC编解码概述
转自:http://www.cnblogs.com/gaozehua/archive/2012/05/03/2479960.html 编码概述 其整体AAC 编解码系统,如图所示,其编码流程概述如下: ...
- C 语言实例 - 判断闰年
用户输入年份,判断该年份是否为闰年. #include <stdio.h> int main() { int year; printf("输入年份: "); scanf ...
- 搭建vue工程遇到的问题汇总
1.vue搭建环境- vue init webpack my-project无响应(如下报错) 原因:node 版本过高.v8.1.0 解决拌饭: 降级,终端指令 n v8.0.0; mac用户: s ...