zoj 3229 Shoot the Bullet(有源汇上下界最大流)

来源:互联网 发布:java开源社区有哪些 编辑:程序博客网 时间:2024/04/30 11:18
Shoot the Bullet

Time Limit: 2 Seconds      Memory Limit: 32768 KB      Special Judge

Gensokyo is a world which exists quietly beside ours, separated by a mystical border. It is a utopia where humans and other beings such as fairies,youkai(phantoms), and gods live peacefully together. Shameimaru Aya is a crow tengu with the ability to manipulate wind who has been inGensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns theBunkachou - her record of interesting observations for Bunbunmaru News articles and pictures of beautifuldanmaku(barrange) or cute girls living in Gensokyo. She is the biggest connoisseur of rumors about the girls ofGensokyo among the tengu. Her intelligence gathering abilities are the best inGensokyo!

During the coming n days, Aya is planning to take many photos of m cute girls living in Gensokyo to write Bunbunmaru News daily and record at leastGx photos of girlx in total in theBunkachou. At the k-th day, there areCk targets,Tk1,Tk2, ...,TkCk. The number of photos of targetTki that Aya takes should be in range [Lki,Rki], if less, Aya cannot write an interesting article, if more, the girl will become angry and use herlast spell card to attack Aya. What's more, Aya cannot take more than Dk photos at thek-th day.Under these constraints, the more photos, the better.

Aya is not good at solving this complex problem. So she comes to you, an earthling, for help.

Input

There are about 40 cases. Process to the end of file.

Each case begins with two integers 1 <= n <= 365, 1 <= m <= 1000. Thenm integers,G1, G2, ...,Gm in range [0, 10000]. Thenn days. Each day begins with two integer 1 <=C <= 100, 0 <= D <= 30000. Then C different targets. Each target is described by three integers, 0 <=T <m, 0 <= L <= R <= 100.

Output

For each case, first output the number of photos Aya can take, -1 if it's impossible to satisfy her needing. If there is a best strategy, output the number of photos of each girl Aya should take at each day on separate lines. The output must be in the same order as the input. If there are more than one best strategy, any one will be OK.

Output a blank line after each case.

Sample Input

2 312 12 123 180 3 91 3 92 3 93 180 3 91 3 92 3 92 312 12 123 180 3 91 3 92 3 93 180 0 31 3 62 6 92 312 12 123 150 3 91 3 92 3 93 210 0 31 3 62 6 12

Sample Output

3666666636963369-1

 

 题意:有m个女孩,要在n天内拍照,每个女孩至少要拍G[j]张。每天只能拍D[i]张,并且对于第i天,只可以对C[i]个给定的女孩j拍照,而给j拍照时,拍照的数量只能为[L[j], R[j]]张。问n天后能否拍到满足条件的照片,若能,输出每一天在该天拍摄的女孩拍了多少张照片。

思路:虚拟一个源点和汇点。源点到每天连一条边,容量为D[i],每天到给定的女孩连一条边,容量为L - R,每个女孩连一条边到汇点,容量为INF-G[i]。汇点到源点连一条边,容量为INF,构成无源汇的循环流图。再虚拟出一个超级源点ss和汇点tt,根据每个点流进和流出的流量建图,求一次最大流,满流则可行。之后,删除ss和tt,再求一次最大流,即为最终答案。


AC代码:

#include <iostream>#include <cmath>#include <cstdlib>#include <cstring>#include <cstdio>#include <queue>#include <ctime>#include <vector>#include <algorithm>#define ll long long#define L(rt) (rt<<1)#define R(rt)  (rt<<1|1)using namespace std;const int INF = 1e9;const int maxn = 2005;struct Edge{    int u, v, cap, flow, next;} et[maxn * 500];int low[maxn], cnt[maxn], dis[maxn], pre[maxn], cur[maxn], eh[maxn], du[maxn];int G[maxn], C[maxn], D[maxn], T[400][1005], L[400][1005], R[400][1005];int n, m, s, t, num, ss, tt, sum;void init(){    memset(eh, -1, sizeof(eh));    memset(du, 0, sizeof(du));    num = sum = 0;}void add(int u, int v, int cap, int flow){    Edge e = {u, v, cap, flow, eh[u]};    et[num] = e;    eh[u] = num++;}void addedge(int u, int v, int cap){    add(u, v, cap, 0);    add(v, u, 0, 0);}int isap(int s, int t, int nv){    int u, v, now, flow = 0;    memset(low, 0, sizeof(low));    memset(cnt, 0, sizeof(cnt));    memset(dis, 0, sizeof(dis));    for(u = 0; u <= nv; u++) cur[u] = eh[u];    low[s] = INF, cnt[0] = nv, u = s;    while(dis[s] < nv)    {        for(now = cur[u]; now != -1; now = et[now].next)            if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;        if(now != -1)        {            cur[u] = pre[v] = now;            low[v] = min(low[u], et[now].cap - et[now].flow);            u = v;            if(u == t)            {                for(; u != s; u = et[pre[u]].u)                {                    et[pre[u]].flow += low[t];                    et[pre[u]^1].flow -= low[t];                }                flow += low[t];                low[s] = INF;            }        }        else        {            if(--cnt[dis[u]] == 0) break;            dis[u] = nv, cur[u] = eh[u];            for(now = eh[u]; now != -1; now = et[now].next)                if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)                    dis[u] = dis[et[now].v] + 1;            cnt[dis[u]]++;            if(u != s) u = et[pre[u]].u;        }    }    return flow;}void build(){    init();    s = 0, t = n + m + 1;    ss = t + 1, tt = t + 2;    for(int i = 1; i <= n; i++)    {        for(int j = 1; j <= C[i]; j++)        {            addedge(i, n + T[i][j] + 1, R[i][j] - L[i][j]);            du[i] -= L[i][j];            du[n + T[i][j] + 1] += L[i][j];        }    }    for(int i = 1; i <= n; i++)        addedge(s, i, D[i]);    for(int i = 1; i <= m; i++)    {        addedge(n + i, t, INF - G[i]);        du[n + i] -= G[i];        du[t] += G[i];    }    addedge(t, s, INF);    for(int i = s; i <= t; i++)    {        if(du[i] > 0)        {            addedge(ss, i, du[i]);            sum += du[i];        }        else if(du[i] < 0) addedge(i, tt, -du[i]);    }}void solve(){    build();    int tmp = isap(ss, tt, tt + 1); //求可行流    if(tmp != sum)    {        printf("-1\n");        return;    }    int ans = isap(s, t, tt + 1);   //删除ss和tt,求最大流    printf("%d\n", ans);    int cnt = 0;    for(int i = 1; i <= n; i++)        for(int j = 1; j <= C[i]; j++)        {            printf("%d\n", et[cnt].flow + L[i][j]);            cnt += 2;        }}int main(){    while(~scanf("%d%d", &n, &m))    {        for(int i = 1; i <= m; i++)            scanf("%d", &G[i]);        for(int i = 1; i <= n; i++)        {            scanf("%d%d", &C[i], &D[i]);            for(int j = 1; j <= C[i]; j++)                scanf("%d%d%d", &T[i][j], &L[i][j], &R[i][j]);        }        solve();        puts("");    }    return 0;}


0 0
原创粉丝点击