ZOJ 3229 无源汇上下界最大流

来源:互联网 发布:智多星软件视频教程 编辑:程序博客网 时间:2024/06/05 10:15
Shoot the Bullet
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu

[Submit]  [Go Back]  [Status]  

Description

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 inGensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns theBunkachou - 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 ofGensokyo among the tengu. Her intelligence gathering abilities are the best inGensokyo!

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 leastGx photos of girl x in total in the Bunkachou. At thek-th day, there are Ck targets, Tk1,Tk2, ..., TkCk. The number of photos of targetTki that Aya takes should be in range [Lki,Rki], if less, Aya cannot write an interesting article, if more, the girl will become angry and use herlast 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. Thenm integers, G1, G2, ..., Gm in range [0, 10000]. Thenn days. Each day begins with two integer 1 <= C <= 100, 0 <= D <= 30000. Then Cdifferent 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


把每一天看成一个点,每个女孩也看成一个点,增加源和汇s、t,源向每一天连上[0,d]的边,每一天与每个女孩如果有拍照任务的话连上[l,r]的边,每个女孩与汇连上[g,oo]的边,于是构成一个有上下界的图

所以这道题目我们可以转换一下

只要连一条T → S的边,流量为无穷,没有下界,那么原图就得到一个无源汇的循环流图。接下来的事情一样:原图中的边的流量设成自由流量ci – bi。新建源点SS汇点TT,求Mi,连边。然后求SS → TT最大流,判是否满流。

判定有解之后然后求最大流,信息都在上面求得的残留网络里面。满足所有下界时,从s → t的流量为后悔边s → t的边权!然后在残留网络中s → t可能还有些自由流没有流满,再做一次s → t的最大流,所得到的最大流就是原问题的最大流(内含两部分:残留的自由流所得到的流+后悔边s → t)。

low数组开小,oj居然不报re,而是wa,检查了一天,简直坑爹。

代码:

/* ***********************************************Author :rabbitCreated Time :2014/4/6 15:11:05File Name :10.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;const int maxn=20100;const int maxm=1002000;struct Edge{int next,to,cap;Edge(){};Edge(int _next,int _to,int _cap){next=_next;to=_to;cap=_cap;}}edge[maxm];int head[maxn],tol,dep[maxn],gap[maxn];void addedge(int u,int v,int flow){    edge[tol]=Edge(head[u],v,flow);head[u]=tol++;    edge[tol]=Edge(head[v],u,0);head[v]=tol++;}void bfs(int start,int end){    memset(dep,-1,sizeof(dep));    memset(gap,0,sizeof(gap));    gap[0]++;int front=0,rear=0,Q[maxn];    dep[end]=0;Q[rear++]=end;    while(front!=rear){        int u=Q[front++%maxn];        for(int i=head[u];i!=-1;i=edge[i].next){            int v=edge[i].to;if(dep[v]==-1)                Q[rear++%maxn]=v,dep[v]=dep[u]+1,gap[dep[v]]++;        }    }}int sap(int s,int t,int N){int res=0;bfs(s,t);int cur[maxn],S[maxn],top=0,u=s,i;memcpy(cur,head,sizeof(head));while(dep[s]<N){if(u==t){int temp=INF,id;    for( i=0;i<top;i++)   if(temp>edge[S[i]].cap)   temp=edge[S[i]].cap,id=i;    for( i=0;i<top;i++)      edge[S[i]].cap-=temp,edge[S[i]^1].cap+=temp;    res+=temp;top=id;u=edge[S[top]^1].to;}if(u!=t&&gap[dep[u]-1]==0)break;for( i=cur[u];i!=-1;i=edge[i].next)if(edge[i].cap&&dep[u]==dep[edge[i].to]+1)break;if(i!=-1)cur[u]=i,S[top++]=i,u=edge[i].to;else{int MIN=N;for( i=head[u];i!=-1;i=edge[i].next)if(edge[i].cap&&MIN>dep[edge[i].to])MIN=dep[edge[i].to],cur[u]=i;--gap[dep[u]];++gap[dep[u]=MIN+1];if(u!=s)u=edge[S[--top]^1].to;}}return res;}int d[maxn],in[maxn],low[100010];int main(){int n,m;while(~scanf("%d%d",&n,&m)){memset(head,-1,sizeof(head));tol=0;memset(in,0,sizeof(in));for(int i=1;i<=m;i++){int g;scanf("%d",&g);in[n+m+1]+=g;in[i+n]-=g;}int e=0;for(int i=1;i<=n;i++){int c;scanf("%d%d",&c,&d[i]);for(int j=1;j<=c;j++){int T,up;scanf("%d%d%d",&T,&low[++e],&up);addedge(i,T+n+1,up-low[e]);in[i]-=low[e];in[T+n+1]+=low[e];}}for(int i=1;i<=n;i++)addedge(0,i,d[i]);for(int i=1;i<=m;i++)addedge(i+n,n+m+1,INF);addedge(n+m+1,0,INF);int sum=0;for(int i=0;i<=n+m+1;i++){if(in[i]>0)addedge(n+m+2,i,in[i]),sum+=in[i];if(in[i]<0)addedge(i,n+m+3,-in[i]);}int ans=sap(n+m+2,m+n+3,m+n+1000);if(ans==sum){int pp=sap(0,m+n+1,m+n+1000);addedge(n+m+1,0,-INF);int cnt=0;for(int i=head[0];i!=-1;i=edge[i].next)cnt+=edge[i^1].cap;printf("%d\n",cnt);for(int i=1;i<=e;i++)printf("%d\n",edge[2*i-1].cap+low[i]);}else puts("-1");puts("");}}


0 0