UVA 10817 headmaster ' s headache

Source: Internet
Author: User

Original question:
The headmaster of Spring Field School is considering employing some new teachers for certain subjects.
There is a number of teachers applying for the posts. Each teacher are able to teach one or more subjects. The headmaster wants to select applicants so, each subject are taught by at least
Teachers, and the overall cost is minimized.
Input
The input consists of several test cases. The format of each of them are explained Below:the first line contains three positive integers s, M and N. S (≤8) is the Number of subjects, M (≤20) is the number of serving teachers, and N (≤100) is the number of applicants. Each of the following M lines describes a serving teacher. It first gives the cost of employing him/her (10000≤c≤50000), and followed by a list of subjects that he/she can teach. The subjects is numbered from 1 to S. You must keep on employing all of them. After that there is N lines, giving the details of the applicants in the same format.
Input is terminated by a null case where S = 0. This case is should not being processed.
Output
For each test case, give the minimum cost to employ the teachers under the constraints.
Sample Input
2 2 2
10000 1
20000 2
30000 1 2
40000 1 2
0 0 0
Sample Output
60000


English:
Give you three numbers s,m,n
How many courses are there, how many serving teachers are there, and how many teachers are candidates?
And then I'll give you n+m, every teacher's salary and lessons to teach.



Now requires at least two teachers in each class to teach, each teacher can teach many courses, in-service teachers do not dismiss, ask you to meet the minimum of two teachers in each course, how much money to pay to hire a teacher


#include<bits/stdc++.h>
using namespace std;
const int maxn=1<<8;
int dp[150][maxn][maxn];
int n,m,s,st[150],c[150];
const int inf=121*50000;
int dfs(int i,int s0,int s1,int s2)
{
    if(i==m+n)
    {
        return s2==(1<<s)-1 ? 0 : inf;
    }
    int& ans=dp[i][s1][s2];
    if(ans>=0)
        return ans;
    ans=inf;
    if(i>=m)
        ans=dfs(i+1,s0,s1,s2);
    int m0=st[i]&s0,m1=st[i]&s1;
    s0^=m0;
    s1=(s1^m1)|m0;
    s2|=m1;
    ans=min(ans,c[i]+dfs(i+1,s0,s1,s2));
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    string line;
    while(cin>>s>>m>>n)
    {
        cin.ignore();
        if(s==0)
            return 0;
        memset(dp,-1,sizeof(dp));
        memset(st,0,sizeof(st));
        for(int i=0;i<n+m;i++)
        {
            string tmp;
            int res;
            getline(cin,tmp);
            stringstream ss(tmp);
            ss>>c[i];
            while(ss>>res)
            {
                st[i]|=(1<<(res-1));
            }
            //cout<<st[i]<<endl;
        }

        int ans=dfs(0,(1<<s)-1,0,0);
        cout<<ans<<endl;
    }
    return 0;
}


Answer:



Purple book above the example, but also with code ~



First, from the data point of view, we can see that the problem needs to be solved by state compression method.
The state transition equation does not take into account that at least two teachers are taught in each course and there are no in-service teachers
Dp[s]=min (Dp[s-{appi}]+money[i])
where S is used to indicate whether each course is taught by state compression, {Appi} indicates the course that candidate I can teach



However, if you consider the requirements in the topic, it is easy to think of preserving the state s of each course, and this s is best to see how many people are taught in each course. So down, you can think of setting the state Dp[s][i] means that each course in the case of State S, select the minimum amount of money to be spent on the first I candidate.
However, the key is how the state of this lesson can represent how many people are taught in each lesson.



Consider the limit data situation, there are 100 candidates, counted on the number of serving teachers 120, assuming that each teacher can teach the most 8 courses, then there is 2^ (120x8) state. Obviously not.



In the purple book, processing is very ingenious, using two states, S1 and S2, respectively, only one person to teach the state and more than two people teach the state, so down, do not need to consider the extra two people teach the subject of how many people ~ so that the realization of the definition of state



The transfer equation of state compression is still very good to think, see the code can understand


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.