ZOJ Monthly, January 2018 - D Seat Assignment (最大流)

Hint

The first sample:
There are 10 seats numbered from 1 to 10.
There are 2 people wanting their seat number to be a multiple of 1, 1 person wanting his seat number to be a multiple of 2, and 4 people wanting their seat number to be a multiple of 3.
The 2 people wanting their seat number to be a multiple of 1 can take seats numbered 1 and 4.
The only person wanting his seat number to be a multiple of 2 can take seat numbered 2. 
The 3 people wanting their seat number to be a multiple of 2 can take seats numbered 3, 6, 9.

The second sample:
The 2 people wanting their seat number to be a multiple of 2 can take seats numbered 2, 6.
The only person wanting his seat number to be a multiple of 4 can take seat numbered 4.




题意:

给你M个位置。和10种人的数量。x种人代表他只做x*n号座位。(n=0,1,2……)

问你最多满足几个人坐位置。


POINT:

跑一边1,2,3,……,10发现他们的最小公倍数只有2000多一点。

即最多只有2^10种组合。(去重之后只有50个左右)

那么便是左边10个点,右边50个点的网络流,只要设置好容量即可。2的容量要减去4,6,8的容量、等等。 类似容斥


#include <iostream>
#include <stdio.h>
#include <cmath>
#include <string.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1111;
const int inf = 0x3f3f3f3f;
struct edge
{
	int from,to,cap,flow;
	edge(int u,int v,int c,int f):
	from(u),to(v),cap(c),flow(f){}
};
vector<edge> edges;
vector<int> G[maxn];
int ss,tt,cnt;
void add(int u,int v,int w)
{
	edges.push_back(edge(u,v,w,0));
	edges.push_back(edge(v,u,0,0));
	int m = (int)edges.size();
	G[u].push_back(m-2);
	G[v].push_back(m-1);
}

void init()
{
	for(int i=0;i<maxn;i++) G[i].clear();
	edges.clear();
}

int d[maxn];

int bfs()
{
	int vis[maxn];
	memset(vis,0,sizeof vis);
	memset(d,0,sizeof d);
	queue<int>q;
	q.push(ss);
	vis[ss]=1;
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int i=0;i<G[x].size();i++){
			edge len = edges[G[x][i]];
			if(len.cap>len.flow&&!vis[len.to]){
				q.push(len.to);
				d[len.to]=d[x]+1;
				vis[len.to]=1;
			}
		}
	}
	return vis[tt];
}
int cur[maxn];
int dfs(int x,int a)
{
	if(x==tt||a==0) return a;
	int flow=0,f=0;
	for(int &i=cur[x];i<G[x].size();i++){
		edge &len = edges[G[x][i]];
		if(d[x]+1==d[len.to]&&(f=dfs(len.to,min(a,len.cap-len.flow)))>0){
			a-=f;
			edges[G[x][i]].flow+=f;
			edges[G[x][i]^1].flow-=f;
			flow+=f;
		}
		if(a==0) break;
	}
	if(flow==0) d[x]=-1;
	return  flow;
}
LL maxflow()
{
	LL ans = 0;
	while(bfs()){
		memset(cur,0,sizeof cur);
		ans+=(LL)dfs(ss,inf);
	}
	return ans;
}

int m;
int num[11];
int temp[2000];
int gcd(int x,int y)
{
	return y==0?x:gcd(y,x%y);
}
void ddfs(int x,int now)
{
	if(x==11){
		temp[++cnt]=now;
		return;
	}
	int d = gcd(now,x);
	ddfs(x+1,now*x/d);
	ddfs(x+1,now);
}

void xinit()
{
	memset(temp,0,sizeof temp);
	ddfs(1,1);
	sort(temp+1, temp+1+cnt);
	cnt=unique(temp+1, temp+1+cnt)-temp-1;

}

int main()
{
	int T;
	scanf("%d",&T);
	xinit();
	while(T--){
		scanf("%d",&m);
		for(int i=1;i<=10;i++) scanf("%d",&num[i]);
		init();
		int xnum[66];
		for(int i=1;i<=cnt;i++){
			xnum[i]=m/temp[i];
		}
		for(int i=cnt;i>=1;i--){
			for(int j=i+1;j<=cnt;j++){
				if(temp[j]%temp[i]==0){
					xnum[i]-=xnum[j];
				}
			}
			if(xnum[i]!=0)
				add(i+10,100,xnum[i]);
		}
		for(int i=1;i<=10;i++){
			if(num[i]==0)  continue;
			add(0,i,num[i]);
			for(int j=1;j<=cnt;j++){
				if(temp[j]>m) break;
				if(temp[j]%i==0&&xnum[j]!=0){
					add(i,j+10,inf);
				}
			}
		}
		ss=0,tt=100;
		printf("%lld\n",maxflow());



	}


}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值