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

 

题意:
给m个女神拍照,计划拍照n天,每一天给定的C个女神拍照,每天拍照数不能超过D张,而且给每个女神i拍照有数量限制[Li,Ri],对于每个女神n天的拍照总和不能少于Gi,如果有解求最多能拍多少张照,并求每天给对应女神拍多少张照;否则输出-1。

分析:
这是一个有源汇点的图,需要新建超级源点ss和超级汇点,还有一些使满足可行流的附加边,必须保证附加边满流,才能保证符合条件,才能求最大流。如果不懂有上下界的参考:https://blog.csdn.net/lml11111/article/details/82939666

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int MAXN=44222;
const int MAXM=888888;


struct Edge{
    int to, c, next;
};
Edge edge[MAXM];
int head[MAXN], cnt;
int dis[MAXN], num[MAXN], cur[MAXN], pre[MAXN];
int s, t, nv,p;
int n,m;
void addedge(int u, int v, int c){
    edge[cnt].to= v; edge[cnt].c = c; edge[cnt].next = head[u]; head[u] = cnt++;
    edge[cnt].to = u; edge[cnt].c = 0; edge[cnt].next = head[v]; head[v] = cnt++;
}

void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void bfs(int s,int t)
{
    queue<int>q;
    memset(dis,-1,sizeof(dis));
    memset(num,0,sizeof(num));
    while(!q.empty()) q.pop();
    dis[t] = 0;
    q.push(t);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(dis[v]==-1){
                dis[v]=dis[u]+1;
                num[dis[v]]++;
                q.push(v);
            }
        }
    }
}
int ISAP(int s,int t){
    memcpy(cur,head,sizeof(head));
    bfs(s,t);
    int flow = 0, u = pre[s] = s, i;
    while(dis[s] < nv){
        if(u==t){
            int f = inf, neck;
            for(i = s; i != t; i = edge[cur[i]].to){
                if(f > edge[cur[i]].c){
                    f = edge[cur[i]].c;
                    neck = i;
                }
            }
            for(i = s; i != t; i = edge[cur[i]].to){
                edge[cur[i]].c -= f;
                edge[cur[i] ^ 1].c += f;
            }
            flow += f;
            u = neck;
        }
        for(i = cur[u]; ~i; i = edge[i].next) if(edge[i].c &&dis[u] == dis[edge[i].to] + 1) break;
        if(~i){
            cur[u] = i;
            pre[edge[i].to] = u;
            u = edge[i].to;
        }
        else{
            if(0 == (--num[dis[u]])) break;
            int mind = nv;
            for(i = head[u]; ~i; i = edge[i].next){
                if(edge[i].c && mind > dis[edge[i].to]){
                    mind = dis[edge[i].to];
                    cur[u] = i;
                }
            }
            dis[u] = mind + 1;
            num[dis[u]]++;
            u = pre[u];
        }
    }
    return flow;
}


int du[MAXN];
int dn[500][2100];
int id[500][2100];
int main()
{

    while(~scanf("%d%d",&n,&m))
    {
        int s=0,t=n+m+1;
        int ss=n+m+2,tt=n+m+3;
        nv=tt+1;
        int a,b,cc;
        memset(du,0,sizeof(du));///附加边的信息
        memset(dn,0,sizeof(dn));///记录第i天给第j个人拍照的最低标准
        memset(id,0,sizeof(id));///记录第i天给第j个人拍照的边号,以便查询里面的流量
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&a);
            addedge(n+i,t,inf-a);
            du[n+i]-=a;
            du[t]+=a;
        }
        int C,D;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&C,&D);
            addedge(s,i,D);
            for(int j=1;j<=C;j++)
            {
                scanf("%d%d%d",&a,&b,&cc);
                addedge(i,a+n+1,cc-b);
                du[i]-=b;
                du[a+n+1]+=b;
                dn[i][a]=b;
                id[i][a]=cnt-2;
            }
        }
        addedge(t,s,inf);
        int sum=0;
        for(int i=1;i<=t;i++)
        {
            if(du[i]<0)
            {
                addedge(i,tt,-du[i]);
            }
            else if(du[i]>0)
            {
                addedge(ss,i,du[i]);
                sum+=du[i];
            }
        }
        //printf("jfodjfidf\n");

        nv=tt+1;
        int maxflow=ISAP(ss,tt);
        //printf("sssss %d %d\n",sum,maxflow);
        if(maxflow==sum)
        {
            nv=t+1;
            int ans=ISAP(s,t);
            printf("%d\n",ans);
            for(int i=1;i<=n;i++)
            {
                for(int j=0;j<m;j++)
                    if(id[i][j])
                        printf("%d\n",edge[id[i][j]^1].c+dn[i][j]);
            }
        }
        else printf("-1\n");
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值