小车过桥有没有讨论的?
不知道a了多少,最后没时间写完了……来讨论下
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int N, W;
cin >> N >> W;
vector<int> w(N, 0);
vector<int> t(N, 0);
for (int i = 0; i < N; i++)
{
cin >> w[i];
}
int T = 0;
for (int i = 0; i < N; i++)
{
cin >> t[i];
T += t[i];
}
vector< vector<int>> dp(N, vector<int>(T, 0));
for (int j = 0; j < T; j++)
{
if (t[0] > j)
dp[0][j] = w[0];
else
break;
}
for (int i = 1; i < N; i++)
{
int time = 0;
int index = 0;
bool flag = 1;
for (int j = 0; j < T; j++)
{
dp[i][j] = dp[i - 1][j];
if (flag && W - dp[i - 1][j] >= w[i])
{
index = j; flag = 0;
}
if (W - dp[i - 1][j] >= w[i] && (t[i] + index > j))
{
dp[i][j] = w[i] + dp[i - 1][j];
}
}
}
bool flag = 1;
for (int j = 0; j < T; j++)
{
if ( dp[N - 1][j] == 0)
{
cout<<j;
break;
}
}
system("pause");
return 0;
}