题意:在一家超市里,每个时刻都需要有营业员看管,R(i) (0 <= i < 24)表示从i时刻开始到i+1时刻结束需要的营业员的数目,现在有N(N <= 1000)个申请人申请这项工作,并且每个申请者都有一个起始工作时间 ti,如果第i个申请者被录用,那么他会从ti时刻开始连续工作8小时。现在要求选择一些申请者进行录用,使得任何一个时刻i,营业员数目都能大于等于R(i)。求出至少需要录用多少营业员。
思路:我们用S[i]表示一天内前i+1个小时录用的人员,
当i>=7时,我们需要满足s[i]−s[i−8]>=R[i]
当0<=i<7,经过推倒不难发现,我们需要满足s[i]+s[23]−s[i+16]>=R[i]
同时,因为题目中人数的限制,我们还需要满足0<=s[i]−s[i−1]<=b[i]
这样这道题就看起来可做了
但是我们的第二个式子左边有三项,不过还好s[23]是个常数项,我们可以二分解决
因为是求最小,所以我们按照套路连边,求最长路
第一种写法:我们知道从0开始可以走到所有的点,所以我们从0开始跑最短路即可。
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 30;
const int maxm = 1005;
int tot,ver[maxm],he[maxn],ne[maxm],cost[maxm];
void add( int x,int y,int c ){
ver[++tot] = y;
ne[tot] = he[x];
he[x] = tot;
cost[tot] = c;
}
int d[maxn],cnt[maxn];
int v[maxn];
queue<int> que;
bool spfa(int s,int n){
for( int i = 0;i <= n+1;i++ ){
d[i] = -0x3f3f3f3f;
}
//memset( d,-1,sizeof(int)*(n+1) );
memset(v ,0,sizeof(int)*(n+1) );
cnt[s] = 0;d[s] = 0;v[s] = 1;
while(que.size()) que.pop();
que.push(s);
while( que.size() ){
int x = que.front();que.pop();
v[x] = 0;
for( int cure = he[x];cure;cure = ne[cure] ){
int y = ver[cure],c = cost[cure];
if( d[y] < d[x] + c ){
d[y] = d[x] + c;
cnt[y] = cnt[x] + 1;
if( cnt[y] >= n ) return false;
if(!v[y]){
que.push(y);
v[y] = 1;
}
}
}
}
return true;
}
void init(int n){
memset( he,0,sizeof(int)*(n+1) );
tot = 1;
}
int R[25],b[25];
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(b,0,sizeof(b));
for( int i = 1;i <= 24;i++ )scanf("%d",&R[i]);
int n;scanf("%d",&n);
for( int i = 1;i <= n;i++ ){
int x;
scanf("%d",&x);
b[x+1]++;
}
int l = 0,r = n+1;
bool flag = false;
int ans;
for( int mid = 0;mid <= n;mid++ ) {
init(25);
//int mid = l+r>>1;
add( 0,24,mid );
for (int i = 1; i <= 24; i++) {
add(i, i-1, -b[i]);
add(i-1,i,0 );
}
for (int i = 1; i <= 8; i++) {
add(i + 16, i, R[i] - mid);
}
for (int i = 9; i <= 24; i++) {
add(i-8, i, R[i]);
}
bool f = spfa(0,25);
if(f && d[24]==mid ){
flag = true;
ans = mid;
break;
}
}
if( !flag ){
puts("No Solution");
}else{
printf("%d\n",ans);
}
}
return 0;
}
第二种写法:新加一个节点用来判断图中是否有环。
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 30;
const int maxm = 1005;
int tot,ver[maxm],he[maxn],ne[maxm],cost[maxm];
void add( int x,int y,int c ){
ver[++tot] = y;
ne[tot] = he[x];
he[x] = tot;
cost[tot] = c;
}
int d[maxn],cnt[maxn];
int v[maxn];
queue<int> que;
bool spfa(int s,int n){
for( int i = 0;i <= n+1;i++ ){
d[i] = -0x3f3f3f3f;
}
while(que.size()) que.pop();
//memset( d,-1,sizeof(int)*(n+1) );
memset(v ,0,sizeof(int)*(n+1) );
cnt[s] = 0;d[s] = 0;v[s] = 1;
que.push(s);
while( que.size() ){
int x = que.front();que.pop();
v[x] = 0;
for( int cure = he[x];cure;cure = ne[cure] ){
int y = ver[cure],c = cost[cure];
if( d[y] < d[x] + c ){
d[y] = d[x] + c;
cnt[y] = cnt[x] + 1;
if( cnt[y] >= n ) return false;
if(!v[y]){
que.push(y);
v[y] = 1;
}
}
}
}
return true;
}
void init(int n){
memset( he,0,sizeof(int)*(n+1) );
tot = 1;
}
int R[25],b[25];
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(b,0,sizeof(b));
for( int i = 0;i < 24;i++ )scanf("%d",&R[i]);
int n;scanf("%d",&n);
for( int i = 1;i <= n;i++ ){
int x;
scanf("%d",&x);
b[x]++;
}
int l = 0,r = n+1;
while( l != r-1 ) {
init(25);
int mid = l+r>>1;
for (int i = 8; i <= 23; i++) {
add(i-8, i, R[i]);
}
for (int i = 0; i <= 7; i++) {
add( i+16,i,R[i]-mid );
}
for (int i = 1; i <= 23; i++) {
add(i, i-1, -b[i]);
add(i-1,i,0 );
}
add( 0,24,-b[0] );
add( 24,0,0 );
add( 24,23,mid );
for( int i = 0;i < 23;i++ ){
add( 24,i,0 );
}
bool flag = spfa(24,25);
if(flag){
r = mid;
}else{
l = mid;
}
}
if( r == n+1 ){
puts("No Solution");
}else{
printf("%d\n",r);
}
}
return 0;
}
第三种
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 30;
const int maxm = 1005;
int tot,ver[maxm],he[maxn],ne[maxm],cost[maxm];
void add( int x,int y,int c ){
ver[++tot] = y;
ne[tot] = he[x];
he[x] = tot;
cost[tot] = c;
}
int d[maxn],cnt[maxn];
int v[maxn];
queue<int> que;
bool spfa(int s,int n){
for( int i = 0;i <= n+1;i++ ){
d[i] = -0x3f3f3f3f;
}
while(que.size())que.pop();
//memset( d,-1,sizeof(int)*(n+1) );
memset(v ,0,sizeof(int)*(n+1) );
cnt[s] = 0;d[s] = 0;v[s] = 1;
que.push(s);
while( que.size() ){
int x = que.front();que.pop();
v[x] = 0;
for( int cure = he[x];cure;cure = ne[cure] ){
int y = ver[cure],c = cost[cure];
if( d[y] < d[x] + c ){
d[y] = d[x] + c;
cnt[y] = cnt[x] + 1;
if( cnt[y] >= n ) return false;
if(!v[y]){
que.push(y);
v[y] = 1;
}
}
}
}
return true;
}
void init(int n){
memset( he,0,sizeof(int)*(n+1) );
tot = 1;
}
int R[25],b[25];
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(b,0,sizeof(b));
for( int i = 1;i <= 24;i++ )scanf("%d",&R[i]);
int n;scanf("%d",&n);
for( int i = 1;i <= n;i++ ){
int x;
scanf("%d",&x);
b[x+1]++;
}
int l = -1,r = n+1;
while( l != r-1 ) {
init(25);
int mid = l+r>>1;
add( 0,24,mid );
for (int i = 1; i <= 24; i++) {
add(i, i-1, -b[i]);
add(i-1,i,0 );
}
for (int i = 1; i <= 8; i++) {
add(i + 16, i, R[i] - mid);
}
for (int i = 9; i <= 24; i++) {
add(i-8, i, R[i]);
}
bool flag = spfa(0,25);
if(flag && d[24] == mid ){
r = mid;
}else{
l = mid;
}
}
if( r == n+1 ){
puts("No Solution");
}else{
printf("%d\n",r);
}
}
return 0;
}
我认为二分的时候判断 d[ 24 ] == mid是有用的,因为如果大于证明这个值还是偏小的。2019ccpc黑龙江的题放出来后应该可以验证我的猜想。