随笔分类 -  ACM~~背包问题

摘要:1073. Square CountryTime Limit: 1.0 secondMemory Limit: 64 MBThere live square people in a square country. Everything in this country is square also. Thus, the Square Parliament has passed a law about a land. According to the law each citizen of the country has a right to buy land. A land is sold in 阅读全文
posted @ 2013-04-22 09:16 OpenSoucre 阅读(266) 评论(0) 推荐(0)
摘要:1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <cstring> 5 6 #define MAX 1001 7 using namespace std; 8 9 struct cloth{10 int xi,yi,ci;11 };12 int dp[MAX][MAX]={0};13 14 int main(){15 int T;16 cin >> T;17 while(T--){18 int N,X,Y;19 cin >.. 阅读全文
posted @ 2013-04-09 23:18 OpenSoucre 阅读(184) 评论(0) 推荐(0)
摘要:此题一定要注意输入是 n || m 不是 n&&m, 就因为这个WA了几次此题是01背包的增加型,不要完全套01背包,由于是概率所以要考虑乘法,题目要求的是至少被录取,根据概率论,求其反面简单 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <cstdio> 5 #include <cstring> 6 7 using namespace std; 8 9 int main(){10 int n,m;11 while 阅读全文
posted @ 2013-04-09 21:43 OpenSoucre 阅读(156) 评论(0) 推荐(0)
摘要:像下面代码直接利用二进制求解多重背包会超时 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <vector> 5 #include <algorithm> 6 7 using namespace std; 8 9 int main(){10 int n,m;11 while(cin>>n>>m && n && m){12 vector<int> A(n),C(n);13 阅读全文
posted @ 2013-04-09 17:34 OpenSoucre 阅读(187) 评论(0) 推荐(0)
摘要:多维费用背包 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 struct good { 8 int a,b,val; 9 };10 11 int main(){12 int n,v1,v2,k;13 while(cin >> n>>v1>>v2>>k){14 vector<good> commodity(n);15 for(int i = 0; i < 阅读全文
posted @ 2013-04-09 15:39 OpenSoucre 阅读(244) 评论(0) 推荐(0)
摘要:利用传统的完全背包解法会超时,下面代码超时 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 #define INF 1<<30 6 using namespace std; 7 8 int main(){ 9 int T;10 cin >> T;11 while(T--){12 int E,F;13 cin >>E>>F;14 int pigWeight =F-E;15 int n;16 cin >... 阅读全文
posted @ 2013-04-08 23:03 OpenSoucre 阅读(172) 评论(0) 推荐(0)
摘要:此题实在无语就因为把判断输入条件写成 n!=-1就WA了几次,自己以后要多注意 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <cstdio> 5 #include <cmath> 6 7 using namespace std; 8 9 int main(){10 int n;11 while(cin >> n && n >= 0){12 int v,num,sumV = 0;13 vector 阅读全文
posted @ 2013-04-08 22:22 OpenSoucre 阅读(159) 评论(0) 推荐(0)
摘要:1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 int main(){ 8 int n; 9 while(cin >>n && n){10 vector<int> food(n);11 for(int i = 0; i < n; i ++ ) cin >> food[i];12 int price;13 cin >> price;14 if(price 阅读全文
posted @ 2013-04-08 21:25 OpenSoucre 阅读(180) 评论(0) 推荐(0)
摘要:1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 int main(){ 8 int T; 9 cin >> T;10 while(T--){11 int N, V;12 cin >> N>>V;13 vector<int> value(N), volume(N);14 for(int i = 0; i < N; i ++) cin >>value[i];1 阅读全文
posted @ 2013-04-08 21:01 OpenSoucre 阅读(142) 评论(0) 推荐(0)
摘要:每种物品只有一件的是01背包每种物品有无限件的是完全背包每种物品有有限件的是多重背包 (利用二进制思想,转化为01背包)每种物品有多重价值的是二维费用背包这几种背包基本的动态转移方程 dp[i][j] 从前i件物品选择若干物品装到容量为j的背包中产生的最大价值 dp[i][j] = max{ dp[i-1][j] , dp[i][j - c[i] ] + w[i] } 阅读全文
posted @ 2013-04-08 20:43 OpenSoucre 阅读(284) 评论(0) 推荐(0)
摘要:1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <cstring> 5 #include <cstdio> 6 7 using namespace std; 8 9 int main(){10 int n;11 cin >>n;12 vector<int> stone(n+1,0);13 int sum = 0;14 for(int i = 1; i <= n; i ++){15 cin >> 阅读全文
posted @ 2013-04-08 19:33 OpenSoucre 阅读(1540) 评论(0) 推荐(1)