WA原因：当同一种状况再次发生时直接否定这个状况

AC代码：

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>

using namespace std;

#define INF 0x3f3f3f3f
#define LL long long

struct teach
{
int t;     //count
int c[10];
int co;     //spend
}tea[110];

int me[1 << 16][105];

int a, b;
char ch;
int sum;
int s, n, m;
int init;
int ans;
int mx;

int dp(int status, int num)
{
int &k = me[status][num];

if(k != INF)
return k;

if(num < n)
{

int a, b = status;
int i, j;
for(i = 0; i < tea[num].t; i++)
{
a = tea[num].c[i];

if(b & (1 << (a + s)))
{

}
else if(b & (1 << a))
{
b += (1 << (a + s));
}
else
{
b += (1 << a);
}
}
k = min(k, min(dp(status, num + 1), dp(b, num + 1) + tea[num].co));
}
if(k == INF)
{
k = INF + 1;
}
return k;
}

int main()
{
int i, j, k;

while(scanf("%d %d %d", &s, &m, &n) != EOF)
{
ans = INF;
memset(me, 0, sizeof(me));
mx = (1 << (s * 2)) - 1;

memset(tea, 0, sizeof(tea));
init = 0;
sum = 0;

if(!s)
break;
for(i = 0; i < m; i++)
{
scanf("%d", &k);
sum += k;
while(scanf("%c", &ch))
{
if(ch >= '1' && ch <= '8')
{
a = ch - '1';

if(init & (1 << (a + s)))
{

}
else if(init & (1 << a))
{
init += (1 << (a + s));
}
else
{
init += (1 << a);
}
}
else if(ch == '\n')
break;
}
}

for(i = 0; i < n; i++)
{
scanf("%d", &tea[i].co);
while(scanf("%c", &ch))
{
if(ch == '\n')
break;
else if(ch >= '1' && ch <= '8')
{
tea[i].c[tea[i].t++] = ch - '1';
//  cout << tea[i].t << endl;
}
}
}

for(int i = init; i < mx ;i++)
for(int j = 0; j < n + 1; j++)
me[i][j] = INF;
for(int i = 0; i < n + 1; i++)
me[mx][i] = 0;

cout << dp(init, 0) + sum << endl;

}
return 0;
}

WA代码：

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>

using namespace std;

#define INF 0x3f3f3f3f
#define LL long long

struct teach
{
int t;     //count
int c[10];
int co;     //spend
}tea[110];

int me[1 << 16][10];

int a, b;
char ch;
int sum;
int s, n, m;
int init;
int ans;
int mx;

void dfs(int status, int num, int sums)
{
if(status == mx)
{
ans = min(ans, sums);
return ;
}

if(me[status][num])
{
return ;
}

me[status][num]++;

if(num >= n)
return ;

dfs(status, num + 1, sums);

int a, b = status;
int i, j;
for(i = 0; i < tea[num].t; i++)
{
a = tea[num].c[i];

if(b & (1 << (a + s)))
{

}
else if(b & (1 << a))
{
b += (1 << (a + s));
}
else
{
b += (1 << a);
}
}

dfs(b, num + 1, sums + tea[num].co);
}

int main()
{
int i, j, k;

while(scanf("%d %d %d", &s, &m, &n) != EOF)
{
ans = INF;
memset(me, 0, sizeof(me));
mx = (1 << (s * 2)) - 1;

memset(tea, 0, sizeof(tea));
init = 0;
sum = 0;
if(!s)
break;
for(i = 0; i < m; i++)
{
scanf("%d", &k);
sum += k;
while(scanf("%c", &ch))
{
if(ch >= '1' && ch <= '8')
{
a = ch - '1';

if(init & (1 << (a + s)))
{

}
else if(init & (1 << a))
{
init += (1 << (a + s));
}
else
{
init += (1 << a);
}
}
else if(ch == '\n')
break;
}
}

for(i = 0; i < n; i++)
{
scanf("%d", &tea[i].co);
while(scanf("%c", &ch))
{
if(ch == '\n')
break;
else if(ch >= '1' && ch <= '8')
{
tea[i].c[tea[i].t++] = ch - '1';
//  cout << tea[i].t << endl;
}
}
}

//   cout << mx << ' ' << init;
dfs(init, 0, 0);

cout << ans + sum << endl;

}
return 0;
}

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
09-20 130
04-22 254
08-24 99
08-24 555
10-23 102
09-07 277
09-10

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。