天天看點

LightOJ 1350 Aladdin and the Grand Feast 最大流(難)

題目:http://www.lightoj.com/volume_showproblem.php?problem=1350

題意:要舉辦宴會,時長為e,有t個桌子,每個桌子可以坐c個人。每個賓客吃飯的時間為[a, b),要吃f機關的食物,沒人每個機關時間隻能吃一機關食物。問能不能滿足所有賓客的需求。若能,則輸出每個機關時間桌子上的賓客,多解任意輸出一個

思路:正常思路是把賓客和每個時間機關作為點,很好套路,但機關時間為1e4,肯定會T。我們隻用每個賓客吃飯的時間[a, b)這兩個時間點,這樣點就不超過200個,但是後續處理挺惡心的。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define debug() puts("here");
using namespace std;

const int N = 210, M = 10010;
const int INF = 0x3f3f3f3f;
struct edge
{
    int to, cap, next;
} g[N*N*2];
int cnt, nv, head[N], level[N], gap[N], cur[N], pre[N];
int aa[N], bb[N], cc[N], deg[M], id[M], last[M], arr[M];
char ans[M][60];
int cas;
void add_edge(int v, int u, int cap)
{
    g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
    g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;
}
int sap(int s, int t)
{
    memset(level, 0, sizeof level);
    memset(gap, 0, sizeof gap);
    memcpy(cur, head, sizeof head);
    gap[0] = nv;
    int v = pre[s] = s, flow = 0, aug = INF;
    while(level[s] < nv)
    {
        bool flag = false;
        for(int &i = cur[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && level[v] == level[u] + 1)
            {
                flag = true;
                pre[u] = v;
                v = u;
                aug = min(aug, g[i].cap);
                if(v == t)
                {
                    flow += aug;
                    while(v != s)
                    {
                        v = pre[v];
                        g[cur[v]].cap -= aug;
                        g[cur[v]^1].cap += aug;
                    }
                    aug = INF;
                }
                break;
            }
        }
        if(flag) continue;
        int minlevel = nv;
        for(int i = head[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && minlevel > level[u])
                minlevel = level[u], cur[v] = i;
        }
        if(--gap[level[v]] == 0) break;
        level[v] = minlevel + 1;
        gap[level[v]]++;
        v = pre[v];
    }
    return flow;
}
bool cmp(int a, int b)
{
    return deg[a] > deg[b];
}
char work(int i)
{
    if(i <= 26) return i - 1 + 'a';
    else return i - 26 - 1 + 'A';
}
int main()
{
    int T, n, t, c, e;
    scanf("%d", &T);
    while(T--)
    {
        cnt = 0;
        memset(head, -1, sizeof head);
        scanf("%d%d%d%d", &n, &t, &c, &e);
        int tot = 0, sum = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", &aa[i], &bb[i], &cc[i]);
            arr[tot++] = aa[i], arr[tot++] = bb[i];
            sum += cc[i];
        }
        if(sum > e * t * c)
        {
            printf("Case %d: No\n", ++cas); continue;
        }
        sort(arr, arr + tot);
        tot = unique(arr, arr + tot) - arr;
        arr[tot] = arr[tot-1] + 1;
        for(int i = 1; i <= n; i++)
        {
            aa[i] = lower_bound(arr, arr + tot, aa[i]) - arr + 1;
            bb[i] = lower_bound(arr, arr + tot, bb[i]) - arr + 1;
        }
        int ss = 0, tt = n + tot + 1;
        for(int i = 1; i <= n; i++) add_edge(ss, i, cc[i]);
        for(int i = 0; i < tot; i++) add_edge(n + i + 1, tt, (arr[i+1] - arr[i]) * t * c);
        for(int i = 1; i <= n; i++)
            for(int j = aa[i]; j < bb[i]; j++)
                add_edge(i, n + j, arr[j] - arr[j-1]);
        nv = tt + 1;
        int res = sap(ss, tt);
        if(res != sum)
        {
            printf("Case %d: No\n", ++cas); continue;
        }
        printf("Case %d: Yes\n", ++cas);
        memset(last, 0, sizeof last);
        for(int i = 1; i <= e; i++)
            for(int j = 0; j <= t * c; j++) ans[i][j] = '.';
        for(int i = 1; i <= n; i++)
            for(int j = head[i]; j != -1; j = g[j].next)
                if(g[j^1].cap)
                {
                    int l = g[j].to - n;
                    for(int k = arr[l-1]; k < arr[l]; k++)
                        deg[k] = t * c - last[k], id[k] = k;
                    sort(id + arr[l-1], id + arr[l], cmp);
                    for(int k = arr[l-1]; k < g[j^1].cap + arr[l-1]; k++)
                    {
                        int x = id[k];
                        ans[x][last[x]++] = work(i);
                    }
                }
        for(int i = 1; i < e; i++)
            for(int j = 0; j < t * c; j++)
            {
                if(j && j % c == 0) printf("|");
                printf("%c", ans[i][j]);
                if(j + 1 == t * c) printf("\n");
            }
    }
    return 0;
}