有源汇上下限最大流

ZOJ Problem Set - 3229

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 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 Cktargets, Tk1Tk2, ..., TkCk. The number of photos of target Tki that Aya takes should be in range [LkiRki], 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, G1G2, ..., Gm in range [0, 10000]. Then n days. Each day begins with two integer 1 <= C <= 100, 0 <= D <= 30000. Then C differenttargets. 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

 

题目大意:看了半天也看不太懂,我英语太垃圾了,大意就是要给几个女孩拍照,gi代表的是n天每个女孩最少要拍多少张

然后n天 每天有一个c代表有多少个女孩要在今天拍照,d代表今天最多拍多少张。然后每行3个数字分别代表第k个女孩的最少拍照数和最大拍照数。拍照数要在这个区间内。

解题思路:这是我刚刚学完上下限网络流后做的,调了很长时间

建立虚拟源点和汇点,超级源点汇点ss,tt 

s向天数连边上限为每天的上限照片数 每天向对应的k个女孩连边,上下限分别对应,女孩向t连边下限为gi 上限为正无穷

然后连t到s的上限为无穷的边,然后分离必要弧(就是下限所对应的弧),调整原边的容量为上限减下限(因为把下限分离出来了),然后跑tt到ss的最大流(代码中tt和ss颠倒了一下)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define sca(x) scanf("%d",&x)
#define inf 0x3f3f3f3f
using namespace std;
#define N 2000
#define maxn 500000
int n,m;
int G[N];

struct edg
{
    int to,cap,w,nt,num;
}g[maxn];
int tot;

int head[N],b[N],cur[N];
int low[maxn],ans[maxn];
void addedg(int u,int v,int cap,int num=0)
{
    g[tot]={v,cap,0,head[u],num};
    head[u]=tot++;
    g[tot]={u,0,0,head[v],num};
    head[v]=tot++;
}

int dep[N];
bool bfs(int s,int t)
{
    memset(dep,0,sizeof(dep));
    dep[s]=1;
    queue<int>q;
    q.push(s);

    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=g[i].nt)
        {
            int to=g[i].to;
            if(!dep[to]&&g[i].cap-g[i].w>0)
            {
                dep[to]=dep[u]+1;
                q.push(to);
            }
        }
    }
    return dep[t]>0;
}


int dfs(int s,int t,int flow)
{
    if(s==t)return flow;
    int tmp=0;
    for(int& i=cur[s];i!=-1;i=g[i].nt)
    {
        int to=g[i].to;
        if(dep[to]==dep[s]+1&&g[i].cap-g[i].w>0
        &&(tmp=dfs(to,t,min(flow,g[i].cap-g[i].w))))
        {
            g[i].w+=tmp;
            g[i^1].w-=tmp;
            return tmp;
        }
    }
    return 0;
}

int dinic(int s,int t)
{
    int ans=0,tmp=0;
    while(bfs(s,t))
    {
        memcpy(cur,head,sizeof(head));
        while(tmp=dfs(s,t,inf))
            ans+=tmp;
    }
    return ans;
}

void work( )
{
    tot=0;
    int sum=0;
    int s=0,t=m+n+1,ss=m+n+2,tt=m+n+3;
    memset(head,-1,sizeof(head));
    memset(b,0,sizeof(b));
    int now=1;
    rep(i,1,m)
    {
        sca(G[i]);
        b[n+i]+=G[i];
        b[t]-=G[i];
    }
    rep(i,1,n)
    {
        int k,up;
        scanf("%d%d",&k,&up);
        addedg(s,i,up);
        rep(j,1,k)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            x++;
            addedg(i,n+x,z-y,now);
            b[i]+=y;
            b[n+x]-=y;
            low[now++]=y;
        }
    }
    rep(i,1,m)addedg(n+i,t,inf-G[i]);
    rep(i,0,n+m+1)
    {
        if(b[i]>0)addedg(i,tt,b[i]),sum+=b[i];
        else addedg(ss,i,-b[i]);
    }
    addedg(t,s,inf);
    if(sum==dinic(ss,tt))
    {
        for(int i=head[ss];i!=-1;i=g[i].nt)
        {
            g[i].w=g[i^1].w=0;
        }
        for(int i=head[tt];i!=-1;i=g[i].nt)
        {
            g[i].w=g[i^1].w=0;
        }
        g[tot-1].w=g[tot-2].w=0;
        printf("%d\n",sum+dinic(s,t));
        for(int i=1;i<=m;i++)
        {
            for(int j=head[n+i];j!=-1;j=g[j].nt)
            {
                int id=g[j].num;
                if(id)
                {
                    ans[id]=g[j^1].w+low[id];
                }
            }
        }
        rep(i,1,now-1)
        {
            printf("%d\n",ans[i]);
        }
    }
    else puts("-1");
    puts("");
}

int main()
{
    while(cin>>n>>m)
    {
        work();
    }
}

关于输出答案:如果tt到ss的最大流等于所有必要弧的流量之和;说明存在可行流,然后我们将于ss和tt有关的边去掉之后做

s到t的最大流即可,最终的最大流即为s到t的最大流加上可行流。

输出的话就是记录每条天与女孩的连边的编号和下限,答案即为正向弧的流量加上下限。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值