本文主要是介绍bzoj 2502 有上下限的最小流,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
觉得有可能是网络流~~但是题目一直说是最小次数~~~一直卡在最大流这个思维,都快忘记有最小流这个东西了~~~~
建图方式:每条边设下限为1,上限为无穷大
s连接到每一个点,下限为0,上限无穷大
t连接到每一个点,下限为0,上限无穷大
这样跑一个有上下限的最小流,就可以得出答案了~~~所有边都被遍历了~~~
#include <algorithm>
#include <algorithm>
#include <iostream>
#include<string.h>
#include <fstream>
#include <math.h>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define exp 1e-8
#define fi first
#define se second
#define ll long long
#define INF 0x3f3f3f3f
#define lson l,mid,rt<<1
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rson mid+1,r,(rt<<1)+1
#define all(a) a.begin(),a.end()
#define mm(a,b) memset(a,b,sizeof(a));
#define for1(a,b) for(int a=1;a<=b;a++)//1---(b)
#define rep(a,b,c) for(int a=b;a<=c;a++)//b---c
#define repp(a,b,c)for(int a=b;a>=c;a--)///
using namespace std;
void bug(string m="here"){cout<<m<<endl;}
template<typename __ll> inline void READ(__ll &m){__ll x=0,f=1;char ch=getchar();while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}m=x*f;}
template<typename __ll>inline void read(__ll &m){READ(m);}
template<typename __ll>inline void read(__ll &m,__ll &a){READ(m);READ(a);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b){READ(m);READ(a);READ(b);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c){READ(m);READ(a);READ(b);READ(c);}
template < class T > T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template < class T > T lcm(T a, T b) { return a / gcd(a, b) * b; }
template < class T > inline void rmin(T &a, const T &b) { if(a > b) a = b; }
template < class T > inline void rmax(T &a, const T &b) { if(a < b) a = b; }
template < class T > T pow(T a, T b) { T r = 1; while(b > 0) { if(b & 1) r = r * a; a = a * a; b /= 2; } return r; }
template < class T > T pow(T a, T b, T mod) { T r = 1; while(b > 0) { if(b & 1) r = r * a % mod; a = a * a % mod; b /= 2; } return r; }
const int maxn=100000;
struct EDGE
{int to,flow,next;
}edge[maxn];
int head[maxn],cnt;
int pre[maxn];
int cur[maxn];
int gap[maxn];
int level[maxn];
int w[maxn];
int s,t,ss,tt,tot,sum;
inline void addedge(int u,int v,int flow)
{edge[cnt].to=v;edge[cnt].flow=flow;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].flow=0;edge[cnt].next=head[v];head[v]=cnt++;
}
void build_graph(int n)
{s=n+1,t=s+1,ss=t+1,tt=ss+1,cnt=0,sum=0,tot=tt;memset(head,-1,sizeof(head));memset(w,0,sizeof(w));for(int i=1;i<=n;i++){int a,b;read(a);while(a--){read(b);addedge(i,b,INF);w[i]--,w[b]++;}}for(int i=1;i<=n;i++)addedge(s,i,INF),addedge(i,t,INF);for(int i=1;i<=tot;i++)if(w[i]>0)addedge(ss,i,w[i]),sum+=w[i];else if(w[i]<0) addedge(i,tt,-w[i]);
}
int sap(int start,int End)//别打成s,t
{memset(gap,0,sizeof(gap));memset(pre,-1,sizeof(pre));memset(level,0,sizeof(level));int u=start,ans=0,inf=INF;gap[0]=tot;for(int i=0;i<=tot;i++)cur[i]=head[i];while(level[start]<tot){bool flag=0;for(int &i=cur[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(edge[i].flow&&level[u]==level[v]+1){flag=1;inf=min(inf,edge[i].flow);pre[v]=u;u=v;if(u==End){while(u!=start){u=pre[u];edge[cur[u]].flow-=inf;edge[cur[u]^1].flow+=inf;}ans+=inf;inf=INF;}break;}}if(!flag){if(--gap[level[u]]==0)break;int minn=tot;for(int i=head[u];i!=-1;i=edge[i].next)if(edge[i].flow&&level[edge[i].to]<minn){minn=level[edge[i].to];cur[u]=i;}level[u]=minn+1;gap[minn+1]++;if(u!=start)u=pre[u];}}return ans;
}
int main()
{int n;read(n);build_graph(n);int idx=cnt;sap(ss,tt); //这一步还得跑~~addedge(t,s,INF);sap(ss,tt);cout<<edge[idx^1].flow<<endl;return 0;
}
这篇关于bzoj 2502 有上下限的最小流的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!