zoukankan      html  css  js  c++  java
  • 【UVa】Headmaster's Headache(状压dp)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1758

    晕。。。。状压没考虑循环方向然后错了好久。。

    这点要注意。。。(其实就是01背包变成了完全背包QAQ

    我们将课程拆成两个点,然后状压

    那么答案就是(1<<(s<<1))-1

    转移就不说了,,,,,太简单。。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    typedef long long ll;
    #define pii pair<int, int>
    #define pii pair<int, int>
    #define mkpii make_pair<int, int>
    #define pdi pair<double, int>
    #define mkpdi make_pair<double, int>
    #define pli pair<ll, int>
    #define mkpli make_pair<ll, int>
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << (#x) << " = " << (x) << endl
    #define error(x) (!(x)?puts("error"):0)
    #define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
    #define printarr1(a, b) for1(_, 1, b) cout << a[_] << '	'; cout << endl
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    inline const int max(const int &a, const int &b) { return a>b?a:b; }
    inline const int min(const int &a, const int &b) { return a<b?a:b; }
    
    const int N=17, oo=0x3f3f3f3f;
    int f[1<<N], n, m, s, sum[N];
    struct dat { int c, s; }a[105], b[105];
    
    void readin(dat arr[], int n) {
        for1(i, 1, n) {
            arr[i].s=0; arr[i].c=oo;
            read(arr[i].c); //dbg(arr[i].c);
            char ch=getchar();
            int r=0;
            while(ch!='
    ' && ch!='
    ') {
                if(ch<'0'||ch>'9') {
                    if(r!=0) arr[i].s|=(3<<((r-1)<<1));
                    r=0;
                    while(ch<'0'||ch>'9') {
                        ch=getchar();
                        if(ch=='
    ' || ch=='
    ') break;
                    }
                    if(ch=='
    ' || ch=='
    ') break;
                }
                r=r*10+ch-'0';
                ch=getchar();
            }
            if(r!=0) arr[i].s|=(3<<((r-1)<<1));
            //rep(k, s) if(arr[i].s&(3<<(k<<1))) printf("%d ", k+1);
            //puts("");
        }
    }
    void Prin(int x) {
        for3(k, ((s-1)<<1)+1, 0) { printf("%d", (bool)(x&(1<<k))); if((k&1)==0) printf(" "); }
        puts("");
    }
    int main() {
        read(s); read(n); read(m);
        while(s) {
            int all=(1<<(s<<1))-1;
            CC(f, 0x3f); CC(sum, 0);
            readin(a, n); readin(b, m);
    
            int tc=0, tn=0;
            for1(i, 1, n) rep(k, s) if((a[i].s>>(k<<1))&3) sum[k]++;
            rep(k, s) if(sum[k]) {
                int t=1;
                if(sum[k]>=2) t=3;
                tc|=(t<<(k<<1));
            }
            for1(i, 1, n) tn+=a[i].c;
            f[tc]=tn;
    
            for1(i, 1, m) {
                //dbg(i);
                for3(j, all, 0) if(f[j]!=oo) {
                int nx=0; //Prin(j);
                rep(k, s) if(b[i].s&(3<<(k<<1))) {
                    int now=(j>>(k<<1))&3;
                    if(now==0 || now==2) nx|=1<<(k<<1);
                    if(now==1 || now==3) nx|=2<<(k<<1);
                }
                f[j|nx]=min(f[j|nx], f[j]+b[i].c); //Prin(nx); Prin(j|nx); puts("");
            } }
            if(f[all]==oo) f[all]=-1;
            printf("%d
    ", f[all]);
            read(s); read(n); read(m);
        }
        return 0;
    }
    

      

  • 相关阅读:
    the process cannot access the file because it is being used by another process
    SharePoint 2013: The "New Web Application" button is disabled is the central administration
    【转】Windows按键消息—虚拟键码
    【转】windows消息16进制对应表
    C#将图片字节流转为Base64直接放入html的img标签src属性中
    WPF自定义Main函数
    c#geckofx文件流下载
    腾讯RTX登录提示失败问题及处理办法
    C# \u8888类型的unicode转换为字符串方法
    C#System.Net.Mail采用简单邮件传输协议发送邮件
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/4069926.html
Copyright © 2011-2022 走看看