UVA 10817——Headmaster's Headache(DP)

本文探讨了Springfield学校校长在招聘教师时面临的挑战,如何在满足每个科目至少两位教师授课的同时,实现成本最小化。通过分析输入案例,提出了一种策略来解决这一问题,并给出了具体的最小成本解决方案。

Problem D: Headmaster's Headache

Time limit: 2 seconds

The headmaster of Spring Field School is considering employing some new teachers for certain subjects. There are a number of teachers applying for the posts. Each teacher is able to teach one or more subjects. The headmaster wants to select applicants so that each subject is taught by at least two teachers, and the overall cost is minimized.

Input

The input consists of several test cases. The format of each of them is explained below:

The first line contains three positive integers SM and NS (≤ 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), followed by a list of subjects that he/she can teach. The subjects are numbered from 1 to SYou must keep on employing all of them. After that there are N lines, giving the details of the applicants in the same format.

Input is terminated by a null case where S = 0. This case should not be 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

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <queue>
#include <map>
using namespace std;

const int N = 100 + 10;
const int MOD = 1e9 + 7;
const int INF = 1e9;

int n,m,s;
int dp[N][(1<<16)+2];
int T[N];
int C[N];
int state,cost;

void init()
{
    state=0;
    cost=0;
    memset(T,0,sizeof(T));
    memset(C,0,sizeof(C));

    char c;
    int t;
    for(int i=0;i<m;i++)
    {
        scanf("%d",&t);
        cost += t;
        while((c = getchar()) != '\n')
        {
            if(c==' ') continue;
            else t=c-'0';
            if(state & 1<<(t-1)) state |= 1<<(t-1+s);
            else state |= 1<<(t-1);
        }
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&C[i]);
        while((c = getchar()) != '\n')
        {
            if(c==' ') continue;
            else t=c-'0';

            T[i] |= 1<<(t-1);
        }
    }
}

void solve()
{
    memset(dp,-1,sizeof(dp));
    int limit = (1<<(2*s)) - 1;

    dp[0][state] = cost;

    for(int i=0;i<n;i++)
    {
        for(int j=state;j<=limit;j++)
        {
            if(dp[i][j]==-1) continue;

            int k = j&T[i];
            k = k<<s;
            int st = j|T[i];
            st|=k;
            st %= 1<<(s*2);

            if(dp[i+1][st]==-1)
                dp[i+1][st] = dp[i][j]+C[i];
            else
                dp[i+1][st] = min(dp[i][j]+C[i],dp[i+1][st]);

            if(dp[i+1][j]==-1)
                dp[i+1][j]=dp[i][j];
            else
                dp[i+1][j]=min(dp[i+1][j],dp[i][j]);
        }
    }
    printf("%d\n",dp[n][limit]);
}

int main()
{
#ifdef tyh
    freopen("in.txt","r",stdin);
#endif // tyh
    while(scanf("%d%d%d",&s,&m,&n)!=EOF && n+m+s)
    {
        init();
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值