problemscpp
A collection of my answers to algorithm problems in c++.
| 函数
acwing::acwing2 命名空间参考

  1. 01背包问题
更多...

struct  status
 

函数

int main (istream &cin, ostream &cout)
 
 TEST (acwing2, case1)
 

详细描述

  1. 01背包问题

函数说明

◆ main()

int acwing::acwing2::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing408.cpp1806 行定义.

1806 {
1807 int N, V;
1808 cin >> N >> V;
1809 vector<int> v = vector<int>(N);
1810 vector<int> w = vector<int>(N);
1811 vector<status> dp = vector<status>(V + 1, status{
1812 .w = 0,
1813 .free = vector<bool>(N, true)});
1814 for(int i = 0; i < N; i++) {
1815 cin >> v[i] >> w[i];
1816 }
1817 for(int i = 0; i <= V; i++) {
1818 for(int j = 0; j < N; j++) {
1819 if(dp[i].free[j]) {
1820 int next_w = dp[i].w + w[j];
1821 int next_v = i + v[j];
1822 if(next_v <= V && next_w > dp[next_v].w) {
1823 dp[next_v].w = next_w;
1824 dp[next_v].free = vector<bool>(dp[i].free);
1825 dp[next_v].free[j] = false;
1826 }
1827 }
1828 }
1829 }
1830 cout << dp[V].w;
1831 return 0;
1832 }
const int N
Definition: acwing.h:146

引用了 acwing::acwing2019::N , 以及 acwing::acwing2::status::w.

被这些函数引用 TEST().

◆ TEST()

acwing::acwing2::TEST ( acwing2  ,
case1   
)

在文件 acwing408_test.cpp1990 行定义.

1990 {
1991 istringstream in("4 5\n"
1992 "1 2\n"
1993 "2 4\n"
1994 "3 4\n"
1995 "4 5");
1996 auto out = ostringstream();
1997 main(in, out);
1998 const auto ans = out.str();
1999 ASSERT_EQ("8", ans);
2000 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().