最大流问题:dinic做的,isap正在学习中
建图还是比较容易的:
源点与每个任务建边流量为任务需要的时间,任务与时间段的每个时间点建边流量为1,
每个时间点与汇点建边流量为机器的个数
#include<stdio.h>
#include<queue>
#include<string.h>
#define N 3000
#define inf 0x3fffffff
using namespace std;
int head[N],num,d[N],n,et;
struct edge
{
int st,ed,flow,next;
}E[500100];
void addedge(int x,int y,int w)
{
E[num].st=x;
E[num].ed=y;
E[num].flow=w;
E[num].next=head[x];
head[x]=num++;
}
bool bfs(int sp,int tp)
{
int u,v,i;
memset(d,-1,sizeof(d));
queue<int>Q;
d[sp]=0;
Q.push(sp);
while(!Q.empty())
{
u=Q.front();
Q.pop();
for(i=head[u];i!=-1;i=E[i].next)
{
v=E[i].ed;
if(d[v]==-1&&E[i].flow>0)
{
d[v]=d[u]+1;
Q.push(v);
}
}
}
if(d[tp]==-1)return false;
return true;
}
int dfs(int u,int minflow)
{
if(u==et)return minflow;
int i,v,r=0,f;
for(i=head[u];i!=-1;i=E[i].next)
{
v=E[i].ed;
if(d[v]==d[u]+1&&E[i].flow>0)
{
v=E[i].ed;
if(d[v]==d[u]+1&&E[i].flow>0&&r<minflow&&(f=dfs(v,E[i].flow>minflow-r?minflow-r:E[i].flow)))
{
E[i].flow-=f;
E[i^1].flow+=f;
r+=f;
}
}
}
if(f==0)d[u]=-1;
return r;
}
int dinic(int sp,int tp)
{
int maxflow=0,sum;
while(bfs(sp,tp))
{
while(sum=dfs(sp,inf))
maxflow+=sum;
}
return maxflow;
}
int main()
{
int a,b,c,i,m,t,op=1,j,max,sum,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
num=0;max=0;sum=0;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&a,&b,&c);
sum+=a;
addedge(0,i,a);
addedge(i,0,0);
if(max<c)max=c;
for(j=b;j<=c;j++)
{
addedge(i,n+j,1);
addedge(n+j,i,0);
}
}
et=n+max+1;
for(i=1;i<=max;i++)
{
addedge(n+i,et,m);
addedge(et,n+i,0);
}
ans=dinic(0,et);
printf("Case %d: ",op++);
if(sum==ans)
printf("Yes\n");
else printf("No\n");
printf("\n");
}
return 0;
}