2015
05-23

# Help-or-else

A penal colony for finance professionals will soon be holding its annual community service activity with some rules that are considered suitable for a penal colony. Every inmate is assigned a set P of N people to help with their finances and a limit of K minutes. In addition to the circumstances of the jth person, 1 <= j <= N, a time penalty of ej for choosing not to give advice and the time duration of dj minutes allotted to provide the advice are also made clear to the inmate. An inmate starts his community service at time T equal to zero. If the inmate started working with the jth person at time T, then he must terminate his work no later than T+dj. Regardless of the validity of the advice and time of completion, a value of Cj ( = T+ dj ) is deducted from the inmate’s alloted minutes. Also the inmate is not permitted to work with another person until the time T+ dj. If S is the set of people helped by an inmate, then the total number of used minutes is calculated as
Your task is to write a program to calculate the maximum number of persons that can be helped by an inmate without exceeding his K minutes limit.

Input consists of sets for many inmates. The description for each inmate begins with two integers N and K, separated by a single space on a line by themselves, that represent the number of people and the maximum allowed minutes. 0 < N <= 200 and 0 < K <= 6000. Each of the following N lines contains two integers, separated by a single space, which represent the penalty and time duration one person to be assisted. All integers have values between 0 and 10000, inclusive. Input terminates with two zeros on a line by themselves.

Input consists of sets for many inmates. The description for each inmate begins with two integers N and K, separated by a single space on a line by themselves, that represent the number of people and the maximum allowed minutes. 0 < N <= 200 and 0 < K <= 6000. Each of the following N lines contains two integers, separated by a single space, which represent the penalty and time duration one person to be assisted. All integers have values between 0 and 10000, inclusive. Input terminates with two zeros on a line by themselves.

1 1000
100 1000
2 100
1000 1000
20 10
1 1
0 10000
4 293
61 30
295 39
206 27
94 85
0 0

1: 1
2: Mission Impossible
3: 0
4: 3

HDU 4182 Judges’ response(01背包+TSP状态压缩DP)

http://acm.hdu.edu.cn/showproblem.php?pid=4281

首先第一问:

dp1[i][j+st]= min(dp1[i-1][j+st]  , dp1[i-1][j]+1)  st与j的交集为空(可以不为空,代码实现就不没考虑空),其实就算不为空也行..

dp1[i]=min(dp1[i],dp1[i-j]+1);   j是i的子集的合法状态

第二问:

cost[i][j+{k}] = min( cost[k][ j] +dist[i][k] )  其中k不属于j.

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define INF (1<<29)
int n,m;
int x[20],y[20],c[20];
int state[1<<17],cnt;//cnt表示合法状态有多少个,state[i]=x,表示第i个合法状态是x
bool isok[1<<17];
int dp1[1<<17],cost[17][1<<17];
int dist[20][20];
int np[1<<17];
bool good(int st)
{
int sum=0;
for(int i=0;i<n;i++)if(st&(1<<i))
sum +=c[i];
return sum<=m;
}
int solve_1()
{
cnt=0;
for(int i=0;i<(1<<n);i++)
{
isok[i]=good(i);
if(isok[i]) state[cnt++]=i;
}
for(int i=0;i<(1<<n);i++)
dp1[i]=INF;
dp1[0]=0;
for(int i=0;i<cnt;i++)
{
for(int j=(1<<n)-1;j>=0;j--)if(dp1[j]!=INF)
dp1[j|state[i]]=min(dp1[j|state[i]],dp1[j]+1);
}
return dp1[(1<<n)-1];
}
int solve_2()
{
//计算任意两点间的距离
memset(dist,0,sizeof(dist));
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
dist[i][j]=dist[j][i]= ceil(sqrt((double)(x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j])));

for(int i=0;i<n;i++)
for(int j=0;j<(1<<n);j++)
cost[i][j]=INF;//cost[i][j]表示当前在i点,已经走过了集合j中的点,所需要的最短距离
cost[0][1]=0;

for(int i=0;i<(1<<n);i++)
for(int j=0;j<n;j++)if( (i&(1<<j)) )//j在集合i中
for(int k=0;k<n;k++)if( (i&(1<<k))==0 )//k不在集合i中
cost[k][i|(1<<k)] = min(cost[k][i|(1<<k)] , cost[j][i]+dist[j][k]);
for(int i=0;i<(1<<n);i++)
{
np[i]=INF;
if(isok[i])
for(int j=0;j<n;j++)if(i&(1<<j))//j在集合i中
{
np[i] = min(np[i],cost[j][i]+dist[j][0]);//这里计算出来的有效np[i]的i&1都是!=0的
//因为cost中的i&1 !=0
}
}
for(int i=1;i<(1<<n);i++)
if(i&1)for(int j=(i-1)&i;j;j=(j-1)&i)
np[i]=min(np[i],np[j|1]+np[(i-j)|1]);//如果此处不|1的话,那么有可能分成的集合j和(i-j)因为没有前置1从而是INF的值
return np[(1<<n)-1];
}
int main()
{

while(scanf("%d%d",&n,&m)==2)
{
for(int i=0;i<n;i++)
scanf("%d%d",&x[i],&y[i]);
for(int i=0;i<n;i++)
scanf("%d",&c[i]);
int ans1=solve_1();
int ans2=solve_2();
if(ans1==INF)
printf("-1 -1\n");
else
printf("%d %d\n",ans1,ans2);
}
return 0;
}