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

来源:互联网 发布:百年孤独中的名句 知乎 编辑:程序博客网 时间:2024/05/17 02:28

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 312 12 123 180 3 91 3 92 3 93 180 3 91 3 92 3 92 312 12 123 180 3 91 3 92 3 93 180 0 31 3 62 6 92 312 12 123 150 3 91 3 92 3 93 210 0 31 3 62 6 12

Sample Output

3666666636963369-1

n天给m个女的拍照片 下面有m个数字Gx  代表最多给第x个女的拍Gx张照片

然后下面有n组数字 每组 首先有 C D 代表给C个人拍照  这一天最多拍D张照片

下面有C行  三个数字 分别代表给第j个拍照的上下界

求这些天能够拍的最多的照片数量


很显然二部图由人数 天数构成

容量网络的构成 添加源点是 汇点t

s与天数的每个节点相连 权值为这一天至少拍的照片数量

人数的节点与t相连 权值为这个人最多的照片数量

天数与人数相连 上下界为这个人这一天能够拍照数量的上下界

添加一个t->s 权值为INF  转化为无源汇的容量网络

然后根据容量网络建图 添加超级源点ss 超级汇点tt

如果ss->tt的网络流有最大值 则存在可行流 再将ss tt 删除 跑s->t的最大流 得到最终的最大流就是答案

每条边的最大流就是 low[i][j]+第二次最大流流通的自由流


第一次写有源汇有上下界的最大流  不知道如何处理删除ss tt 然后继续跑最大流

借鉴了别人的写法

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <queue>#include <vector>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 10010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl;#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;void read(int &x){    char ch;    x=0;    while(ch=getchar(),ch!=' '&&ch!='\n')    {        x=x*10+ch-'0';    }}struct Edge{    int from,to,cap,flow;    bool operator <(const Edge e) const    {        if(e.from!=from)  return from<e.from;        else return to<e.to;    }    Edge() {}    Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow) {}};struct Dinic{    vector<Edge> edges;    vector<int> G[MAXN];    bool vis[MAXN];//BFS使用    int d[MAXN];   //从起点到i的距离    int cur[MAXN]; //当前弧下标    int n,m,s,t,maxflow;   //节点数 边数(包括反向弧) 源点编号和弧点编号    void init(int n)    {        this->n=n;        for(int i=0;i<=n;i++)            G[i].clear();        edges.clear();    }    void addedge(int from,int to,int cap)    {        edges.push_back(Edge(from,to,cap,0));        edges.push_back(Edge(to,from,0,0));        m=edges.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    bool bfs()    {        MEM(vis,0);        MEM(d,-1);        queue<int> q;        q.push(s);        d[s]=maxflow=0;        vis[s]=1;        while(!q.empty())        {            int u=q.front(); q.pop();            int sz=G[u].size();            for(int i=0;i<sz;i++)            {                Edge e=edges[G[u][i]];                if(!vis[e.to]&&e.cap>e.flow)                {                    d[e.to]=d[u]+1;                    vis[e.to]=1;                    q.push(e.to);                }            }        }        return vis[t];    }    int dfs(int u,int a)    {        if(u==t||a==0)  return a;        int sz=G[u].size();        int flow=0,f;        for(int &i=cur[u];i<sz;i++)        {            Edge &e=edges[G[u][i]];            if(d[u]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)            {                e.flow+=f;                edges[G[u][i]^1].flow-=f;                flow+=f;                a-=f;                if(a==0)  break;            }        }        return flow;    }    int Maxflow(int s,int t)    {        this->s=s; this->t=t;        int flow=0;        while(bfs())        {            MEM(cur,0);            flow+=dfs(s,INF);        }        return flow;    }}Dic;int out[1500],low[400][1100];int id[400][1100];int main(){//    fread;    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        int s=0,t=n+m+1;        int ss=n+m+2,tt=n+m+3;        Dic.init(tt);        MEM(out,0); MEM(low,0); MEM(id,0);        for(int i=1;i<=m;i++)        {            int c;            scanf("%d",&c);            Dic.addedge(i+n,t,INF-c);            out[i+n]-=c;            out[t]+=c;        }        int c,d;        for(int i=1;i<=n;i++)        {            scanf("%d%d",&c,&d);            Dic.addedge(s,i,d);//            out[0]-=d;//            out[i]+=d;            for(int j=0;j<c;j++)            {                int u,l,h;                scanf("%d%d%d",&u,&l,&h);                low[i][u+1]=l;//                high[i][u+1]=h;                Dic.addedge(i,u+n+1,h-l);                out[i]-=l;                out[u+1+n]+=l;                id[i][u+1]=Dic.edges.size()-2;            }        }        Dic.addedge(t,s,INF);        int sum=0;        for(int i=1;i<=n+m+1;i++)        {            if(out[i]>0)            {                Dic.addedge(ss,i,out[i]);                sum+=out[i];            }            else if(out[i]<0)                Dic.addedge(i,tt,-out[i]);        }        int mx=Dic.Maxflow(ss,tt);//        cout<<mx<<endl;//        cout<<sum<<endl;        if(mx!=sum)        {            puts("-1");            puts("");            continue;        }//        Dic.addedge(t,s,-INF);        int ans=Dic.Maxflow(s,t);        printf("%d\n",ans);        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                int x=id[i][j];//                cout<<"xx "<<x<<endl;//                cout<<"low "<<low[i][j]<<endl;                if(x)                    printf("%d\n",Dic.edges[x].flow+low[i][j]);            }        }        puts("");    }    return 0;}





0 0
原创粉丝点击