题目描述:鱼的种类有多种,但有些鱼会互相攻击对方,在给定一定数目的钱时,怎么买尽可能多的鱼,并且要求找出在买的鱼数目相同的情况下所花的钱是最多的一个方案。
测试用例
输入
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->100010
1078
9179
89
7344
676
5224
4127
391
2276
147
109
106
97
91
82
76
74
72
63
53
52
31
00
1078
9179
89
7344
676
5224
4127
391
2276
147
109
106
97
91
82
76
74
72
63
53
52
31
00
输出
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->5702
1
5
7
8
10
1
5
7
8
10
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<iostream>
usingnamespacestd;
constintMAXSIZE=31;//鱼的最大种类数
intm,n;//输入的钱数和鱼种类数
boolattack[MAXSIZE][MAXSIZE];//鱼之间的攻击性
intfish[MAXSIZE];//鱼的价格
intp[MAXSIZE];//买鱼的策略
intpbest[MAXSIZE];//买鱼的最佳策略
intcone,best;//买鱼的数目,最优数目
intsum,sumbest;//买鱼的花费,最优花费
voidSolve(intt)
{
boolbb;
inti;
p[t]=-1;
do
{
p[t]=p[t]+1;
if(p[t]==1)
{//买下这种鱼
++cone;
sum+=fish[t];
}
//钱还有剩余
if(sum<=m)
{
bb=true;
}
else
bb=false;
if(bb==true&&p[t]==1)
{
for(i=n;i>t;--i)
{
//判断当前鱼与前面选择的是否互相攻击
if(p[i]==1&&attack[i][t]==true)
{
bb=false;
break;
}
}
}
if(bb==true)
{
if(t==1)
{//到最后一种鱼了
if(cone>best||(cone==best&&sum>sumbest))
{//找到一个更优解
best=cone;
sumbest=sum;
for(i=1;i<MAXSIZE;++i)
{
pbest[i]=p[i];
}
}
}
else
{//继续向下搜索
Solve(t-1);
}
}
if(p[t]==1)
{//恢复到不买这种鱼的状态
--cone;
sum-=fish[t];
}
}while(p[t]!=1);
}
voidOutput()
{//输出最优解
cout<<best<<""<<sumbest<<endl;
for(inti=1;i<=n;++i)
{
if(pbest[i]==1)
{
cout<<i<<endl;
}
}
}
intmain()
{
inti,nId,nPrice,s,t;
cin>>m>>n;
//各种鱼的价格
for(i=0;i<n;++i)
{
cin>>nId>>nPrice;
fish[nId]=nPrice;
}
//鱼之间互相攻击对方的关系
while(cin>>s>>t&&(s!=0&&t!=0))
{
attack[s][t]=true;
attack[t][s]=true;
}
best=0;//鱼的最优数目
sumbest=0;//鱼的最优花费
Solve(n);
Output();
system("pause");
return0;
}
usingnamespacestd;
constintMAXSIZE=31;//鱼的最大种类数
intm,n;//输入的钱数和鱼种类数
boolattack[MAXSIZE][MAXSIZE];//鱼之间的攻击性
intfish[MAXSIZE];//鱼的价格
intp[MAXSIZE];//买鱼的策略
intpbest[MAXSIZE];//买鱼的最佳策略
intcone,best;//买鱼的数目,最优数目
intsum,sumbest;//买鱼的花费,最优花费
voidSolve(intt)
{
boolbb;
inti;
p[t]=-1;
do
{
p[t]=p[t]+1;
if(p[t]==1)
{//买下这种鱼
++cone;
sum+=fish[t];
}
//钱还有剩余
if(sum<=m)
{
bb=true;
}
else
bb=false;
if(bb==true&&p[t]==1)
{
for(i=n;i>t;--i)
{
//判断当前鱼与前面选择的是否互相攻击
if(p[i]==1&&attack[i][t]==true)
{
bb=false;
break;
}
}
}
if(bb==true)
{
if(t==1)
{//到最后一种鱼了
if(cone>best||(cone==best&&sum>sumbest))
{//找到一个更优解
best=cone;
sumbest=sum;
for(i=1;i<MAXSIZE;++i)
{
pbest[i]=p[i];
}
}
}
else
{//继续向下搜索
Solve(t-1);
}
}
if(p[t]==1)
{//恢复到不买这种鱼的状态
--cone;
sum-=fish[t];
}
}while(p[t]!=1);
}
voidOutput()
{//输出最优解
cout<<best<<""<<sumbest<<endl;
for(inti=1;i<=n;++i)
{
if(pbest[i]==1)
{
cout<<i<<endl;
}
}
}
intmain()
{
inti,nId,nPrice,s,t;
cin>>m>>n;
//各种鱼的价格
for(i=0;i<n;++i)
{
cin>>nId>>nPrice;
fish[nId]=nPrice;
}
//鱼之间互相攻击对方的关系
while(cin>>s>>t&&(s!=0&&t!=0))
{
attack[s][t]=true;
attack[t][s]=true;
}
best=0;//鱼的最优数目
sumbest=0;//鱼的最优花费
Solve(n);
Output();
system("pause");
return0;
}