zoukankan      html  css  js  c++  java
  • poj1275 Cashier Employment 差分约束

    poj1275 Cashier Employment

    题目传送

    sol:

    不是很容易想到。。

    不妨令(S[i](0≤i≤23))表示前i小时已经定了i个人。

    那么根据题目给定条件及隐含条件作出约束:

    [s[i]-s[i-8]≥need[i] (8≤i≤23)\ sum-(s[i+16]-s[i])≥need[i] (0≤i≤7)\ s[i-1]≤s[i] (0≤i≤23)\ s[i+1]-s[i]≤have[i] (0≤i≤23)\ ]

    整理一下得到:

    [s[i]≥s[i-8]+need[i] (8≤i≤23)\ s[i]≥need[i]-sum+s[i+16] (0≤i≤7)\ s[i]≥s[i-1] (0≤i≤23)\ s[i]≥[i+1]-have[i] (0≤i≤23)\ ]

    但是注意到第二个不等式中多出了一个(s[23]​),没法直接做。

    所以考虑枚举或二分(sum​)的值,然后求出来的(s[23]​)应该满足(s[23]≥sum​)才能保证求出来之后回代入二式仍然成立。

    所以又存在这么一条限制(令s[-1]=0):(s[23]≥sum+0=sum+s[-1])

    为了方便,不妨所有的下标都加1,然后就ok了。

    code

    #include<queue>
    #include<string>
    #include<cstdio>
    #include<cstring>
    #define LL long long
    #define DB double
    #define RG register
    #define IL inline 
    using namespace std;
    
    const int N=1003;
    
    queue<int> q;
    int n,l,r,mid,tot,t[33],s[33],R[N],inq[N],cnt[N],head[N];
    
    struct EDGE{int next,to,v;}e[N*21];
    
    IL int gi() {
        RG int x=0,p=1; RG char ch=getchar();
        while(ch<'0'||ch>'9') {if(ch=='-') p=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=getchar();
        return x*p;
    }
    
    IL void make(int a,int b,int c) {e[++tot]=(EDGE){head[a],b,c},head[a]=tot;}
    
    IL void New_Graph() {
    	tot=0;
    	memset(&e,0,sizeof(e));
    	memset(head,0,sizeof(head));
    }
    
    IL int spfa(int val) {
    	RG int i,x,y;
    	memset(s,0xcf,sizeof(s));
    	memset(inq,0,sizeof(inq));
    	memset(cnt,0,sizeof(cnt));
    	while(!q.empty()) q.pop();
    	s[0]=0,cnt[0]=1,q.push(0);
    	while(!q.empty()) {
    		x=q.front(),inq[x]=0,q.pop();
    		for(i=head[x];i;i=e[i].next)
    			if(s[y=e[i].to]<s[x]+e[i].v) {
    				s[y]=s[x]+e[i].v;
    				if(++cnt[y]==24) return 0; 
    				if(!inq[y]) inq[y]=1,q.push(y);
    			}
    	}
    	return s[24]>=val;
    }
    
    int main()
    {
    	RG int i,T=gi();
    	while(T--) {
    		for(i=1;i<=24;++i) R[i]=gi();
    		n=gi(),l=0,r=n+1;
    		memset(t,0,sizeof(t));
    		for(i=1;i<=n;++i) ++t[gi()+1];
    		while(l<r) {
    			mid=l+r>>1,New_Graph(),make(0,24,mid);
    			for(i=1;i<=24;++i) {
    				if(i>=9) make(i-8,i,R[i]);
    				else make(i+16,i,R[i]-mid);
    				make(i-1,i,0),make(i,i-1,-t[i]);
    			}
    			if(spfa(mid)) r=mid;
    			else l=mid+1;
    		}
    		if(r==n+1) puts("No Solution");
    		else printf("%d
    ",r);
    	}
    	return 0;
    }
    // 差分约束(不错的题目!)
    
  • 相关阅读:
    sql: 左连接 和内连接区别联系 No
    Maven 浅谈一 No
    日记090212
    js 获取asp 控件DropDownList的选择值,及所有的text和value
    ASP.NET 2.0 只读 TextBox 回发后信息丢失
    Cry On My Shoulder (背景音乐)
    Inside ASP.NET 2.0 DataBound Control 3
    ASP.NET自定义控件复杂属性声明持久性浅析
    Inside ASP.NET 2.0 DataBound Control 2
    ASP.NET 2.0 Client Callback 浅析
  • 原文地址:https://www.cnblogs.com/Bhllx/p/11258280.html
Copyright © 2011-2022 走看看