【ZOJ3229】Shoot the Bullet 有源汇上下界最大流

Description

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 in Gensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns the Bunkachou - her record of interesting observations for Bunbunmaru News articles and pictures of beautiful danmaku(barrange) or cute girls living in Gensokyo. She is the biggest connoisseur of rumors about the girls of Gensokyo among the tengu. Her intelligence gathering abilities are the best in Gensokyo!

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 least Gx photos of girl x in total in the Bunkachou. At the k-th day, there are Ck targets, Tk1, Tk2, …, TkCk. The number of photos of target Tki 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 her last spell card to attack Aya. What’s more, Aya cannot take more than Dk photos at the k-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. Then m integers, G1, G2, …, Gm in range [0, 10000]. Then n days. Each day begins with two integer 1 <= C <= 100, 0 <= D <= 30000. Then Cdifferent 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 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 3 9
1 3 9
2 3 9

2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 0 3
1 3 6
2 6 9

2 3
12 12 12
3 15
0 3 9
1 3 9
2 3 9
3 21
0 0 3
1 3 6
2 6 12

Sample Output

36
6
6
6
6
6
6

36
9
6
3
3
6
9

-1

Wikipedia
Touhou Wiki

Source

ZOJ Monthly, July 2009

窝认真的把链接附上了~~~

题目大意:文文要给幻想乡的女♂孩子们拍照,一共n天,m个女♂孩子,每天文文至多拍D[i]张照片,每个女♂孩子总共要被文文至少拍G[i]次。在第i天,文文可以拍c[i]个女♂孩子,c[i]个女♂孩子中每个女♂孩子在当天被拍的次数是 [li,ri] ,求最多可以拍多少张照片,以及每天每个可以拍的女♂孩子被拍了多少张照片。

然后就很容易想到一个网络流模型:

n天放左部,m个人放右部,源点向每天连边,容量D[i],m个人向汇点连边,容量G[i],左部向右部连边,容量是它们的 rili ,这是有源汇的上下界最大流问题…

首先先做一个有源汇上下界可行流,然后再从残量网络上跑一个s到e的最大流就是答案。

建边的代码比算法的代码还恶心系列

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

const int INF = 1000000010;
const int SZ = 500010;

int n,m;

int head[SZ],nxt[SZ],tot = 1;

struct edge{
    int t,d;
}l[SZ];

void build(int f,int t,int d)
{
    l[++ tot].t = t;
    l[tot].d = d;
    nxt[tot] = head[f];
    head[f] = tot;
}

void insert(int f,int t,int d)
{
    build(f,t,d); build(t,f,0);
}

int deep[SZ];
queue<int> q;

bool bfs(int s,int e)
{
    memset(deep,0,sizeof(deep));
    deep[s] = 1;
    while(q.size()) q.pop();
    q.push(s);
    while(q.size())
    {
        int u = q.front(); q.pop();
        for(int i = head[u];i;i = nxt[i])
        {
            int v = l[i].t;
            if(!deep[v] && l[i].d)
            {
                deep[v] = deep[u] + 1;
                q.push(v);
                if(v == e) return true;
            }
        }
    }
    return false;
}

int dfs(int u,int flow,int e)
{
    if(u == e || flow == 0) return flow;
    int rest = flow;
    for(int i = head[u];i;i = nxt[i])
    {
        int v = l[i].t;
        if(deep[v] == deep[u] + 1 && l[i].d)
        {
            int f = dfs(v,min(rest,l[i].d),e);
            if(f > 0)
            {
                l[i].d -= f;
                l[i ^ 1].d += f;
                rest -= f;
                if(rest == 0) break;
            }
            else deep[v] = 0;
        }
    }
    if(flow - rest == 0) deep[u] = 0;
    return flow - rest;
}


int dinic(int s,int e)
{
    int ans = 0;
    while(bfs(s,e))
    {
        int tmp = dfs(s,INF,e);
        if(!tmp) break;
        ans += tmp;
    }
    return ans;
}

int g[SZ],d[SZ];
int inb[SZ],outb[SZ];

bool can()
{
    int sum = 0;
    int S = n + m + 3,E = n + m + 4;
    for(int i = 1;i <= n + m + 2;i ++)
    {
        int tmp = inb[i] - outb[i];
        if(tmp > 0) 
            insert(S,i,tmp),sum += tmp;
        else
            insert(i,E,-tmp);
    }   
    return dinic(S,E) == sum;
}


int B[SZ];

void init()
{
    tot = 1;
    memset(inb,0,sizeof(inb));
    memset(outb,0,sizeof(outb));
    memset(head,0,sizeof(head));
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        int S = n + m + 1,E = n + m + 2;
        for(int i = 1;i <= m;i ++)
        {
            scanf("%d",&g[i]);
            inb[E] += g[i];
            outb[i + n] += g[i];
        }
        int top = 0;
        for(int i = 1;i <= n;i ++)
        {
            int c;
            scanf("%d%d",&c,&d[i]);
            for(int j = 1;j <= c;j ++)
            {
                int id,l,r;
                scanf("%d%d%d",&id,&l,&r);
                B[++ top] = l;
                id ++;
                insert(i,id + n,r - l);
                outb[i] += l;
                inb[id + n] += l;
            }
        }
        for(int i = 1;i <= n;i ++) insert(S,i,d[i]);
        for(int i = 1;i <= m;i ++) insert(i + n,E,INF - g[i]);

        insert(E,S,INF);

        if(can())
        {
            dinic(S,E);
            int ans = 0;
            for(int i = head[S];i;i = nxt[i])
                ans += l[i ^ 1].d;
            printf("%d\n",ans - INF);
            for(int i = 1;i <= top;i ++)
                printf("%d\n",l[i * 2 + 1].d + B[i]);
        }
        else
            puts("-1");
        puts("");
    }
    return 0;
}

你可能感兴趣的:(【ZOJ3229】Shoot the Bullet 有源汇上下界最大流)