ZOJ 3229 Shoot the Bullet

有源点汇点的上下界最大流问题。。。

建图很简单。。。按题意即可。。。

设原图 源点为 s 汇点 为 t,连一条t到s无下界上界无限大的边。。。。设两个超级源S,T,像无源汇判断可行流的问题一样,记录每个点的in,连接到相应的超级源汇点。。。对S,T跑一遍最大流,并检测S所连边是否满流。。。如果不满足连可行流都没有无解。。。否则去掉S,T点(但总点数不要边。。。在这里错了一下午)对s,t跑一遍最大流。得到的结果既答案。。。。。第一遍最大流保证了每个点的下界流得到满足,此时的图里还有很多自由流可以走,第二遍最大流就将这些流走满了得到的就是答案。。。

总结一下有源点汇点的上下界最大流 步骤为:

1:连接 t-->s INF,并增加S,T 像无源汇可行流一样建边,第一次最大流判断可行流

2:去掉S,T(Adj变-1) 总点数不变,第二次最大流得到答案

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

External Links

Wikipedia

Touhou Wiki


Author: WU, Zejun

Source: ZOJ Monthly, July 2009

Submit    Status

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn=2222;
const int maxm=1000000;
const int INF=0x3f3f3f3f;

struct Edge
{
    int to,next,cap,flow;
}edge[maxm];

int Size,Adj[maxn];
int gap[maxn],dep[maxn],pre[maxn],cur[maxn];

void init()
{
    Size=0;
    memset(Adj,-1,sizeof(Adj));
}

void addedge(int u,int v,int w,int rw=0)
{
    edge[Size].to=v; edge[Size].cap=w; edge[Size].next=Adj[u];
    edge[Size].flow=0; Adj[u]=Size++;
    edge[Size].to=u; edge[Size].cap=rw; edge[Size].next=Adj[v];
    edge[Size].flow=0; Adj[v]=Size++;
}

int sap(int start,int end,int N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,Adj,sizeof(Adj));

    int u=start;
    pre[u]=-1; gap[0]=N;
    int ans=0;

    while(dep[start]<N)
    {
        if(u==end)
        {
            int Min=INF;
            for(int i=pre[u];~i;i=pre[edge[i^1].to])
                if(Min>edge[i].cap-edge[i].flow)
                    Min=edge[i].cap-edge[i].flow;
            for(int i=pre[u];~i;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
            }
            u=start;
            ans+=Min;
            continue;
        }
        bool flag=false;
        int v;
        for(int i=cur[u];~i;i=edge[i].next)
        {
            v=edge[i].to;
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
            {
                flag=true;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if(flag)
        {
            u=v;
            continue;
        }
        int Min=N;
        for(int i=Adj[u];~i;i=edge[i].next)
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]]) return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if(u!=start) u=edge[pre[u]^1].to;
    }
    return ans;
}

int n,m;
int G[maxn],in[maxn];

int bian[maxm],low[maxm],bn;

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init(); bn=0;
        memset(in,0,sizeof(in));

        for(int i=1;i<=m;i++)
        {
            scanf("%d",&G[i]);
            addedge(n+i,n+m+1,INF-G[i]);
            in[n+i]-=G[i]; in[n+m+1]+=G[i];
        }

        for(int i=1;i<=n;i++)
        {
            int C,D;
            scanf("%d%d",&C,&D);
            addedge(0,i,D);
            for(int j=0;j<C;j++)
            {
                int T,L,R;
                scanf("%d%d%d",&T,&L,&R);
                T++;
                low[bn]=L;
                bian[bn++]=Size;
                addedge(i,n+T,R-L);
                in[i]-=L; in[n+T]+=L;
            }
        }
        addedge(n+m+1,0,INF);
        int sum=0;
        for(int i=0;i<n+m+2;i++)
        {
            if(in[i]<0) addedge(i,n+m+3,-in[i]);
            if(in[i]>0)
            {
                sum+=in[i];
                addedge(n+m+2,i,in[i]);
            }
        }
        int MaxFlow=sap(n+m+2,n+m+3,n+m+4);
        if(MaxFlow!=sum) puts("-1");
        else
        {
            Adj[n+m+2]=Adj[n+m+3]=-1;
            MaxFlow=sap(0,n+m+1,n+m+4);
            printf("%d\n",MaxFlow);
            for(int i=0;i<bn;i++)
            {
                printf("%d\n",edge[bian[i]].flow+low[i]);
            }
        }
        putchar(10);
    }
    return 0;
}
时间: 2024-10-04 03:25:30

ZOJ 3229 Shoot the Bullet的相关文章

ZOJ 3229 Shoot the Bullet(有源汇有上下界的最大流)

ZOJ 3229 Shoot the Bullet 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3229 题意:一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝最多给C个女神拍照,每天拍照数不能超过D张,而且给每个女神 i 拍照有数量限制[Li,Ri],对于每个女神n天的拍照总和不能超过Gi,如果有解求屌丝最多能拍多少张照,并求每天给对应女神拍多少张照:否则输出-1. 思路: 有源汇有上下界的最大流 1. 在原先

ZOJ 3229 Shoot the Bullet 有源有汇带下界的最大流

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=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 u

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)

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

Shoot the Bullethttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442 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

【网络流】ZOJ 3229 Shoot the Bullet

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3229 题目大意: n天给m个女孩拍照(1<=n<=365,1<=m<=1000),每个女孩X在n天里总共至少拍Gx张照片. 第k天可以给Ck个女孩拍照,当天总共最多拍Dk张照片,每个女孩这天被拍的数量在[Li,Ri]. 满足要求的条件下尽量多拍照. 求是否有可行解,没有输出-1,有的话输出最大流以及每天对该天能拍照的女孩的拍照数量(巨坑..怪我没

[zoj] 3229 Shoot the Bullet || 有源汇上下界最大流

zoj 文文要给幻想乡的女孩子们拍照,一共n天,m个女孩子,每天文文至多拍D[i]张照片,每个女孩子总共要被文文至少拍G[i]次.在第i天,文文可以拍c[i]个女孩子,c[i]个女孩子中每个女孩子在当天被拍的次数是[l,r],求最多可以拍多少张照片,以及每天每个可以拍的女孩子被拍了多少张照片. 有源汇上下界最大流. 先跑有源汇上下界可行流,判断是否可行,若可行则此时跑原图中s到t的最大流即为答案. //代码与题解不符-- #include<cstdio> #include<algorit

ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】

题目:ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇有上下界网络流 题意:有 n 天和 m 个girls,然后每天给一部分girls拍照,每个girls 有拍照的下限,即最少要拍这么多张,然后每天有k个女孩拍照,摄影师最多可以拍num张,然后 k 个女该每天拍照数量值有上下限,然后问你有没有满足这样条件的给女孩拍照的最大方案,然后按照输入输出每天给女孩拍照的张数. 做这道题目推荐先做:这儿 分析:首先它让你判断能不能满足条件. 按照题目给出的条件很

zoj 3229 dinic算法的非递归实现以及有上下界的有源汇的网络流的最大流的求解

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)

ZOJ 3229 有上下界最大流

1: /** 2: ZOJ 3229 有上下界的最大流 3: 两次求最大流的过程,非二分 4: 有源汇上下界的最大流问题, 首先连接 sink -> src, [0,INF]. 5: 根据net的正负,来建立 Supersrc 与 supersink 之间的边,做一次 maxflow. 6: 若所有的Supersrc 与 Supersink满流,则说明存在可行流. 7: 然后删除 sink -> src之间的边.(cap 置零即可). 从src -> sink 做一次最大流. 8: 两次