标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7015 Accepted Submission(s): 2192
#include<iostream> #include<cstdio> #include<cstring> #include <algorithm> #include <math.h> #include <queue> using namespace std; const int N = 10000; const int INF = 999999999; struct Edge{ int v,w,next; }edge[N*N]; int head[N]; int p[N],s[N],e[N]; int level[N]; int tot,n,m; void init() { memset(head,-1,sizeof(head)); tot=0; } void addEdge(int u,int v,int w,int &k) { edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++; edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++; } int BFS(int src,int des) { queue<int>q; memset(level,0,sizeof(level)); level[src]=1; q.push(src); while(!q.empty()) { int u = q.front(); q.pop(); if(u==des) return 1; for(int k = head[u]; k!=-1; k=edge[k].next) { int v = edge[k].v; int w = edge[k].w; if(level[v]==0&&w!=0) { level[v]=level[u]+1; q.push(v); } } } return -1; } int dfs(int u,int des,int increaseRoad){ if(u==des||increaseRoad==0) return increaseRoad; int ret=0; for(int k=head[u];k!=-1;k=edge[k].next){ int v = edge[k].v,w=edge[k].w; if(level[v]==level[u]+1&&w!=0){ int MIN = min(increaseRoad-ret,w); w = dfs(v,des,MIN); if(w > 0) { edge[k].w -=w; edge[k^1].w+=w; ret+=w; if(ret==increaseRoad) return ret; } else level[v] = -1; if(increaseRoad==0) break; } } if(ret==0) level[u]=-1; return ret; } int Dinic(int src,int des) { int ans = 0; while(BFS(src,des)!=-1) ans+=dfs(src,des,INF); return ans; } int main() { int tcase; scanf("%d",&tcase); int t = 1; while(tcase--){ init(); scanf("%d%d",&n,&m); int MIN = INF,MAX = -1,sum = 0; for(int i=1;i<=n;i++){ scanf("%d%d%d",&p[i],&s[i],&e[i]); MIN = min(s[i],MIN); MAX = max(e[i],MAX); sum+=p[i]; } int start = 0,ed = n+(MAX-MIN+1)+m+1; for(int i=1;i<=n;i++){ addEdge(start,i,p[i],tot); } for(int i=1;i<=n;i++){ for(int j=s[i]+n;j<=e[i]+n;j++){ addEdge(i,j,1,tot); } } for(int i=MIN+n;i<=MAX+n;i++){ for(int j=n+MAX-MIN+1+1;j<ed;j++){ addEdge(i,j,1,tot); } } for(int i=n+MAX-MIN+1+1;i<ed;i++){ addEdge(i,ed,INF,tot); } int max_flow = Dinic(start,ed); printf("Case %d: ",t++); if(max_flow==sum){ printf("Yes\n"); }else{ printf("No\n"); } printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5720303.html