hdu 3572 Task Schedule(邻接表dinic)

来源:互联网 发布:网络用语bgm是什么意思 编辑:程序博客网 时间:2024/05/22 09:40

Task Schedule



Problem Description
Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th task, the factory has to start processing it at or after day Si, process it for Pi days, and finish the task before or at day Ei. A machine can only work on one task at a time, and each task can be processed by at most one machine at a time. However, a task can be interrupted and processed on different machines on different days. 
Now she wonders whether he has a feasible schedule to finish all the tasks in time. She turns to you for help.
 

Input
On the first line comes an integer T(T<=20), indicating the number of test cases.

You are given two integer N(N<=500) and M(M<=200) on the first line of each test case. Then on each of next N lines are three integers Pi, Si and Ei (1<=Pi, Si, Ei<=500), which have the meaning described in the description. It is guaranteed that in a feasible schedule every task that can be finished will be done before or at its end day.
 

Output
For each test case, print “Case x: ” first, where x is the case number. If there exists a feasible schedule to finish all the tasks, print “Yes”, otherwise print “No”.

Print a blank line after each test case.
 

Sample Input
24 31 3 5 1 1 42 3 73 5 92 22 1 31 2 2
 

Sample Output
Case 1: Yes Case 2: Yes
 

Author
allenlowesy
 

Source
2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC
 

Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:  1532 3491 1533 3416 3081 


邻接表dinic没什么好说的,dinic相对于朴素增广路算法就是增加了层次图,每次取消所有同层次点之间的边(为什么是对的呢?因为如果计算完当前层次图的话,如果现在同层次之间的边会成为下次层次图中不同层次的边,然后所有情况都会被计算进去),如果一个点的层次比终点的层次大(怎么办呢?计算完当前层次图之后,如果这个点与终点联通,在不断计算层次图的过程中,迟早会出现只有通过这个点的路还联通终点的情况,这样这个点的层次在这次计算就比终点的层次小了)
#include<cstdio>#include<cstring>#include<queue>using namespace std;#define N 1005const int inf=0x7ffffff;int cnt,n,m,t;int head[N],dist[N];struct node{    int u,v,w,next;}edge[N*N];int min(int x,int y){    return x>y?y:x;}void add(int u,int v,int w){    edge[cnt].u=u;    edge[cnt].v=v;    edge[cnt].w=w;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].u=v;    edge[cnt].v=u;    edge[cnt].w=0;    edge[cnt].next=head[v];    head[v]=cnt++;}int bfs()//寻早新的层次图{    int i,u,v;    queue<int>q;    memset(dist,0,sizeof(dist));    u=0;    dist[u]=1;    q.push(u);    while(!q.empty())    {        u=q.front();        q.pop();        for(i=head[u];i!=-1;i=edge[i].next)        {            v=edge[i].v;            if(edge[i].w&&!dist[v])            {                dist[v]=dist[u]+1;                if(v==t)                    return 1;                q.push(v);            }        }    }    return 0;}int dfs(int s,int lim)//lim记录当前路上的最大流{    int i,tmp,v,cost=0;    if(s==t)        return lim;    for(i=head[s];i!=-1;i=edge[i].next)    {        v=edge[i].v;        if(edge[i].w&&dist[s]==dist[v]-1)//前往下一个层次        {            tmp=dfs(v,min(lim-cost,edge[i].w));            if(tmp>0)            {                edge[i].w-=tmp;                edge[i^1].w+=tmp;                cost+=tmp;                if(cost==lim)                    break;            }            else                dist[v]=-1;        }    }    return cost;}int dinic(){    int ans=0,s=0;    while(bfs())//搜索当前层次图        ans+=dfs(s,inf);//若层次图存在,则将当前层次图所有可增广的路都增广    return ans;}int main(){    freopen("C:\\Users\\Administrator\\Desktop\\input.txt","r",stdin);    int i,j,T,sum,t1,t2,cas=1;    int s[N],e[N],p[N];    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        t1=N;t2=0;        sum=0;        for(i=1;i<=n;i++)        {            scanf("%d%d%d",&p[i],&s[i],&e[i]);            t1=min(t1,s[i]);            t2=max(t2,e[i]);            sum+=p[i];        }        cnt=0;        memset(head,-1,sizeof(head));        for(i=1;i<=n;i++)        {            add(0,i,p[i]);        }        t=n+t2-t1+1+1;        for(i=1;i<=n;i++)        {            for(j=s[i];j<=e[i];j++)            {                add(i,n+j-t1+1,1);            }        }        for(i=t1;i<=t2;i++)            add(n+i-t1+1,t,m);        if(sum==dinic())            printf("Case %d: Yes\n\n",cas++);        else            printf("Case %d: No\n\n",cas++);    }    return 0;}

0 0