Pressure dp/01 Backpack
Orz Gromah
Easy to find the range of M is very small ... Only 16, then you can press, with a binary number to represent the collection of purchased items.
A simple and straightforward idea is to make $f[i][j]$ say that the first $i$ store buys the minimum price of a state set as a $j$, then we need to enumerate which items were bought in the $i$ store when we move, so that with the enumeration subset, the complexity becomes $o (n*3^m) $, It's not what we can endure ...
So what's wrong? Every time we move, we don't enumerate subsets, but we do something like 01 backpacks: (Below from Gromah)
We first made $f[i][j]=f[i-1][j]+d[i]$ to $i$ store.
Then enumerate each state $j$, and each item not in $j$ $k$, so that: $ $f [I][j+\{k\}]=min (F[i][j+\{k\}],f[i][j]+cost[i][k]) $$
This process is equivalent to a 01 backpack.
Finally, $f[i][j]=min (F[i][j],f[i-1][j]) $ to see if the purchase plan at the store $i$ is cost-effective.
Time complexity $o (nm2^m) $, spatial complexity $o (N2^M) $.
1 /**************************************************************2 problem:41453 User:tunix4 language:c++5 result:accepted6 time:6948 Ms7 memory:29440 KB8 ****************************************************************/9 Ten //Bzoj 4145 One#include <cstdio> A#include <cstring> -#include <cstdlib> -#include <iostream> the#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) + #definePB Push_back - using namespacestd; +typedefLong LongLL; AInlineintGetint () { at intR=1, v=0;CharCh=GetChar (); - for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') r=-1; - for(; isdigit (ch); Ch=getchar ()) v=v*Ten-'0'+ch; - returnr*v; - } - Const intn= the, m=1<< -, inf=0x3f3f3f3f; in /*******************template********************/ - to intn,m,f[n][m],d[n],c[n][ -]; + intMain () { - #ifndef Online_judge theFreopen ("4145.in","R", stdin); *Freopen ("4145.out","W", stdout); $ #endifPanax NotoginsengN=getint (); m=getint (); -F (I,1, N) { thed[i]=getint (); +Rep (j,m) c[i][j]=getint (); A } theRep (J,1<<M) f[0][j]=INF; +f[0][0]=0; -F (I,1, N) { $Rep (J,1<<M) f[i][j]=f[i-1][j]+D[i]; $Rep (J,1<<m) Rep (k,m) { - ints=1<<K; - if((j&s) = =0) F[i][j^s]=min (f[i][j^s],f[i][j]+c[i][k]); the } -Rep (J,1<<m) F[i][j]=min (f[i][j],f[i-1][j]);Wuyi } theprintf"%d\n", f[n][(1<<M)-1]); - return 0; Wu } -
View Code 4145: [amppz2014]the prices Time limit:20 Sec Memory limit:256 MB
Submit:70 solved:47
[Submit] [Status] [Discuss] Description you want to buy one of the M-type items, there are n shops, you go to the first store of D[i], at the first store to buy J items of the cost of c[i][j], to find the minimum total cost. The first line of input contains two positive integer n,m (1<=n<=100,1<=m<=16), which indicates the number of stores and items. Next n rows, the first positive integer per line d[i] (1<=d[i]<=1000000) represents the fare to the shop I store, followed by M positive integers, which in turn represent C[i][j] (1<=c[i][j]<=1000000). Output a positive integer, which is the minimum total cost. Sample Input3 4
5 7 3) 7 9
2 1 20) 3 2
8 1 1 1Sample Output -HINT
Buy Item 2nd at the first store and buy the rest in the second store.
Source
Acknowledgement Claris Upload
[Submit] [Status] [Discuss]
"Bzoj" "4145" "AMPPZ2014" the prices