Zoj 3229 shoot the bullet maximum upstream/downstream stream

Source: Internet
Author: User

Shoot the bullet Time Limit: 2 seconds memory limit: 32768 kb Special Judge

GensokyoIs 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 inGensokyoFor over 1000 years. She runs the bunbunmaru news-a newspaper chock-full of rumors, and ownsBunkachou-Her record of interesting observations for bunbunmaru news articles and pictures of beautifulDanmaku(Barrange) or cute girls living inGensokyo. She is the biggest connoisseur of rumors about the girlsGensokyoAmong the Tengu. Her intelligence gathering abilities are the best inGensokyo!

during the coming n days, aya is planning to take advantage of photos of m cute girls living in gensokyo to write bunmaru 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, TK1 , tk2 ,..., tkck . the number of photos of target TKI that Aya takes shocould be in range [ lki , RKI ], 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& Lt; = 365, 1 & lt; =M<= 1000. ThenMIntegers,G1,G2,...,GMIn range [0, 10000]. ThenNDays. Each day begins with two integer 1 <=C& Lt; = 100, 0 & lt; =D<= 30000. ThenC 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 shocould 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

External links

 
Wikipedia
Touhou Wiki


Every day is regarded as a point, and every girl is also seen as a point, increasing the source and sink, and connecting the source to the edge of [0, d] each day, every day is connected to every girl if they have a photo task with the [L, C] side, and every girl is connected to the [g, oo] side, thus forming a graph with the upper and lower bounds, simply use the maximum stream in the lower bound.

#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <map>#include <set>#include <queue>#include <stack>#include <bitset>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(int i=0;i<(n);++i)#define FOR(i,l,h) for(int i=(l);i<=(h);++i)#define DWN(i,h,l) for(int i=(h);i>=(l);--i)#define CLR(vis,pos) memset(vis,pos,sizeof(vis))#define PI acos(-1.0)#define INF 0x3f3f3f3f#define LINF 1000000000000000000LL#define eps 1e-8typedef long long ll;const int MAXN=2222;struct Edge{    int from,to,cap,flow;};bool cmp(const Edge& a,const Edge& b){    return a.from < b.from || (a.from == b.from && a.to < b.to);}struct Dinic{    int n,m,s,t;    vector<Edge> edges;    vector<int> G[MAXN];    bool vis[MAXN];    int d[MAXN];    int cur[MAXN];    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});//当是无向图时,反向边容量也是cap,有向边时,反向边容量是0        m=edges.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    bool BFS(){        CLR(vis,0);        queue<int> Q;        Q.push(s);        d[s]=0;        vis[s]=1;        while(!Q.empty()){            int x=Q.front();            Q.pop();            for(int i=0;i<G[x].size();i++){                Edge& e=edges[G[x][i]];                if(!vis[e.to]&&e.cap>e.flow){                    vis[e.to]=1;                    d[e.to]=d[x]+1;                    Q.push(e.to);                }            }        }        return vis[t];    }    int DFS(int x,int a){        if(x==t||a==0)return a;        int flow=0,f;        for(int& i=cur[x];i<G[x].size();i++){            Edge& e=edges[G[x][i]];            if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){                e.flow+=f;                edges[G[x][i]^1].flow-=f;                flow+=f;                a-=f;                if(a==0)break;            }        }        return flow;    }    //当所求流量大于need时就退出,降低时间    int Maxflow(int s,int t,int need){        this->s=s;this->t=t;        int flow=0;        while(BFS()){            CLR(cur,0);            flow+=DFS(s,INF);            if(flow>need)return flow;        }        return flow;    }    //最小割割边    vector<int> Mincut(){        BFS();        vector<int> ans;        for(int i=0;i<edges.size();i++){            Edge& e=edges[i];            if(vis[e.from]&&!vis[e.to]&&e.cap>0)ans.push_back(i);        }        return ans;    }    void Reduce(){        for(int i = 0; i < edges.size(); i++) edges[i].cap -= edges[i].flow;    }    void ClearFlow(){        for(int i = 0; i < edges.size(); i++) edges[i].flow = 0;    }};int w[MAXN],d[MAXN],low[100010];Dinic solver;int main(){    int n,m;    while(~scanf("%d%d",&n,&m)){        solver.init(n+m+3);        int tmp;        CLR(w,0),CLR(d,0),CLR(low,0);        FOR(i,1,m){            scanf("%d",&tmp);            w[n+i]-=tmp;            w[n+m+1]+=tmp;        }        int c;        int g=0;        FOR(i,1,n){            scanf("%d%d",&c,&d[i]);            int t;            REP(j,c){                scanf("%d%d%d",&t,&low[++g],&tmp);                t++;                solver.AddEdge(i,t+n,tmp-low[g]);                w[i]-=low[g];                w[t+n]+=low[g];            }        }        FOR(i,1,n) solver.AddEdge(0,i,d[i]);        FOR(i,1,m) solver.AddEdge(n+i,n+m+1,INF);        solver.AddEdge(n+m+1,0,INF);        int sum=0;        FOR(i,0,n+m+1){            if(w[i]>0){                solver.AddEdge(n+m+2,i,w[i]);                sum+=w[i];            }            if(w[i]<0)                solver.AddEdge(i,n+m+3,-w[i]);        }        int ans=solver.Maxflow(n+m+2,n+m+3,2*INF);        //cout<<sum<<" "<<ans<<endl;        if(ans!=sum){            printf("-1\n");        }        else{            tmp=solver.Maxflow(0,n+m+1,INF);            cout<<tmp<<endl;            FOR(i,1,g)             printf("%d\n",solver.edges[2*i-2].flow+low[i]);        }        printf("\n");    }    return 0;}


Zoj 3229 shoot the bullet maximum upstream/downstream stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.