Employment Planning
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4164 Accepted Submission(s): 1743
Problem Description
A project manager wants to determine the number of the workers needed in every month. He does know the minimal number of the workers needed in each month. When he hires or fires a worker, there will be some extra cost. Once a worker is hired, he will get the salary even if he is not working. The manager knows the costs of hiring a worker, firing a worker, and the salary of a worker. Then the manager will confront such a problem: how many workers he will hire or fire each month in order to keep the lowest total cost of the project.
Input
The input may contain several data sets. Each data set contains three lines. First line contains the months of the project planed to use which is no more than 12. The second line contains the cost of hiring a worker, the amount of the salary, the cost of firing a worker. The third line contains several numbers, which represent the minimal number of the workers needed each month. The input is terminated by line containing a single '0'.
Output
The output contains one line. The minimal total cost of the project.
Sample Input
3 4 5 6 10 9 11 0
Sample Output
199
Source
Recommend
Ignatius
正确转移:
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson num<<1,le,mid
#define rson num<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
using namespace std;
const int maxn=1000+20 ;
//const int maxm= ;
//const int INF= ;
//typedef long long ll;
const int INF=0x3f3f3f3f;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int dp[14][maxn];
int a[14];
int main()
{
//我从来不贴代码
int n;int hire,salary,fire;
while(~scanf("%d",&n)&&n)
{
scanf("%d%d%d",&hire,&salary,&fire);
int maxi=0;
FOR1(i,n) scanf("%d",&a[i]),maxi=max(maxi,a[i]);
a[0]=0;
for(int i=0;i<=maxi;i++) dp[0][i]=INF;
dp[0][0]=0;
maxi=0;
for(int i=1;i<=n;i++)
{
for(int j=a[i];j<=max(a[i],maxi);j++)
{
if( j>a[i-1] ) dp[i][j]=dp[i-1][a[i-1]]+ hire*(j-a[i-1] )+salary*(j);
else dp[i][j]=dp[i-1][a[i-1]]+fire*(a[i-1]-j)+salary*(j);
for(int k=a[i-1]+1;k<= maxi ;k++)
{
if( j>k )
dp[i][j]=min(dp[i][j],dp[i-1][k]+hire*(j-k)+salary*j);
else
dp[i][j]=min(dp[i][j],dp[i-1][k]+fire*(k-j)+salary*j);
}
}
maxi=max(a[i],maxi);
}
int ans=INT_MAX;
for(int i=a[n];i<=maxi;i++)
ans=min(ans,dp[n][i]);
printf("%d\n",ans);
}
return 0;
}
错误转移:
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson num<<1,le,mid
#define rson num<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
using namespace std;
const int maxn=1000+20 ;
//const int maxm= ;
//const int INF= ;
//typedef long long ll;
const int INF=0x3f3f3f3f;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int dp[14][maxn];
int a[14];
int main()
{
//我从来不贴代码
int n;int hire,salary,fire;
while(~scanf("%d",&n)&&n)
{
scanf("%d%d%d",&hire,&salary,&fire);
int maxi=0;
FOR1(i,n) scanf("%d",&a[i]),maxi=max(maxi,a[i]);
a[0]=0;
for(int i=0;i<=maxi;i++) dp[0][i]=INF;
dp[0][0]=0;
maxi=0;
for(int i=1;i<=n;i++)
{
for(int j=a[i];j<=max(a[i],maxi);j++)
{
// if( j>a[i-1] ) dp[i][j]=dp[i-1][a[i-1]]+ hire*(j-a[i-1] )+salary*(j);
// else dp[i][j]=dp[i-1][a[i-1]]+fire*(a[i-1]-j)+salary*(j);
dp[i][j]=INF;
}
for(int k=a[i-1];k<= maxi ;k++)
{
if( k<=a[i])
dp[i][a[i]]=min(dp[i][a[i]],dp[i-1][k]+hire*(a[i]-k)+salary*a[i]);
else if( salary<fire )
dp[i][k]=min(dp[i][k],dp[i-1][k]+salary*k);
else if(salary >fire)
dp[i][a[i]]=min( dp[i][a[i]],dp[i-1][k]+fire*(k-a[i])+salary*(a[i]) );
else if(salary==fire)
{
for(int left=a[i];left<=k;left++ )
dp[i][left]=min(dp[i][left],dp[i-1][k]+fire*(k-left)+salary*left );
}
/*
if( j>k )
dp[i][j]=min(dp[i][j],dp[i-1][k]+hire*(j-k)+salary*j);
else
dp[i][j]=min(dp[i][j],dp[i-1][k]+fire*(k-j)+salary*j);*/
}
maxi=max(a[i],maxi);
}
int ans=INT_MAX;
for(int i=a[n];i<=maxi;i++)
ans=min(ans,dp[n][i]);
printf("%d\n",ans);
}
return 0;
}