poj1275 Cashier Employment
sol:
It's not easy to think of...
Let(S[i] (0 < I < 23) indicate that I have been appointed in the previous hour.
Then constraints are made according to the given and implicit conditions of the title:
\[
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)\\
\]
I'll sort it out and get it:
\[
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)\\
\]
However, it is noted that there is an extra \(s[23]) in the second inequality, which can not be done directly.
So consider the value of enumeration or dichotomy sum and then find out that the \ (s[23] should satisfy \\\\\\\\\\
So there is another limitation (let s[-1]=0): \ (s[23] (> sum+0=sum+s[-1]).
For convenience, add 1 to all subscriptions, and then 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\n",r); } return 0; } // Differential constraints (good topic!)