hdu 2883 kebab【最大流Dinic+缩点】

来源:互联网 发布:centos 7 smtp 编辑:程序博客网 时间:2024/05/17 22:58

kebab

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1623    Accepted Submission(s): 688

Problem Description

Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on a long thin stick). Have you, however, considered about the hardship of a kebab roaster while enjoying the delicious food? Well, here's a chance for you to help the poor roaster make sure whether he can deal with the following orders without dissatisfying the customers.

Now N customers is coming. Customer i will arrive at time si (which means the roaster cannot serve customer i until time si). He/She will order ni kebabs, each one of which requires a total amount of ti unit time to get it well-roasted, and want to get them before time ei(Just at exactly time ei is also OK). The roaster has a big grill which can hold an unlimited amount of kebabs (Unbelievable huh? Trust me, it’s real!). But he has so little charcoal that at most M kebabs can be roasted at the same time. He is skillful enough to take no time changing the kebabs being roasted. Can you help him determine if he can meet all the customers’ demand?

Oh, I forgot to say that the roaster needs not to roast a single kebab in a successive period of time. That means he can divide the whole ti unit time into k (1<=k<=ti) parts such that any two adjacent parts don’t have to be successive in time. He can also divide a single kebab into k (1<=k<=ti) parts and roast them simultaneously. The time needed to roast one part of the kebab well is linear to the amount of meat it contains. So if a kebab needs 10 unit time to roast well, he can divide it into 10 parts and roast them simultaneously just one unit time. Remember, however, a single unit time is indivisible and the kebab can only be divided into such parts that each needs an integral unit time to roast well.

 

 

Input

There are multiple test cases. The first line of each case contains two positive integers N and M. N is the number of customers and M is the maximum kebabs the grill can roast at the same time. Then follow N lines each describing one customer, containing four integers: si (arrival time), ni (demand for kebabs), ei (deadline) and ti (time needed for roasting one kebab well). 

There is a blank line after each input block.

Restriction:
1 <= N <= 200, 1 <= M <= 1,000
1 <= ni, ti <= 50
1 <= si < ei <= 1,000,000

 

 

Output

If the roaster can satisfy all the customers, output “Yes” (without quotes). Otherwise, output “No”.

 

 

Sample Input

2 10

1 10 6 3

2 10 4 2

 

2 10

1 10 5 3

2 10 4 2

 

 

Sample Output

Yes

No

 

 

Source

2009 Multi-University Training Contest 9 - Host by HIT

题目大意:

有n个顾客,有一台烧烤机器,烧烤机器每一个单位时间可以处理m个需求。

然后对于n个顾客的输入有四个元素,分别表示到这儿的时间si,需求量a,截止时间ti,以及需要多少份东西b,那么其实一共的需求量就是:a*b;

问能否满足所有顾客的需求。


思路:


1、首先设定每一单位时间为一个节点,那么我们能够初建模型:

①建立源点,连入各个顾客,其权值为a*b,表示每个顾客需要a*b那么多东西、

②建立汇点,将每一单位时间连入汇点,其权值为m,表示每一单位时间可以搞定m个需求。

③将顾客能够等待的时间区间从顾客连入时间点,其权值为INF。


2、初建模型还是比较好理解的,但是观察到数据范围比较大,时间节点过分的多,O(n^2*m)显然是会超时的,然后我们还注意到一共最多有200个顾客,那么我们可以将每个顾客的时间区间作为一个点,作为缩点优化。


3、那么我们记录每一位顾客能够等待的时间区间,然后将其sort一下,比如:1 6 、1 7

sort:1  1 6 7


显然(1.1)是没有意义的,直接去掉,剩下三个数:1 6 7,我们将其分成两个区间,也就是分成了两个点:(1,6),(1,7);


4、得到区间点之后,将初建模型的第三条修改为:

③将顾客能够等待的时间区间连入时间区间点,其权值为INF。


5、优化算法之后,直接跑最大流Dinic,得到最终解。判断一下最大流是否等于所有顾客的需求量之和即可。



Ac代码:


#include<stdio.h>#include<algorithm>#include<queue>#include<string.h>using namespace std;#define INF 0x3f3f3f3fstruct node2{    int from;    int to;    int next;    int w;}e[1551515];struct node{    int si,ti,c;}a[205];int cont;int divv[151515];int cur[151515];int time[50000];int head[151515];int dian[151515][3];int n,m,contzz,ss,tt,sum;int cmp(node a,node b){    if(a.si==b.si)    {        return a.ti>b.ti;    }    else return a.si<b.si;}void add(int from,int to,int w){    e[cont].from=from;    e[cont].to=to;    e[cont].w=w;    e[cont].next=head[from];    head[from]=cont++;}void getmap(){    int contz=1;    for(int i=1;i<=n;i++)    {        time[contz++]=a[i].si;        time[contz++]=a[i].ti;    }    sort(time,time+contz);    contzz=1;    time[1]=time[1];    for(int i=2;i<=contz;i++)    {        if(time[i]==time[i-1])continue;        time[++contzz]=time[i];    }    ss=contzz+n+1;    tt=ss+1;    cont=0;    memset(head,-1,sizeof(head));    for(int i=1;i<=n;i++)    {        add(ss,i,a[i].c);        add(i,ss,0);    }    for(int i=2;i<contzz;i++)    {        int tmpp=(time[i]-time[i-1])*m;        add(n+i-1,tt,tmpp);        add(tt,n+i-1,0);    }    for(int i=1;i<=n;i++)    {        for(int j=2;j<contzz;j++)        {            if(a[i].si<=time[j-1]&&a[i].ti>=time[j])            {                add(i,n+j-1,INF);                add(n+j-1,i,0);            }        }    }}int makedivv(){    queue<int>s;    memset(divv,0,sizeof(divv));    divv[ss]=1;    s.push(ss);    while(!s.empty())    {        int u=s.front();        if(u==tt)return 1;        s.pop();        for(int i=head[u];i!=-1;i=e[i].next)        {            int v=e[i].to;            int w=e[i].w;            if(w&&divv[v]==0)            {                divv[v]=divv[u]+1;                s.push(v);            }        }    }    return 0;}int Dfs(int u,int maxflow,int tt){    if(u==tt)return maxflow;    int ret=0;    for(int &i=cur[u];i!=-1;i=e[i].next)    {        int v=e[i].to;        int w=e[i].w;        if(w&&divv[v]==divv[u]+1)        {            int f=Dfs(v,min(maxflow-ret,w),tt);            e[i].w-=f;            e[i^1].w+=f;            ret+=f;            if(ret==maxflow)return ret;        }    }    return ret;}void Dinic(){    int ans=0;    while(makedivv()==1)    {        memcpy(cur,head,sizeof(head));        ans+=Dfs(ss,INF,tt);    }    //printf("%d\n",ans);    if(ans==sum)printf("Yes\n");    else printf("No\n");}int main(){    while(~scanf("%d%d",&n,&m))    {        sum=0;        for(int i=1;i<=n;i++)        {            int x,bb,y,cc;            scanf("%d%d%d%d",&x,&bb,&y,&cc);            a[i].si=x;            a[i].ti=y;            a[i].c=bb*cc;            sum+=a[i].c;        }        getmap();        Dinic();    }}





0 0