HDU 2883 kebab (最大流)

kebab

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


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
 

 

Recommend
gaojie

 

 

最大流的题目。

题意比较难懂的。

出题人解题报告:

将所有的到达时间和结束时间按升序排序,得到 x <= 2n-1 个时间区间。建立网络流模型:s为源,t为汇,每个顾客i作为一个结点并连边(s, i, ni*ti),每个区间j作为一个结点并连边(j, t, (ej-sj)*M),其中sj, ej分别表示区间j的起始时间和终止时间。对任意顾客i和区间j,若 [sj, ej] 完全包含在 [si, ei] 之中,则连边(i, j, INF)。若最大流等于 ∑ni*ti 则是 Yes,否则是 No。



注意,这个题目的e[i]是不能算上去的

//============================================================================

// Name        : HDU.cpp

// Author      : 

// Version     :

// Copyright   : Your copyright notice

// Description : Hello World in C++, Ansi-style

//============================================================================



#include <iostream>

#include <string.h>

#include <algorithm>

#include <stdio.h>

using namespace std;



const int MAXN=1010;

const int MAXM=200010;

const int INF=0x3f3f3f3f;

struct Node

{

    int to,next,cap;

}edge[MAXM];

int tol;

int head[MAXN];

int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];

void init()

{

    tol=0;

    memset(head,-1,sizeof(head));

}

void addedge(int u,int v,int w,int rw=0)

{

    edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];head[u]=tol++;

    edge[tol].to=u;edge[tol].cap=rw;edge[tol].next=head[v];head[v]=tol++;

}

int sap(int start,int end,int nodenum)

{

    memset(dis,0,sizeof(dis));

    memset(gap,0,sizeof(gap));

    memcpy(cur,head,sizeof(head));

    int u=pre[start]=start,maxflow=0,aug=-1;

    gap[0]=nodenum;

    while(dis[start]<nodenum)

    {

        loop:

        for(int  &i=cur[u];i!=-1;i=edge[i].next)

        {

            int v=edge[i].to;

            if(edge[i].cap&&dis[u]==dis[v]+1)

            {

                if(aug==-1||aug>edge[i].cap)

                    aug=edge[i].cap;

                pre[v]=u;

                u=v;

                if(v==end)

                {

                    maxflow+=aug;

                    for(u=pre[u];v!=start;v=u,u=pre[u])

                    {

                        edge[cur[u]].cap-=aug;

                        edge[cur[u]^1].cap+=aug;

                    }

                    aug=-1;

                }

                goto loop;

            }

        }

        int mindis=nodenum;

        for(int i=head[u];i!=-1;i=edge[i].next)

        {

            int v=edge[i].to;

            if(edge[i].cap&&mindis>dis[v])

            {

                cur[u]=i;

                mindis=dis[v];

            }

        }

        if((--gap[dis[u]])==0)break;

        gap[dis[u]=mindis+1]++;

        u=pre[u];

    }

    return maxflow;

}



int n[210],s[210],e[210],t[210];

int a[1000];



int main()

{

    //freopen("in.txt","r",stdin);

    //freopen("out.txt","w",stdout);

    int N,M;

    while(scanf("%d%d",&N,&M)==2)

    {

        init();

        int sum=0;

        int num=0;

        for(int i=1;i<=N;i++)

        {

            scanf("%d%d%d%d",&s[i],&n[i],&e[i],&t[i]);

            sum+=n[i]*t[i];

            addedge(0,i,n[i]*t[i]);

            a[num++]=s[i];

            a[num++]=e[i];

        }

        sort(a,a+num);

        num=unique(a,a+num)-a;

        int start=0,end=N+num,nodenum=end+1;

        for(int i=0;i<num-1;i++)

        {

            addedge(N+1+i,end,(a[i+1]-a[i])*M);

            for(int j=1;j<=N;j++)

                if(s[j]<=a[i]&&e[j]>=a[i+1])

                    addedge(j,N+1+i,INF);

        }

        if(sap(start,end,nodenum)==sum)printf("Yes\n");

        else printf("No\n");

    }

    return 0;

}

 

 

 

你可能感兴趣的:(HDU)