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

 题意:一个人给m个女孩照相,共进行n天,第一行输入的就是n,m。接下来一行m个数字,表示这m个女孩每个人要求最少照的相片数目。在接下来n组数据,每组数据第一行输入两个数c,d。其中c表示每天最多给多少个人照相,d表示每天最多照多少张相,然后c行数据,每行输入三个数,第一个数表示给第几个人照相,人从0开始计数,接下来两个数分别表示给那个人照相的上下限。

 

题解:赤裸裸的有上下界的最大流,但是这题的坑点在于卡时间,递归的dinic算法必挂,必须要用非递归的dinic算法(好吧,是我的技术太渣,大神递归的dinic照样ac,顺便附上大神递归的dinic代码)。

ac代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#define MAXN 150500
using namespace std;
struct Edge
{
    int s,t,f,next;
}edge[MAXN];
int stack[MAXN];
int n,m;
int ent;
int s,t,supers,supert;
int girl;
int d[1100];
int pre[1400];
int head[1400];
int cur[1400];
int flows[1400];
int Minn[370][1050];
int q[1400];
int Min(int a,int b)
{
    return a<b?a:b;
}
void add(int start,int last,int flow)
{
    edge[ent].s=start;edge[ent].t=last;edge[ent].f=flow;edge[ent].next=head[start];head[start]=ent++;
    edge[ent].s=last;edge[ent].t=start;edge[ent].f=0;edge[ent].next=head[last];head[last]=ent++;
}
bool bfs(int S,int T)
{
    int temp;
    memset(pre,-1,sizeof(pre));
    int hd=0,tail=0,i;
    q[tail++]=S;
    pre[S]=0;
    while(hd<tail)
    {
        temp=q[hd++];
        hd%=1400;
        for(int i=head[temp];i!=-1;i=edge[i].next)
        {
            int temp2=edge[i].t;
            if(pre[temp2]==-1&&edge[i].f)
            {
                pre[temp2]=pre[temp]+1;
                q[tail++]=temp2;
                tail%=1400;
                if(temp2==T)return true;
            }
        }
    }
    return false;
}
int dinic(int start,int last)
{
    int flow=0,now;
    while(bfs(start,last))
    {
        int top=0;
        memcpy(cur,head,sizeof(head));
        int u=start;
        while(1)
        {
            if(u==last)//如果找到终点结束对中间路径进行处理并计算出该流
            {
                int minn=INT_MAX;
                for(int i=0;i<top;i++)
                {
                    if(minn>edge[stack[i]].f)
                    {
                        minn=edge[stack[i]].f;
                        now=i;
                    }
                }
                flow+=minn;
                for(int i=0;i<top;i++)
                {
                    edge[stack[i]].f-=minn;
                    edge[stack[i]^1].f+=minn;
                }
                top=now;
                u=edge[stack[top]].s;
            }
            for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)//找出从u点出发能到的边
                if(edge[i].f&&pre[edge[i].t]==pre[u]+1)
                    break;
            if(cur[u]==-1)//如果从该点未找到可行边,将该点标记并回溯
            {
                if(top==0)break;
                pre[u]=-1;
                u=edge[stack[--top]].s;
            }
            else//如果找到了继续运行
            {
                stack[top++]=cur[u];
                u=edge[cur[u]].t;
            }
        }
    }
    return flow;
}
int main()
{
    int c,g,down,up;
    while(~scanf("%d%d",&n,&m))
    {
        s=0;t=m+n+1;
        ent=0;
        supers=t+1;supert=t+2;
        memset(head,-1,sizeof(head));
        memset(flows,0,sizeof(flows));
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&girl);
            flows[i+n]-=girl;
            flows[t]+=girl;
            add(i+n,t,INT_MAX);
        }
        int sign=ent;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&c,&d[i]);
            for(int j=1;j<=c;j++)
            {
                scanf("%d%d%d",&g,&down,&up);
                g++;
                Minn[i][g]=down;//纪录流的下界
                flows[i]-=down;
                flows[g+n]+=down;
                add(i,g+n,up-down);
            }
        }
        int sign1=ent;
        for(int i=1;i<=n;i++)add(s,i,d[i]);
        add(t,s,INT_MAX);
        int temp=ent-1;
        int sum=0;
        for(int i=s;i<=t;i++)
        {
            if(flows[i]>0){add(supers,i,flows[i]);sum+=flows[i];}
            else if(flows[i]<0)add(i,supert,-flows[i]);
        }
        int flow1=dinic(supers,supert);//对构造好了的网络求最大流
        if(flow1!=sum)//观察附加边是否满流,如果没有满流说明无可行流,输出-1,终止本次任务
            printf("-1\n");
        else//如果满流,去掉附加边再求一次最大流
        {
            for(int i=head[supers];i!=-1;i=edge[i].next)
            {
                edge[i].f=0;edge[i^1].f=0;
            }
            for(int i=head[supert];i!=-1;i=edge[i].next)//去掉附加边的过程
            {
                edge[i].f=0;edge[i^1].f=0;
            }
            edge[temp].f=0;edge[temp^1].f=0;
            flow1+=dinic(s,t);//对剩余网络求最大流
            printf("%d\n",flow1);
            for(int i=sign;i<sign1;i+=2)
            {
                printf("%d\n",Minn[edge[i].s][edge[i].t-n]+edge[i^1].f);//输出路径
            }
        }
        printf("\n");
    }
    return 0;
}

 大神的递归dinic算法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int mn=2222;
const int mm=1000000;
const int oo=100000000;
int node, st, sd, edge;
int reach[mm], flow[mm], next[mm];
int head[mn], work[mn], dis[mn], que[mn];
int du[mn], dn[444][1111], id[444][1111];

inline void init(int _node, int _st, int _sd)
{
    node=_node, st=_st, sd=_sd;
    for(int i=0; i<node; i++)
        head[i]=-1, du[i]=0;
    edge=0;
}

inline void addedge(int u, int v, int c1, int c2)
{
    reach[edge]=v, flow[edge]=c1, next[edge]=head[u],head[u]=edge++;
    reach[edge]=u, flow[edge]=c2, next[edge]=head[v],head[v]=edge++;
}

bool bfs()
{
    int u, v, l=0, h=0;
    for(int i=0; i<node; i++) dis[i]=-1;
    que[l++]=st;
    dis[st]=0;
    while(l!=h)
    {
        u=que[h++];
        if(h==mn) h=0;
        for(int i=head[u]; i>=0; i=next[i])
        {
            v=reach[i];
            if(flow[i]&&dis[v]<0)
            {
                dis[v]=dis[u]+1;
                que[l++]=v;
                if(l==mn) l=0;
                if(v==sd) return true;
            }
        }
    }
    return false;
}

int dfs(int u, int exp)
{
    if(u==sd) return exp;
    for(int &i=work[u]; i>=0; i=next[i])
    {
        int v=reach[i], tp;
        if(flow[i]&&dis[v]==dis[u]+1&&(tp=dfs(v,min(flow[i],exp)))>0)
        {
            flow[i]-=tp;
            flow[i^1]+=tp;
            return tp;
        }
    }
    return 0;
}

int Dinic()
{
    int max_flow=0, flow;
    while(bfs())
    {
        for(int i=0; i<node; i++) work[i]=head[i];
        while(flow=dfs(st,oo)) max_flow+=flow;
    }
    return max_flow;
}

int main()
{
    int n, m, g, l, r, c, d;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init(n+m+2,n+m,n+m+1);
        for(int i=0; i<m; i++)
        {
            scanf("%d",&g);
            du[sd]+=g;
            du[i+n]-=g;
            addedge(i+n,sd,oo-g,0);
        }
        memset(id,0,sizeof(id));
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&c,&d);
            addedge(st,i,d,0);
            for(int j=0; j<c; j++)
            {
                scanf("%d%d%d",&g,&l,&r);
                du[i]-=l;
                du[g+n]+=l;
                dn[i][g]=l;
                addedge(i,g+n,r-l,0);
                id[i][g]=edge-1;
            }
        }
        addedge(sd,st,oo,0);
        st=node, sd=node+1, node+=2;
        head[st]=head[sd]=-1; ///!!
        int sum=0;
        for(int i=0; i<node-2; i++)
        {
            if(du[i]>0) sum+=du[i], addedge(st,i,du[i],0);
            else addedge(i,sd,-du[i],0);
        }
        int maxflow=Dinic();
        if(maxflow!=sum)  puts("-1");
        else
        {
            head[st]=head[sd]=-1, node-=2;
            st=node-2, sd=node-1;
            maxflow=Dinic();
            printf("%d\n",maxflow);
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                   if(id[i][j])
                     printf("%d\n",flow[id[i][j]]+dn[i][j]);
            }
        }
        puts("");
    }
    return 0;
}