This question is very special. It is completely different from the difference constraint previously made, because there are variables in its constraints.
Graph creation method:
Note:
R [I] ------- the person required for the I hour
T [I] ------- the person who applied for the job on the I hour
S [I] ------- Total number of people recruited from 0th to I hours
Constraint system:
1. s [I]-s [I-1] <= t [I]
2. s [I]-s [I-1]> = 0
3. s [j]-s [I]> = r [I], j = (I + 8) % 24, j> I
4. s [j] + sum-s [I]> = r [I], j = (I + 8) % 24, j
5. s [24]-s [0]> = sum
Algorithm idea: Bellman_Ford () + enumeration sum (Binary enumeration can also be used here, I am enumerating directly)
[Cpp]
# Include
# Include
Using namespace std;
Struct {
Int u, v, w;
} E [1, 200];
Int t [25], r [25], s [25], d [25];
Int n, m;
Bool relax (int u, int v, int w)
{
If (d [u] + w> d [v])
{
D [v] = d [u] + w;
Return true;
}
Return false;
}
Bool Bellman_Ford ()
{
Int I, j;
Bool flag = true;
For (I = 0; I <= n; I ++)
D [I] = 100000000;
D [0] = 0;
For (I = 0; I {
Flag = false;
For (j = 0; j If (relax (e [j]. u, e [j]. v, e [j]. w ))
Flag = true;
}
If (! Flag)
Return true;
For (j = 0; j If (relax (e [j]. u, e [j]. v, e [j]. w ))
Return false;
Return true;
}
Int main ()
{
Int Case, sum;
Int I, j;
Scanf ("% d", & Case );
While (Case --)
{
For (I = 1; I <= 24; I ++)
{
T [I] = 0;
Scanf ("% d", r + I );
}
Scanf ("% d", & n );
While (n --)
{
Int time;
Scanf ("% d", & time );
T [time + 1] ++;
}
For (I = 1; I <= 24; I ++)
S [I] = s [I-1] + t [I];
///////////////////////////
// This part is fixed with Edges
M = 0;
N = 24;
For (I = 1; I <= 24; I ++)
{
E [m]. u = I-1;
E [m]. v = I;
E [m]. w = 0;
M ++;
E [m]. u = I;
E [m]. v = I-1;
E [m]. w =-t [I];
M ++;
}
For (I = 1; I <= 16; I ++)
{
J = I + 8;
E [m]. u = I;
E [m]. v = j;
E [m]. w = r [j];
M ++;
}
//////////////////////////////
Int mm = m;
For (sum = 0; sum <= s [24]; sum ++)
{
M = mm;
For (I = 17; I <= 24; I ++)
{
J = I-16;
E [m]. u = I;
E [m]. v = j;
E [m]. w = r [j]-sum;
M ++;
}
E [m]. u = 0;
E [m]. v = 24;
E [m]. w = sum;
M ++;
If (Bellman_Ford ())
Break;
}
If (sum <= s [24])
Printf ("% d \ n", sum );
Else
Printf ("No Solution \ n ");
}
Return 0;
}
# Include
# Include
Using namespace std;
Struct {
Int u, v, w;
} E [1, 200];
Int t [25], r [25], s [25], d [25];
Int n, m;
Bool relax (int u, int v, int w)
{
If (d [u] + w> d [v])
{
D [v] = d [u] + w;
Return true;
}
Return false;
}
Bool Bellman_Ford ()
{
Int I, j;
Bool flag = true;
For (I = 0; I <= n; I ++)
D [I] = 100000000;
D [0] = 0;
For (I = 0; I {
Flag = false;
For (j = 0; j If (relax (e [j]. u, e [j]. v, e [j]. w ))
Flag = true;
}
If (! Flag)
Return true;
For (j = 0; j If (relax (e [j]. u, e [j]. v, e [j]. w ))
Return false;
Return true;
}
Int main ()
{
Int Case, sum;
Int I, j;
Scanf ("% d", & Case );
While (Case --)
{
For (I = 1; I <= 24; I ++)
{
T [I] = 0;
Scanf ("% d", r + I );
}
Scanf ("% d", & n );
While (n --)
{
Int time;
Scanf ("% d", & time );
T [time + 1] ++;
}
For (I = 1; I <= 24; I ++)
S [I] = s [I-1] + t [I];
///////////////////////////
// This part is fixed with Edges
M = 0;
N = 24;
For (I = 1; I <= 24; I ++)
{
E [m]. u = I-1;
E [m]. v = I;
E [m]. w = 0;
M ++;
E [m]. u = I;
E [m]. v = I-1;
E [m]. w =-t [I];
M ++;
}
For (I = 1; I <= 16; I ++)
{
J = I + 8;
E [m]. u = I;
E [m]. v = j;
E [m]. w = r [j];
M ++;
}
//////////////////////////////
Int mm = m;
For (sum = 0; sum <= s [24]; sum ++)
{
M = mm;
For (I = 17; I <= 24; I ++)
{
J = I-16;
E [m]. u = I;
E [m]. v = j;
E [m]. w = r [j]-sum;
M ++;
}
E [m]. u = 0;
E [m]. v = 24;
E [m]. w = sum;
M ++;
If (Bellman_Ford ())
Break;
}
If (sum <= s [24])
Printf ("% d \ n", sum );
Else
Printf ("No Solution \ n ");
}
Return 0;
}