Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 718 Accepted Submission(s): 511
php
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int minn,sum,flag,n,m,ans;
int PHchange;
int hash[20];
int biji;
struct Node
{
int effect;
int double_time;
int PHchange;
};
Node map[20];
void DFS(int now)
{
//printf("now is %d PHchange is %d sum is %d\n",now,map[now].PHchange,sum);
if(map[now].PHchange >= m)
{
ans = 1;
if(sum < minn)
minn = sum;
return ;
}
for(int i = 1;i <= n;i ++)
{
if(hash[i] == 0)
{
sum ++;
hash[i] = 1;
if(m - map[now].PHchange <= map[i].double_time)
map[i].PHchange = map[now].PHchange + map[i].effect * 2;
else
map[i].PHchange = map[now].PHchange + map[i].effect;
DFS(i);
sum --;
hash[i] = 0;
}
}
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i = 0;i < 20;i ++)
{
map[i].effect = 0;
map[i].double_time = 0;
map[i].PHchange = 0;
}
memset(hash,0,sizeof(hash));
for(int i = 1;i <= n;i ++)
scanf("%d%d",&map[i].effect,&map[i].double_time);
minn = 0;flag = 0;ans = 0;
minn = 1000;
for(int i = 1;i <= n;i ++)
{
sum = 1;
biji = 0;
hash[i] = 1;
if(m <= map[i].double_time)
map[i].PHchange = map[i].effect * 2;
else
map[i].PHchange = map[i].effect;
DFS(i);
hash[i] = 0;
map[i].PHchange = 0;
}
if(ans == 0)
printf("-1\n");
else
printf("%d\n",minn);
}
}ios