标签:cstring NPU field shape 最大流 present ESS lines algorithm
Description
Input
Output
Sample Input
3 4 1 1 1 3 2 2 3 2
Sample Output
2
你要很多个点
每个点在方格里
你每次消除可以是一行或是一列
求最小消除次数
将每个点对应的行列连边,求最大流
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<algorithm> #define re return #define inc(i,l,r) for(int i=l;i<=r;++i) const int maxn=1505,maxm=20005; using namespace std; template<typename T>inline void rd(T&x) { char c;bool f=0; while((c=getchar())<‘0‘||c>‘9‘)if(c==‘-‘)f=1; x=c^48; while((c=getchar())>=‘0‘&&c<=‘9‘)x=x*10+(c^48); if(f)x=-x; } int n,m,s,t,deep[maxn],k=1,hd[maxn],cur[maxn]; struct node{ int to,nt,w; }e[maxm]; inline void add(int x,int y,int z) { e[++k].to=y;e[k].nt=hd[x];hd[x]=k;e[k].w=z; e[++k].to=x;e[k].nt=hd[y];hd[y]=k;e[k].w=0; } inline bool bfs() { queue<int>q; inc(i,1,t)deep[i]=0; deep[s]=1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=hd[u];i;i=e[i].nt) { int v=e[i].to; if(!deep[v]&&e[i].w) { deep[v]=deep[u]+1; if(v==t)re 1; q.push(v); } } } re 0; } inline int dfs(int u,int flow) { if(u==t) re flow; int delta=flow; for(int &i=cur[u];i;i=e[i].nt) { int v=e[i].to; if(deep[v]==deep[u]+1&&e[i].w) { int d=dfs(v,min(delta,e[i].w)); e[i].w-=d;e[i^1].w+=d; delta-=d; if(!delta)re flow; } } re flow-delta; } int main() { rd(n),rd(m); s=(n<<1)+1; t=s+1; int x,y; inc(i,1,m) { rd(x),rd(y); add(x,y+n,1); } inc(i,1,n){ add(s,i,1); add(i+n,t,1); } int ans=0; while(bfs()) { inc(i,1,t)cur[i]=hd[i]; ans+=dfs(s,n); } printf("%d",ans); re 0; }
标签:cstring NPU field shape 最大流 present ESS lines algorithm
原文地址:https://www.cnblogs.com/lsyyy/p/11291409.html