【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!

http://7xjob4.com1.z0.glb.clouddn.com/582a4b6f01e9aaa48ccdd4531e25cf4a

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

http://7xjob4.com1.z0.glb.clouddn.com/a87807d461d30befc4a8840a002e5cb6

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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值