本文主要是介绍poj-3281 Dining,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
[题目链接]
思路:
最大流变形—顶点也有容量限制的情况!
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int Max_v=1e5+10;int n,f,d;
struct edge{int to,cap,rev;edge(int t,int c,int r):to(t),cap(c),rev(r){}
};
vector<edge>G[Max_v];
int level[Max_v];
int iter[Max_v];void add_edge(int from,int to,int cap){G[from].push_back(edge(to,cap,G[to].size()));G[to].push_back(edge(from,0,G[from].size()-1));
}void bfs(int s){memset(level,-1,sizeof(level));queue<int>que;level[s]=0;que.push(s);while(!que.empty()){int u=que.front();que.pop();for(int i=0;i<G[u].size();i++){edge e=G[u][i];if(e.cap>0&&level[e.to]<0){level[e.to]=level[u]+1;que.push(e.to);}}}
}int dfs(int u,int t,int f){if(u==t)return f;for(int &i=iter[u];i<G[u].size();i++){edge &e=G[u][i];if(e.cap>0&&level[u]<level[e.to]){int d=dfs(e.to,t,min(f,e.cap));if(d>0){e.cap-=d;G[e.to][e.rev].cap+=d;return d;}}}return -1;
}int max_flow(int s,int t){int flow=0;while(true){bfs(s);if(level[t]<0)return flow;memset(iter,0,sizeof(iter));int dist;while((dist=dfs(s,t,inf))>0){flow+=dist;}}
}int main()
{scanf("%d%d%d",&n,&f,&d);int s=0,t=550; //源点s汇点tfor(int i=1;i<=n;i++){ //牛[1,100]牛[101,200]之间连边add_edge(i,i+100,1);}for(int i=1;i<=f;i++){ //源点s(0)和食物[201,300]之间连边add_edge(s,i+200,1);}for(int i=1;i<=d;i++){ //饮料[301,400]和汇点t(550)之间连边add_edge(i+300,t,1);}int a,b,c,d;for(int i=1;i<=n;i++){scanf("%d%d",&a,&b);for(int j=0;j<a;j++){ //食物[201,300]和牛[1,100]之间连边scanf("%d",&c);add_edge(c+200,i,1);}for(int j=0;j<b;j++){ //牛[101,200]和饮料[301,400]之间连边scanf("%d",&c);add_edge(i+100,c+300,1);}}printf("%d\n",max_flow(s,t));return 0;
}
这篇关于poj-3281 Dining的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!