题目地址:POJ 1698
水题。。将每部电影与它可以演的那一天连边就行了。建二分图。用二分最大匹配也完全可以做。
代码如下:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include <set> #include <algorithm> using namespace std; const int INF=0x3f3f3f3f; int head[2000], source, sink, nv, cnt; int cur[2000], num[2000], d[2000], pre[2000]; struct node { int u, v, cap, next; } edge[1000000]; void add(int u, int v, int cap) { edge[cnt].v=v; edge[cnt].cap=cap; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cap=0; edge[cnt].next=head[v]; head[v]=cnt++; } void bfs() { memset(num,0,sizeof(num)); memset(d,-1,sizeof(d)); queue<int>q; q.push(sink); d[sink]=0; num[0]=1; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(d[v]==-1) { d[v]=d[u]+1; num[d[v]]++; q.push(v); } } } } int isap() { memcpy(cur,head,sizeof(cur)); int flow=0, u=pre[source]=source, i; bfs(); while(d[source]<nv) { if(u==sink) { int f=INF, pos; for(i=source; i!=sink; i=edge[cur[i]].v) { if(f>edge[cur[i]].cap) { f=edge[cur[i]].cap; pos=i; } } for(i=source; i!=sink; i=edge[cur[i]].v) { edge[cur[i]].cap-=f; edge[cur[i]^1].cap+=f; } flow+=f; u=pos; } for(i=cur[u]; i!=-1; i=edge[i].next) { if(d[edge[i].v]+1==d[u]&&edge[i].cap) break; } if(i!=-1) { cur[u]=i; pre[edge[i].v]=u; u=edge[i].v; } else { if(--num[d[u]]==0) break; int mind=nv; for(i=head[u]; i!=-1; i=edge[i].next) { if(mind>d[edge[i].v]&&edge[i].cap) { mind=d[edge[i].v]; cur[u]=i; } } d[u]=mind+1; num[d[u]]++; u=pre[u]; } } return flow; } int main() { int t, n, a[10], d, w, i, j, k, sum; scanf("%d",&t); while(t--) { scanf("%d",&n); sum=0; memset(head,-1,sizeof(head)); cnt=0; source=0; sink=n+50*7+1; nv=sink+1; for(i=1;i<=n;i++) { for(j=1;j<=7;j++) { scanf("%d",&a[j]); } scanf("%d%d",&d,&w); add(source,i,d); sum+=d; for(j=1;j<=7;j++) { if(a[j]) { for(k=1;k<=w;k++) { add(i,n+7*(k-1)+j,1); } } } } for(i=1;i<=50;i++) { for(j=1;j<=7;j++) { add((i-1)*7+j+n,sink,1); } } int ans=isap(); if(ans>=sum) printf("Yes\n"); else printf("No\n"); } return 0; }