zoukankan      html  css  js  c++  java
  • POJ 1275 Cashier Employment

    传送门

    AcWing 393 雇佣收银员HDOJ 1529 Cashier EmploymentZOJ 1420 Cashier EmploymentUVALive 2058 Cashier Employment

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> p;
    const int maxn(30);
    const int maxm(1e2);
    bool vis[maxn];
    int r[maxn], num[maxn];
    int ecnt, head[maxn], cnt[maxn], dis[maxn];
    
    struct edge {
        int to, wt, nxt;
    } edges[maxm];
    
    template<typename T>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, bool ln)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (ln) putchar(10);
    }
    
    void addEdge(int u, int v, int w)
    {
        edges[ecnt].to = v;
        edges[ecnt].wt = w;
        edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
    
    void build(int s24)
    {
        ecnt = 0;
        memset(head, -1, sizeof head);
        for (int i = 1; i <= 24; ++i) {
            addEdge(i - 1, i, 0);
            addEdge(i, i - 1, -num[i]);
        }
        for (int i = 8; i <= 24; ++i) {
            addEdge(i - 8, i, r[i]);
        }
        for (int i = 1; i <= 7; ++i) {
            addEdge(i + 16, i, r[i] - s24);
        }
        addEdge(0, 24, s24);
        addEdge(24, 0, -s24);
    }
    
    bool spfa(int s24)
    {
        build(s24);
        memset(dis, -0x3f, sizeof dis);
        memset(cnt, 0, sizeof cnt);
        memset(vis, false, sizeof vis);
        dis[0] = 0;
        vis[0] = true;
        queue<int> q;
        q.push(0);
        while (not q.empty()) {
            int u = q.front();
            q.pop();
            vis[u] = false;
            for (int i = head[u]; compl i; i = edges[i].nxt) {
                int v = edges[i].to, w = edges[i].wt;
                if (dis[v] < dis[u] + w) {
                    dis[v] = dis[u] + w;
                    if (not vis[v]) {
                        vis[v] = true;
                        q.push(v);
                        cnt[v] = cnt[u] + 1;
                        if (cnt[v] == 25) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        ios::sync_with_stdio(false);
        int t = read<int>();
        while (t--) {
            memset(num, 0, sizeof num);
            for (int i = 1; i <= 24; ++i) {
                r[i] = read<int>();
            }
            int n = read<int>();
            for (int i = 0; i < n; ++i) {
                ++num[read<int>() + 1];
            }
            bool flag = false;
            for (int i = 0; i <= 1000; ++i) {
                if (spfa(i)) {
                    flag = true;
                    printf("%d
    ", i);
                    break;
                }
            }
            if (not flag) {
                printf("No Solution
    ");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    解决UITableView中Cell重用机制导致内容出错的方法总结
    Hdu 1052 Tian Ji -- The Horse Racing
    Hdu 1009 FatMouse' Trade
    hdu 2037 今年暑假不AC
    hdu 1559 最大子矩阵
    hdu 1004 Let the Balloon Rise
    Hdu 1214 圆桌会议
    Hdu 1081 To The Max
    Hdu 2845 Beans
    Hdu 2955 Robberies 0/1背包
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13857255.html
Copyright © 2011-2022 走看看