Bzoj 4145: [amppz2014]the prices (pressure DP + 01 backpack)

Source: Internet
Author: User

I can only think of O (n*3^m) approach .... Will definitely t

O (nm*2^m) Practice:

DP (x, s) indicates that the first X stores are considered, and the set of items bought is S.

Consider transfer: First suppose we go to the X store, so initial DP (x, s) = DP (x-1, s) + d[x]

We can then make 01 backpacks for the X shop, DP (x, S + {h}) = Min (DP (x, S + {h}), DP (x, s) + c[x][h])) (h∉s).

Then we'll compare it to the first X shop. No cost: DP (x, s) = Min (DP (x-1, s), DP (x, s))

Answer = DP (M, {1, 2, ... n})

---------------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#define REP (i, n) for (int i = 0; i < n; i++)#define CLR (x, C) memset (x, C, sizeof (x))#define B (i) (1 << (i))using namespace std;const int MAXN = MAXM = +, oo = int (1e9); int D, C[MAXM], dp[2][b (MAXM)], A = 0, b = 1;inline int Read () {char C = getchar ();For (;!isdigit (c); c = GetChar ());int ans = 0;For (; IsDigit (c); c = GetChar ())ans = ans * + C-' 0 ';return ans;}int main () {freopen ("test.in", "R", stdin);int n = read (), M = Read (), all = B (m);Rep (s, all) dp[a][s] = oo;dp[a][0] = 0;Rep (i, N) {swap (A, B);d = Read ();Rep (J, m) c[j] = read ();Rep (s, all) dp[a][s] = Dp[b][s] + D;Rep (J, M)Rep (s, all) if (! ( S & B (j)))Dp[a][s | b (j)] = min (Dp[a][s | b (j)], Dp[a][s] + c[j]);Rep (s, all) dp[a][s] = min (Dp[a][s], dp[b][s]);}printf ("%d\n", Dp[a][all-1]);return 0;}

---------------------------------------------------------------------------------

4145: [Amppz2014]the Prices Time Limit:Sec Memory Limit:+ MB
Submit:156 Solved:99
[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 Input 3 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

Bzoj 4145: [amppz2014]the prices (pressure DP + 01 backpack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.