注意一个地方,在建立好层次图进行dfs增广时,如果某个点增广不出流量,那么将该点level值置为0,即将它在该层次图中删除,剪枝,否则会超时~
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn=1050; #define inf 10000000 int n,m; int level[maxn],que[maxn*5]; int head[maxn],lon; int min(int a,int b) { if(a<b) return a; else return b; } struct { int next,to,c; }e[500100]; void edgeini() { memset(head,-1,sizeof(head)); lon=-1; } void edgemake(int from,int to,int c) { e[++lon].c=c; e[lon].to=to; e[lon].next=head[from]; head[from]=lon; } void make(int from,int to,int c) { edgemake(from,to,c); edgemake(to,from,0); } bool makelevel(int s,int t) { memset(level,0,sizeof(level)); int front=1,end=0; que[++end]=s; level[s]=1; while(front<=end) { int u=que[front++]; if(u==t) return true; for(int k=head[u];k!=-1;k=e[k].next) { int v=e[k].to; if(!level[v]&&e[k].c) { que[++end]=v; level[v]=level[u]+1; } } } return false; } int dfs(int now,int t,int maxf) { if(now==t||maxf==0) return maxf; int ret=0; for(int k=head[now];k!=-1;k=e[k].next) { int u=e[k].to; int f; if(level[u]==level[now]+1&&e[k].c) { f=dfs(u,t,min(e[k].c,maxf-ret)); e[k].c-=f; e[k^1].c+=f; ret+=f; if(ret==maxf) return ret; } } if (ret==0) level[now] = 0; return ret; } int maxflow(int s,int t) { int ret=0; while(makelevel(s,t)) { ret+=dfs(s,t,inf); } return ret; } int main() { int t; scanf("%d",&t); int sum=0; while(t--) { edgeini(); int STA,END; int tot=0; sum++; int i,j; int d=0; int p,s,e; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { scanf("%d%d%d",&p,&s,&e); tot+=p; if(e>d) d=e; make(0,i,p); for(j=s;j<=e;j++) make(i,j+n,1); } STA=0;END=d+n+1; for(i=1;i<=d;i++) make(i+n,END,m); int ans=maxflow(STA,END); printf("Case %d: ",sum); if(ans==tot) printf("Yes "); else printf("No "); printf(" "); } return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1050; #define inf 10000000 int n,m; int level[maxn],que[maxn*5]; int head[maxn],lon; int a[maxn*2]; int min(int a,int b) { if(a<b) return a; else return b; } struct { int next,to,c; }e[500100]; void edgeini() { memset(head,-1,sizeof(head)); lon=-1; } void edgemake(int from,int to,int c) { e[++lon].c=c; e[lon].to=to; e[lon].next=head[from]; head[from]=lon; } void make(int from,int to,int c) { edgemake(from,to,c); edgemake(to,from,0); } bool makelevel(int s,int t) { memset(level,0,sizeof(level)); int front=1,end=0; que[++end]=s; level[s]=1; while(front<=end) { int u=que[front++]; if(u==t) return true; for(int k=head[u];k!=-1;k=e[k].next) { int v=e[k].to; if(!level[v]&&e[k].c) { que[++end]=v; level[v]=level[u]+1; } } } return false; } int dfs(int now,int t,int maxf) { if(now==t||maxf==0) return maxf; int ret=0; for(int k=head[now];k!=-1;k=e[k].next) { int u=e[k].to; int f; if(level[u]==level[now]+1&&e[k].c) { f=dfs(u,t,min(e[k].c,maxf-ret)); e[k].c-=f; e[k^1].c+=f; ret+=f; if(ret==maxf) return ret; } } if (ret==0) level[now] = 0; return ret; } int maxflow(int s,int t) { int ret=0; while(makelevel(s,t)) { ret+=dfs(s,t,inf); } return ret; } int main() { int t; while(scanf("%d%d",&n,&m)!=EOF) { edgeini(); int STA,END; int tot=0; int i,j; int si[maxn],ni,ei[maxn],ti; int cnt=0; STA=0; for(i=1;i<=n;i++) { scanf("%d%d%d%d",&si[i],&ni,&ei[i],&ti); make(STA,i,ni*ti); tot+=ni*ti; a[cnt++]=si[i]; a[cnt++]=ei[i]; } sort(a,a+cnt); j=1; for(i=1;i<cnt;i++) if(a[i]>a[i-1]) a[j++]=a[i]; cnt=j; END=n+cnt; for(i=1;i<=cnt-1;i++) { make(n+i,END,m*(a[i]-a[i-1])); for(j=1;j<=n;j++) if(a[i-1]>=si[j]&&a[i]<=ei[j]) make(j,n+i,inf); } int ans=maxflow(STA,END); if(ans==tot) printf("Yes "); else printf("No "); } return 0; }