题意:i-th task,start time>=si,time of processing is pi days,it should be finished before ei days.
Tip1.one machine compute one task at one time;
Tip2.one task should be done on one machine at one time;
思路:设sink和source,每一个时间虚拟为一个点,source与每一个task连一条容量为pi的边,
每一个task向它的时间段的每一个点连一条容量为1的边(题目的限制Tip2),最后每一个时间点向
sink连一条容量为the number of machine(Tip1)。
#include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<cstdio> #define inf 0x7fffffff using namespace std; int n,m; int sum; int st,ed; bool cmp(int a,int b) { return a<b; } struct nodee { int v,next,flow; }e[505*505*2]; int head[505*2],cnt; class Dinic { public: int spath() { queue<int>q; while(!q.empty()) q.pop(); memset(dis,-1,sizeof(dis)); dis[st]=0; q.push(st); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i+1;i=e[i].next) { int v=e[i].v; if(dis[v]==-1&&e[i].flow>0) { dis[v]=dis[u]+1; q.push(v); } } } return dis[ed]!=-1; } int Min(int a,int b) { if(a<b) return a; return b; } int dfs(int u,int flow) { int cost=0; if(u==ed) return flow; for(int i=head[u];i+1;i=e[i].next) { int v=e[i].v; if(dis[v]==dis[u]+1&&e[i].flow>0) { int min=dfs(v,Min(e[i].flow,flow-cost)); if(min>0) { e[i].flow-=min; e[i^1].flow+=min; cost+=min; if(cost==flow) break; } else dis[v]=-1; } } return cost; } int result() { int res=0; while(spath()) { res+=dfs(st,inf); } return res; } private: int dis[505*2]; }dinic; void Init() { memset(head,-1,sizeof(head)); cnt=0; } void add(int a,int b,int c) { e[cnt].v=b; e[cnt].flow=c; e[cnt].next=head[a]; head[a]=cnt++; e[cnt].v=a; e[cnt].flow=0; e[cnt].next=head[b]; head[b]=cnt++; } void Input() { int maxx=-1; int minn=inf; st=0; sum=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { int p,s,e; scanf("%d%d%d",&p,&s,&e); sum+=p; add(st,i,p); if(e>maxx) maxx=e; if(s<minn) minn=s; for(int j=s;j<=e;j++) { add(i,n+j,1); } } ed=n+maxx+1; for(int i=n+minn;i<=n+maxx;i++) add(i,ed,m); } void treatment(int ca) { cout<<"Case "<<ca<<": "; int k=dinic.result(); //cout<<k<<endl; if(k==sum) cout<<"Yes"<<endl; else cout<<"No"<<endl; cout<<endl; } int main() { int t,ca=1; scanf("%d",&t); while(t--) { Init(); Input(); treatment(ca++); } return 0; }