ZOJ 3229 Shoot the Bullet 有源汇最大流(好题)


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 theBunkachou. At the k-th day, there are Ck targets, 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 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 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
 
 
  
  
  1. 处理有源汇有上下界最大流问题是: 
  2. 1.构造附加网络 
  3. 2.对ss、tt求最大流(ss、tt满流则有解) 
  4. 3.若有解,对s、t求最大流
题目是说aya要给少女拍照片,一共需要n天,共有m个少女,在n天中,对第i个少女至少要拍G张照片。在第t天中,只能拍D张照片,而且是特定的第ct个人拍,而且要求在l到r张之间。问能否完成任务,如果完成最多拍多少张。
有源汇最大流好的题目,首先要建好图,建立一个源点连接每一天,上界是Dt,下界是0,然后建立一个汇点连接每一个少女,上界是inf,下界是Gi。对于每一天连接到这一天当中需要完成任务的少女,下界是l,上界是r。然后引入超级源点和超级汇点,像建立无源汇最大流那样建图连接,此外还要将t连接到s,容量为inf。
一开始用的是SAP中dfs求的结果求错了,然后参考网上的解题思路,最终解出。⊙﹏⊙b汗
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define M 2014
#define N 1000010
#define inf 0x3f3f3f
using namespace std;
int head[M],gap[M],dis[M],pre[M],cur[M];
int to[N],next[N],flow[N],cap[N];
int ns,n,m,st,ed,num;
int in[M],down[N],id[N];
void init()
{
    memset(head,0,sizeof(head));
    memset(in,0,sizeof(in));
    num=2;
}
void addedge(int u,int v,int c)
{
    to[num]=v;cap[num]=c;flow[num]=0;next[num]=head[u];head[u]=num++;
    to[num]=u;cap[num]=0;flow[num]=0;next[num]=head[v];head[v]=num++;

}
void bfs()
{
    memset(dis,0x3f,sizeof(dis));
    queue<int> que;
    que.push(ed);
    dis[ed]=0;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        ++gap[dis[u]];
        for(int p=head[u];p;p=next[p])
        {
            int &v=to[p];
            if(cap[p^1]>flow[p^1]&dis[v]>n)
            {
                dis[v]=dis[u]+1;
                que.push(v);
            }
        }
    }
}
int Max_flow(int ss,int tt,int nn)
{
    st=ss;
    ed=tt;
    n=nn;
    int ans = 0, minFlow = inf, u;
    for(int i=0;i<=n;i++)
    {
        cur[i]=head[i];
        gap[i]=0;
    }
    u=pre[st]=st;
    bfs();
    while(dis[st]<n)
    {
        bool flag=false;
        for(int &p=cur[u];p;p=next[p])
        {
            int &v=to[p];
            if(cap[p]>flow[p]&&dis[u]==dis[v]+1)
            {
                flag=true;
                minFlow=min(minFlow,cap[p]-flow[p]);
                pre[v]=u;
                u=v;
                if(u==ed)
                {
                    ans+=minFlow;
                    while(u!=st)
                    {
                        u=pre[u];
                        flow[cur[u]]+=minFlow;
                        flow[cur[u]^1]-=minFlow;
                    }
                    minFlow=inf;
                }
                break;
            }
        }
        if(flag)continue;
        int minDis=n-1;
        for(int p=head[u];p;p=next[p])
        {
            int &v=to[p];
            if(cap[p]>flow[p]&&dis[v]<minDis)
            {
                minDis=dis[v];
                cur[u]=p;
            }
        }
        if(--gap[dis[u]]==0)break;
        ++gap[dis[u]=minDis+1];
        u=pre[u];
    }
    return ans;
}
int main()
{
    while(scanf("%d%d",&ns,&m)!=EOF)
    {
        init();
        int s=ns+m+1,t=s+1,ss=t+1,tt=ss+1,c;
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&c);
            in[i]-=c;
            in[t]+=c;
            addedge(i,t,inf);
        }
        int cnt=0;
        int a,b,low,high;
        for(int i=1;i<=ns;i++)
        {
            scanf("%d%d",&a,&b);
            addedge(s,i+m,b);
            while(a--)
            {
                scanf("%d%d%d",&c,&low,&high);
                in[i+m]-=low;
                in[c+1]+=low;
                down[++cnt]=low;
                id[cnt]=num;
                addedge(i+m,c+1,high-low);
            }
        }
        int sum=0;
        for(int i=1;i<=t;i++)
        {
            if(in[i]>0){sum+=in[i];addedge(ss,i,in[i]);}
            if(in[i]<0)addedge(i,tt,-in[i]);
        }
        addedge(t,s,inf);
        if(sum!=Max_flow(ss,tt,tt))
            printf("-1\n");
        else
        {
            printf("%d\n",Max_flow(s,t,tt));
            for(int i=1;i<=cnt;i++)
                printf("%d\n",down[i]+flow[id[i]]);
        }
        printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值