Poj 3211 cycling clothes (01 backpack)

Source: Internet
Author: User

Link:

Http://poj.org/problem? Id = 3211

Question:

Dearboy and his girlfriend wash M pieces of clothing, total N colors (each dress has only one color ). In order to place different colors for mutual dyeing, only one color of clothes can be washed at a time, and no other clothes can be washed before the clothes are washed. Dearboy and his girlfriend can wash clothes at the same time, but they cannot wash the same dress at the same time or in different colors at the same time.

The time required for each piece of clothing is as long as the minimum amount of time can be completely washed.

Analysis:

First of all, we can intuitively determine that we must finish all the clothes of a color and then wash the clothes of another color. The total time for washing all the clothes of each color is separated.

The key is how can dearboy and his girlfriend allocate tasks to complete the same color of clothes as quickly as possible? In the most ideal case, the two wash each half of the time. Therefore, we should try our best to allocate the task length to the two people close to half of the total length. The time spent in washing the clothes in a single color depends on the person with a long length.

Obviously, you can use a 01 backpack. The time for each piece of clothing is capacity and value. Half of the total time is the size of the backpack, and then the 01 backpack.

Code:

# Include <iostream> # include <queue> # include <cstdio> # include <cstring> # include <cmath> # include <map> # include <string> # define MP make_pair # define Sq (X) (x) * (x) using namespace STD; typedef long int64; const int maxn = 110; const int INF = 0x3f3f3f; int n, m; int H [25]; int W [12] [maxn], sum [12], num [12]; int f [maxn * 1000]; int main () {char color [12]; int t; while (~ Scanf ("% d", & N, & M) & N + M) {Map <string, int> MP; For (INT I = 0; I <n; ++ I) {scanf ("% s", color); MP [color] = I;} For (INT I = 0; I <= N; ++ I) sum [I] = num [I] = 0; For (INT I = 0; I <m; ++ I) {scanf ("% d % s", & T, color); int Pt = MP [color]; W [pt] [num [pt] ++] = T; sum [pt] + = T;} int ans = 0; For (int K = 0; k <n; ++ K) {for (INT I = 0; I <= sum [k]/2; ++ I) f [I] = 0; For (INT I = 0; I <num [k]; ++ I) {for (INT v = (sum [k]> 1); V> = W [k] [I]; -- V) f [v] = max (F [v], F [V-W [k] [I] + W [k] [I]);} ans + = sum [k]-f [sum [k]> 1];} printf ("% d \ n", ANS);} return 0 ;}

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.