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

Shoot the Bullet

Time Limit: 2000 ms
Memory Limit: 32768 KB
Problem 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 G x G_x Gx photos of girl x in total in the Bunkachou. At the k k k-th day, there are C k Ck Ck targets, T k 1 T_{k1} Tk1, T k 2 T_{k2} Tk2, …, T k C k T_{kC_k} TkCk. The number of photos of target T k i T_{ki} Tki that Aya takes should be in range [ L k i , R k i ] [L_{ki}, R_{ki}] [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 D k D_k 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 <= n <= 365 1<=n<=365, 1 < = m < = 1000 1 <= m <= 1000 1<=m<=1000. Then m integers, G 1 G_1 G1, G 2 G_2 G2, …, G m G_m Gm in range [0, 10000]. Then n days. Each day begins with two integer 1 < = C < = 100 1 <= C <= 100 1<=C<=100, 0 < = D < = 30000 0 <= D <= 30000 0<=D<=30000. Then C different targets. Each target is described by three integers, 0 < = T < m 0 <= T < m 0<=T<m, 0 < = L < = R < = 100 0 <= L <= R <= 100 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

题意

有m个女生,共有n天,每天可以选择给部分女生拍照,要求n天后,给第i个女生拍的照片数量不少于 G i G_i Gi。在第i天,最多能拍 D i D_i Di张照片,可以选择给C个女生拍照,给第 T k i T_{ki} Tki个女生拍的照片数,需要在 [ L k i , R k i ] [L_{ki}, R_{ki}] [Lki,Rki]之中。求是否存在合适的拍照方案。如果存在,最多能拍多少张照片。

题解:

有源汇有上下界最大流。首先判断是否有可行流。因为有源汇点,所以添加一条汇点到源点的边,边界为 [ 0 , I N F ] [0,INF] [0,INF].然后问题就转化为了,无源汇有上下界可行流,建图即可。

若可行,则继续求最大流。判断可行流时,加的汇点到源点的边的流量,记为ans1。去除判定可行流时添加的点和边,然后在残留网络中求源点到汇点的最大流ans2。ans1+ans2即为所求。实际上直接求源点到汇点的最大流即可。

判断无源汇有上下界最大流:
对于一条边, u − > v u->v u>v,边界为 [ l , r ] [l,r] [l,r]
a [ u ] − = l a[u] -= l a[u]=l, a [ v ] + = l a[v] += l a[v]+=l
建边 u − > v u->v u>v,容量为 r − l r-l rl

所有边处理完后,添加点 s s ss ss, t t tt tt
a [ i ] > 0 a[i]>0 a[i]>0,添加边 s s − > i ss->i ss>i,容量为 a [ i ] a[i] a[i], s u m + = a [ i ] sum+= a[i] sum+=a[i].
a [ i ] < 0 a[i]<0 a[i]<0,添加边 i − > t t i->tt i>tt,容量为 − a [ i ] -a[i] a[i].

建完图后,求 s s ss ss t t tt tt的最大流。若 最 大 流 = = s u m 最大流 == sum ==sum,即存在可行流。

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-6
 
using namespace std;
typedef long long LL;   
typedef pair<int, int> P;
const int maxn = 2020;
const int mod = 1000000007;
struct node{
    int to, nex, cap, flow, id;
}eg[maxn*maxn];
int cnt, dis[maxn], vis[maxn], hd[maxn];
int tot, in[maxn], a[maxn], d[maxn], g[maxn], l[maxn*maxn], r[maxn*maxn];
void init();
bool bfs(int s, int t);
int dinic(int s, int t);
int dfs(int s, int t, int ans);
void add(int fr, int to, int cap, int id);

int main()
{
    int n, m, ed, i, j, k;
    while(~scanf("%d %d", &n, &m))
    {
        tot = 0;
        init();
        memset(a, 0, sizeof(a));
        ed = n+m+1;
        for(i=1;i<=m;i++){
            scanf("%d", &g[i]);
            a[ed] += g[i];
            a[n+i] -= g[i];
            add(n+i, ed, INF, 0);
        }
        for(i=1;i<=n;i++){
            int c;
            scanf("%d %d", &c, &d[i]);
            add(0, i, d[i], 0);
            while(c--){
                tot++;
                scanf("%d %d %d", &j, &l[tot], &r[tot]);
                j++;
                add(i, n+j, r[tot]-l[tot], tot);
                a[n+j] += l[tot];
                a[i] -= l[tot];
            }
        }
        add(ed, 0, INF, 0);
        int ss = ed+1, tt = ed+2, sum = 0;
        for(i=0;i<=ed;i++){
            if(a[i] > 0)add(ss, i, a[i], 0), sum += a[i];
            else if(a[i] < 0)add(i, tt, -a[i], 0);
        }
        int ans = dinic(ss, tt);
        if(ans == sum){
            ans = dinic(0, ed);
            for(i=1;i<=n;i++)
                for(j=hd[i];j!=-1;j=eg[j].nex)
                    if(j%2==0)
                        l[eg[j].id] += eg[j].flow;
            printf("%d\n", ans);
            for(i=1;i<=tot;i++)
                printf("%d\n", l[i]);
        }else printf("-1\n");
        printf("\n");
    }
    return 0;
}

void init()
{
    memset(hd, -1, sizeof(hd));
    cnt = 1;
}

void add(int fr, int to, int cap, int id)
{
    eg[++cnt].to = to;
    eg[cnt].cap = cap;
    eg[cnt].flow = 0;
    eg[cnt].nex = hd[fr];
    eg[cnt].id = id;
    hd[fr] = cnt;
    eg[++cnt].to = fr;
    eg[cnt].flow = eg[cnt].cap = 0;
    eg[cnt].nex = hd[to];
    eg[cnt].id = id;
    hd[to] = cnt;
}

int dinic(int s, int t)
{
    int res = 0;
    while(bfs(s, t))
    {
        for(int i=0;i<=t;i++)
            vis[i] = hd[i];
        int d;
        while((d=dfs(s, t, INF))>0)
            res += d;
    }
    return res;
}


bool bfs(int s, int t)
{
    for(int i=0;i<=t;i++)
        dis[i] = -1;
    queue<int> que;
    que.push(s);
    dis[s] = 0;
    while(que.size())
    {
        int u = que.front();que.pop();
        for(int i=hd[u];i!=-1;i=eg[i].nex)
        {
            if(eg[i].cap > eg[i].flow && dis[eg[i].to] == -1){
                dis[eg[i].to] = dis[u]+1;
                que.push(eg[i].to);
            }
        }
    }
    return dis[t] != -1;
}

int dfs(int s, int t, int ans)
{
    if(s == t)return ans;
    for(int &i = vis[s];i!=-1;i=eg[i].nex)
    if(eg[i].cap > eg[i].flow && dis[eg[i].to] == dis[s]+1)
    {
        int d = dfs(eg[i].to, t, min(ans, eg[i].cap-eg[i].flow));
        if(d>0){
            eg[i].flow += d;
            eg[i^1].flow -= d;
            return d;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值