poj--1275 cashier Employment Differential constraint system

Source: Internet
Author: User
Tags integer numbers

Cashier Employment

Description

A supermarket in Tehran are open hours a day every day and needs a number of cashiers to fit its need. The supermarket manager has hired your to help him and solve his problem. The problem is and the supermarket needs different number of cashiers at different times of all day (for example, a few Cashiers after midnight, and many in the afternoon) to provide good service to it customers, and he wants to hire the Lea St Number of cashiers for this job.

The manager has provided you and the least number of cashiers needed for every one-hour slots of the day. This data is given as R (0), R (1), ..., R (All): R (0) represents the least number of cashiers needed from midnight to. M., R (1) shows this number for duration of the a.m. to. Note that these numbers is the same every day. There is N qualified applicants for the this job. Each applicant I works non-stop once each hours in a shift of exactly 8 hours starting from a specified hour, say Ti (0 <= ti <=), exactly from the start of the hour mentioned. That's, if the ith applicant is hired, he/she'll work starting from ti o ' clock sharp for 8 hours. Cashiers do not replace one another and work exactly as scheduled, and there is enough cash registers and counters for th OSE who is hired.

You is to write a program to read the R (i) ' s for i=0..23 and TI's for I=1..N that is all, non-negative integer numbers and compute the least number of cashiers needed to being employed to meet the mentioned constraints. Note that there can is more cashiers than the least number needed for a specific slot.

Input

The first line of input was the number of test cases for this problem (at most 20). Each test case is starts with an integer numbers representing the R (0), R (1), ..., R (+) in one line (R (i) can be is at most 100 0). Then there was N, number of applicants in another line (0 <= N <=), after which come n lines each containing One ti (0 <= ti <= 23). There is no blank lines between test cases.

Output

For each test case, the output should are written in one line, and which is the least number of cashiers needed.
If There is no solution for the test case, you should write no solution for the case.

Sample Input

11 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1502322110

Sample Output

1
Analysis:
constraint analysis, for the convenience of analysis, we change the discussion interval from [0,23] to [1,24]   :   Apply[i] for the total number of persons working in the first hour of employment (R[i] for the minimum number of persons required for the first I hour x[i] for the actual employment of the total number of persons who started working in the first hour set s[i] = x[1] + ... + x[i] (total number of employees employed in the 1th to the first hour) constraints:
Because of 0<=x[i]<=apply[i], so
1> s[i]-s[i-1]>=0;2> S[i]-s[i-1]<=apply[i] <=> s[i-1]-s[i]>=-apply[i];
Due to topic limitations:
3> S[i]-s[i-8]>=r[i] (8<=i<=24) 4> s[i]-s[i+16]>=r[i]-s[24]  (i<8)  (*)
(*) in inequality, s[24] (also the last requirement) is a variable, so it needs a two-point answer;
After the two-point answer is P, note that the s[24]==p is assumed;
So 5> s[24]-s[0]>=p  && s[0]-s[24]<=-p;
Note that this must be added to the diagram to run together, and cannot be judged individually, because s[24] is not a separate variable, but is related to other variables.
Finally, find the smallest, run the longest road can.
The code is as follows:
<pre name= "code" class= "CPP" > #include <cstdio> #include <iostream> #include <cstring> #i nclude<queue> #include <vector> #define LL Long long #define CLEAR (XXX) memset ((XXX), 0,size        Of (XXX)) using namespace Std;      const int INF=1E9;        const int maxn=1000+5,maxm=100005;      int n,apply[30],r[30];          inline void _read (int &x) {char ch=getchar (); bool Mark=false; for (;!          IsDigit (CH); Ch=getchar ()) if (ch== '-') mark=true;          for (X=0;isdigit (CH); Ch=getchar ()) x=x*10+ch-' 0 ';      if (Mark) x=-x;          } struct edge{int from,to,w;     Edge (int from,int to,int W): From, To, W (w) {}};      Vector<edge> Edge;      int LAST[MAXM],NEXT[MAXM];      int DIST[MAXN];      int CNT[MAXN];         BOOL VIS[MAXN];              struct spfa{int n,m;              void init (int n) {this->n = n; m=0;  CLEAR (last);              CLEAR (Next); Edge. Clear ();          Edge.push_back (Edge (0,0,0));              } void Add_edge (int from,int to,int dist) {Edge.push_back (Edge (from,to,dist));              M=edge.size ()-1;              Next[m]=last[from];          Last[from]=m;              } bool Solve (int s) {int i; CLEAR (VIS);                CLEAR (CNT);              for (i=1;i<=n;i++) dist[i]=-inf;            dist[s]=0;              vis[s]=true;cnt[s]++;           Queue <int> q;              Q.push (s);                  while (!q.empty ()) {int X=q.front ();                 Q.pop (); vis[x]=false;                      for (I=last[x];i;i=next[i]) {edge& e=edge[i];                          if (Dist[e.from]+e.w>dist[e.to]) {//least, with the longest dist[e.to]=dist[e.from]+e.w;                              if (!vis[e.to]) {cnt[e.to]++;                              if (cnt[e.to]==n+1) return false; Q.pusH (e.to);                          Vis[e.to]=true;          }}}} return true;                  } void Answer () {for (int i=1;i<=n;i++) if (dist[i]>=inf) printf ("nopath\n");          else printf ("%d\n", Dist[i]);  }      };  SPFA Solver;       BOOL Check (int p) {//two min ans, that is s[24]; int i;      Solver.init (24);          for (i=1;i<=24;i++) {Solver.add_edge (i-1,i,0);//1> s[i]-s[i-1]>=0;           Solver.add_edge (I,i-1,-apply[i]);          2> S[i]-s[i-1]<=apply[i] <=> s[i-1]-s[i]>=-apply[i];          Solver.add_edge (0,i,0); if (i>=8) Solver.add_edge (I-8,i,r[i]);  3> s[i]-s[i-8]>=r[i];          (8<=i<=24) Else Solver.add_edge (i+16,i,r[i]-p);  4> s[i]-s[i+16]>=r[i]+p} solver.add_edge (0,24,p); 5> dist[24]-dist[0]==p;     Solver.add_edge (24,0,-P); Return Solver.solve (0);}      int main () {//freopen ("Ans.out", "w", stdout);       int t,i,x,j,l,r,y;       _read (t); while (t--) {CLEAR (R);          CLEAR (apply);          for (i=1;i<=24;i++) _read (R[i]);            _read (n);  for (i=1;i<=n;i++) {_read (x);          apply[x+1]++;          } l=0;r=n;              while (l<=r) {int mid= (L+R) >>1; Check (mid)?           (r=mid-1):(l=mid+1);           } if (check (l) &&l<=n) printf ("%d\n", L);       Else puts ("No solution");  } return 0;      }


another version
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < Queue>const int inf=-1e9;using namespace Std;int dis[5005],next[100005],last[100005];bool vis[5005];int cnt[5005]; int num[25];int r[25];int m=0;int n=24;struct edge{int From,to,len;edge () {}edge (int a,int b,int c) {from=a;to=b;len=c;}};      Edge line[100005];inline void Read (int &x) {char t;      BOOL Mark=false; for (; T=getchar (),t< ' 0 ' | |      T> ' 9 ';) if (t== '-') mark=1; for (x=t-' 0 ', T=getchar (); '      0 ' <=t&&t<= ' 9 '; x=x*10+t-' 0 ', T=getchar ());  x=mark?-x:x; }void add_edge (int from,int to,int len) {M++;next[m]=last[from];last[from]=m;line[m]=edge (From,to,len);} void Check_clear () {memset (next) memset (Last,0,sizeof (last)) memset (dis,0,sizeof (dis)); memset ( Line,0,sizeof (line));} void Init_clear () {memset (next) memset (Last,0,sizeof (last)) memset (dis,0,sizeof (DIS)), memset (line , 0,sizeof (line)); memset (r,0,sizeof (R));} BOOL SPFA (int s) {QUEUE&LT;int>q;memset (vis,0,sizeof (Vis)); Memset (cnt,0,sizeof (CNT)); int i,j,k,t;for (i=1;i<=n;i++) dis[i]=inf;vis[s ]=true;dis[s]=0;cnt[s]++; Q.push (s); while (Q.size ()) {T=q.front (); Q.pop (); Vis[t]=false;for (I=last[t];i;i=next[i]) {if (dis[line[i].to]< Dis[line[i].from]+line[i].len) {dis[line[i].to]=dis[line[i].from]+line[i].len;if (vis[line[i].to]==false) {cnt[ Line[i].to]++;if (cnt[line[i].to]>=n+1) {return false;} Q.push (line[i].to); vis[line[i].to]=true;}}} return true;} BOOL Check (int mid) {check_clear (); bool Flag;int i,j,k;m=0;for (i=1;i<=24;i++) Add_edge (i-1,i,0); for (i=1;i<=24; i++) Add_edge (I,i-1,-num[i]), for (i=8;i<=24;i++) Add_edge (I-8,i,r[i]), for (i=1;i<=7;i++) Add_edge (I+16,i,r[i] -mid); for (i=1;i<=24;i++) Add_edge (0,i,0); Add_edge (0,24,mid); Add_edge (24,0,-mid); FLAG=SPFA (0);//if (flag== False) cout<<m id<< "Fuck" <<endl;//else cout<<mid<< "" <<dis[24]<< "" < <dis[0]<<endl;if (Flag==false) return false;//if (Dis[n]>mid) return FALSE;ELSE RETUrn true;} int main () {int H;cin>>h;while (h--) {//s[i]-s[i-1]>=0//s[i-1]+num[i]>=s[i]//s[i]-s[i-8]>=num[i] (8= <i<=24)//s[24]-s[i+16]+s[i]>=r[i] (1<=i<=7) init_clear (); int Minn=1,maxn,tot;int i,j,k,x;m=0;for (i= 1;i<=24;i++) scanf ("%d", &r[i]), scanf ("%d", &tot), for (i=1;i<=tot;i++) {scanf ("%d", &x); num[x+1]++ ;} Maxn=tot;while (MINN&LT;=MAXN) {int mid= (MINN+MAXN)/2;if (check (mid)) Maxn=mid-1;else minn=mid+1;} if (check (Minn) ==false) cout<< "No solution" <<endl;else Cout<<minn<<endl;}



poj--1275 cashier Employment Differential constraint system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.