Task Schedule

来源:互联网 发布:vb下载后如何安装 编辑:程序博客网 时间:2024/04/28 01:39

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

 

交了10遍,一直TLE,然后改成了邻接表建边dinic,然后又进行了优化,A了。。。

这个题意不难,将源点和任务相连,容量是需要的天数,然后将任务需要在允许的天数内建边,容量为1,最后将所有的天数和汇点建边,容量为m,因为每天最多有m个,因为每个任务至少一天,如果同时工作,所以最多为m,但是最重要的是明白dinic邻接表建边

#include<cstdio>#include<cstring>#include<stdio.h>#include<iostream>#include<string.h>#include<queue>#define oo 1<<28using namespace std;int n,m,start,end,cnt;int dep[1100];int p[1100];struct node{    int u,v,w;    int next;} edge[1100000];void add(int u,int v,int w){    edge[cnt].u=u;    edge[cnt].v=v;    edge[cnt].w=w;    edge[cnt].next=p[u];    p[u]=cnt++;    edge[cnt].u=v;    edge[cnt].v=u;    edge[cnt].w=0;    edge[cnt].next=p[v];    p[v]=cnt++;}int BFS(){    queue<int>q;    memset(dep,-1,sizeof(dep));    dep[start]=0;    while(!q.empty())    {        q.pop();    }    q.push(start);    while(!q.empty())    {        int u=q.front();        q.pop();        for(int i=p[u]; i!=-1; i=edge[i].next)        {            if(edge[i].w>0 && dep[edge[i].v]==-1)// 如果可以到达且还没有访问            {                dep[edge[i].v]=dep[u]+1;                q.push(edge[i].v);            }        }    }    if(dep[end]>0)        return 1;    return 0;}int find(int i,int sum) // 查找路径上的最小的流量{    if(i==end)        return sum;    int tmp;    int t=0;    for(int j=p[i]; j!=-1; j=edge[j].next)    {        int v=edge[j].v;        int w=edge[j].w;        if(w>0 && dep[v]==dep[i]+1 && (tmp=find(v,min(sum,w))))        {            edge[j].w-=tmp;     //正向减少            edge[j^1].w+=tmp;     //反向增加            t+=tmp;            sum-=tmp;            if(!sum)                break;        }    }    if(t)        return t;    dep[i]=-1;    return 0;}int Dinic(){    int ans=0,tmp;    while(BFS())//如果能找到一条增广路    {        tmp=find(start,oo);//先把开始给标号,oo是因为没有节点控制,先初始为最        if(tmp==0)            break;        ans+=tmp;    }    return ans;}int main(){    int t,i,j,k;    scanf("%d",&t);    for(k=1; k<=t; k++)    {        cnt=0;        scanf("%d%d",&n,&m);        memset(p,-1,sizeof(p));        int d,u,v,w;        int sum=0,maxx=0,minn=oo;        for(i=1; i<=n; i++)        {            scanf("%d%d%d",&d,&u,&v);            sum+=d;            if(minn>u)                minn=u;            if(maxx<v)                maxx=v;            add(0,i,d);//将源点和每一个任务相连,权值为每个任务需要的天数            for(j=u; j<=v; j++)            {                add(i,j+n,1);//将每一个任务与他们需要完成得天数区间内相连,权值为1,代表可在此天            }        }        int dd=n+maxx+1;//汇点        for(i=minn+n; i<=maxx+n; i++)        {            add(i,dd,m);//最后将每个天数与汇点相连,权值为m,表示这一天可以有运行m个任务,因为每个任务至少需要一个机器1天,故最多运行m个任务        }        start=0,  end=dd;//开始于0,结束于dd        if(sum==Dinic())            printf("Case %d: Yes\n\n",k);        else            printf("Case %d: No\n\n",k);    }    return 0;}


 

0 0
原创粉丝点击