Title Description
Description
God Ben Yjq has n different sister and M food, each day each food only supplies a sister to eat the weight. Within the next T-day, YJQ prepared to pack all the girls. For each sister, she will only eat certain food in the T days, and there are a total of Q special requirements, each requirement is the TI days AI sister will not be able to eat the first fi food for special reasons. In some cases, to meet the requirements of the sister, must change all the girls feeding program. The cost of redistributing food every day is different, and YJQ is reluctant to pay so much, so he hopes to change the cost of her feeding program as little as possible. Now, YJQ, please make a food distribution plan for his sister, and he wants to make sure that every girl has food to eat every day, so that the cost of changing the distribution plan is minimal. The minimum cost of the request. If YJQ no matter how the allocation can not be in the days to raise all the girls (to raise all the sister is to make all girls in ∀i (1<=i<=t) days have food), please output "1".
Note: The initial allocation of food on the first day does not take
Enter a description
Input Description
The first line of 4 integers n,m,t,q indicates the number of females in YJQ, the number of food types, the number of days, and the special requirements; The next line has a T integer representing the cost of modifying the food plan every day; The next n lines, the number of m per line, row i, Column J, indicates whether the first girl can eat the first J of food (1, 0 means no); Next Q line, 3 integers per line ti,ai,fi mean ti days Ai No, sister can't eat food fi
Output description
Output Description
An integer representing the minimum cost of YJQ, if not all girls can be raised in any case, output-1
Sample input
Sample Input
3 3 3 2 2 3 3 1 1 0 0 1 1 1 0 1 1 1 1 2 2 3
Sample output
Sample Output
3
Data range and Tips
Data Size & Hint
For 30% of data, n,m,t,q<4;
For 50% of data, t<20;
For 100% of the data, n,m<100,t<500,q<10^4, all the given data are non-negative integers (friendly hint, yjq may not have sister)
For each I back to find the furthest J satisfies i~j days in the not satisfied edges are deleted after doing the binary map matching, can all points are matched r[i]=J and R[i] have monotonicity, certain>=r[i-1]+1if R[i]=I can not meet on no solution R[i]=J that is, after the I-day replacement program, to J days are not replaced, and finally do DP solution (PS. The price for the 1th day is 0#include<cstdio><o:p></o:p>#include<algorithm><o:p></o:p>#defineN 505<o:p></o:p>Const intinf=1000000009;<o:p></o:p>structfff{intT,a,f;} S[n*n];<o:p></o:p>intN,m,d,qe;<o:p></o:p>intC[n],f[n][n],vis[n],p[n],q[n][n],l[n],r[n],k[n],fir[n];<o:p></o:p>BOOLcmpConstFff&x,ConstFff&y) {returnx.t<y.t;} <o:p></o:p>intMinintXintY) {returnx>y?y:x;} <o:p></o:p>int<st1:place w:st=" on"><st1:city w:st=" on">gary</st1:city></st1:place> (intx) <o:p></o:p>{<o:p></o:p> for(intI=1; i<=m;i++) <o:p></o:p>if(f[x][i]>0&&!vis[i]) {<o:p></o:p>Vis[i]=1;<o:p></o:p>if(!p[i]| | Gary (P[i]) {<o:p></o:p>P[i]=x;<o:p></o:p>return 1;<o:p></o:p> }<o:p></o:p> }<o:p></o:p>return 0;<o:p></o:p>}<o:p></o:p>intMain () <o:p></o:p>{scanf ("%d%d%d%d", &N,&M,&D,&QE);<o:p></o:p>if(n==0) {printf ("0");return 0;} <o:p></o:p>intLe,i,j,h,t,ans;<o:p></o:p> for(i=1; i<=d;i++) scanf ("%d", &c[i]);<o:p></o:p> for(i=1; i<=n;i++) <o:p></o:p> for(j=1; j<=m;j++) scanf ("%d", &f[i][j]);<o:p></o:p> for(i=1; i<=qe;i++) scanf ("%d%d%d", &S[I].T,&S[I].A,&S[I].F);<o:p></o:p>Std::sort (S+1, s+1+QE,CMP);<o:p></o:p> for(i=1; i<=qe;i++) <o:p></o:p>if(s[i].t!=s[i-1].T) fir[s[i].t]=i;<o:p></o:p> for(h=1, le=0; h<=d;h++) {<o:p></o:p>if(r[h-1]==d) { for(i=h;i<=d;i++) q[h][d]=1; r[h]=d;Continue; }<o:p></o:p> for(t=r[h-1]+1;; t++) {<o:p></o:p>if(t>d) {t--; Break;} <o:p></o:p> for(i=fir[t];i&&i<=qe&&s[i].t==t;i++) f[s[i].a][s[i].f]--;//Edge <o:p></o:p> Delete for(i=1; i<=m;i++) p[i]=0;<o:p></o:p> for(i=1, ans=0; i<=n;i++) {<o:p></o:p> for(j=1; j<=m;j++) vis[j]=0;<o:p></o:p>ans+=gary (i);<o:p></o:p> }<o:p></o:p>if(ans<n) {<o:p></o:p> for(i=fir[t];i&&s[i].t==t&&i<=qe;i++) f[s[i].a][s[i].f]++;t--; Break;}//Recovery Diagram <o:p></o:p> }<o:p></o:p>if(T<H) {printf ("-1");return 0;} <o:p></o:p> for(i=h;i<=t;i++) q[h][i]=1;r[h]=t;<o:p></o:p> for(i=fir[h];i&&s[i].t==h&&i<=qe;i++) f[s[i].a][s[i].f]++;//Recovery Diagram <o:p></o:p> }<o:p></o:p> for(i=1; i<=d;i++) {l[i]=inf;q[i][i]=1;} <o:p></o:p>c[1]=l[1]=0; <o:p></o:p> for(i=1; i<=d;i++) <o:p></o:p> for(j=i;j<=d;j++) <o:p></o:p>if(Q[i][j]) l[j]=min (l[j],l[i-1]+c[i]);<o:p></o:p>printf ("%d", L[d]);<o:p></o:p>return 0;<o:p></o:p>}
codevs4439 YJQ Requires Food