1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| #include <cstdio> #include <cstring> #include <algorithm> #define M 205 #define N 10005 #define oo 1000000007 using namespace std; struct kxj{ int t,nxt,c; }E[N<<1]; int clean[105][105],A[105],B[105],S[105]; int n,Q[M],dis[M],head[M],tot,cur[M]; bool mark[105]; void Add(int a,int b,int c){ E[tot]=(kxj){b,head[a],c};head[a]=tot++; E[tot]=(kxj){a,head[b],0};head[b]=tot++; } bool bfs(){ for(int i=0;i<=n;i++)dis[i]=-1; int L=0,R=0; Q[R++]=0;dis[0]=0; while(L<R){ int x=Q[L++]; for(int i=head[x];~i;i=E[i].nxt){ int y=E[i].t; if(E[i].c&&dis[y]==-1){ dis[y]=dis[x]+1; Q[R++]=y; } } } return dis[n]!=-1; } int dfs(int x,int flow){ int res=0; if(x==n||flow==0)return flow; for(int &i=cur[x];~i;i=E[i].nxt){ int y=E[i].t; if(E[i].c&&dis[y]==dis[x]+1){ int tmp=dfs(y,min(E[i].c,flow)); E[i].c-=tmp; E[i^1].c+=tmp; flow-=tmp; res+=tmp; if(flow==0)break; } } return res; } int Dinic(){ int ans=0; while(bfs()){ for(int i=0;i<=n;i++)cur[i]=head[i]; ans+=dfs(0,oo); } return ans; } int main(){ int T,m,o=0; scanf("%d",&T); while(T--&&scanf("%d %d",&n,&m)){ memset(head,-1,sizeof(head)); memset(mark,0,sizeof(mark)); tot=0; int ans=0; for(int i=1;i<=n;i++)scanf("%d %d %d",A+i,B+i,S+i),S[i]=(S[i]-1)/m+1; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&clean[i][j]); for(int i=1;i<=n;i++){ ans+=S[i]; Add(0,i,S[i]); for(int j=1;j<=n;j++) if(A[j]>B[i]+clean[i][j])Add(i,j+n,oo); Add(i+n,n+n+1,S[i]); } n=n+n+1; printf("Case %d: %d\n",++o,ans-Dinic()); } return 0; }
|