hdu 2883 kebab(最大流dinic邻接表)

kebab



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   |   We have carefully selected several similar problems for you:   2882  2884  1532  3061  3338 



主要是建图,和3572很像
si,ei<1000000点太多了 我们肯定不能把每个点当成点来计算,故我们把一个个区间当成点来计算,我们要把smin-emax之间的所有区间都包含,所以我们这样来分区间,把所有边的左右点都加入数组,然后将所有点进行排序,然后去掉重复点,这样我们把数组中相邻的两个点构成的区间当成点,进行建图
      1.构造超级源点,与每个顾客建边,容量为ni*ti
      2.遍历所有区间,如果这个区间被一个顾客所要求的时间段完全覆盖,就将顾客与该区间建边,容量为inf
      3.将所有区间与超级汇点建边,容量为m*(ei-si+1)
      4.判断sum==dinic()?

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int inf=0x7ffffff;
int n,m,t;
const int maxn=610;
const int M=999999;
int head[maxn],dist[maxn],edgenum,from,to;
struct node
{
    int v,w,next;
}edge[2*M];
int min(int x,int y)
{
    return x>y?y:x;
}
void add(int u,int v,int w)
{
    edge[edgenum].v=v;
    edge[edgenum].w=w;
    edge[edgenum].next=head[u];
    head[u]=edgenum++;

    edge[edgenum].v=u;
    edge[edgenum].w=0;
    edge[edgenum].next=head[v];
    head[v]=edgenum++;
}
int bfs()
{
    int i,u,v;
    queue<int>q;
    memset(dist,0,sizeof(dist));
    u=from;
    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==to)
                    return 1;
                q.push(v);
            }
        }
    }
    return 0;
}
int dfs(int s,int lim)//lim记录当前路上的最大流
{
    int i,tmp,v,cost=0;
    if(s==to)
        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;
    while(bfs())
        ans+=dfs(from,inf);
    return ans;
}
int main()
{
    int s[222],q[222],e[222],t[222],i,j,time[maxn],cnt;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(head,-1,sizeof(head));
        edgenum=0;
        cnt=1;
        memset(time,0,sizeof(time));
        int sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&s[i],&q[i],&e[i],&t[i]);
            sum+=q[i]*t[i];
            time[cnt++]=s[i];
            time[cnt++]=e[i];
        }
        sort(time+1,time+cnt);
        int c=0;
        for(i=1;i<cnt;i++)
        {
            if(time[c]!=time[i])
                time[++c]=time[i];
        }
        from=n+c+1;
        to=from+1;
        for(i=1;i<=n;i++)
            add(from,i,q[i]*t[i]);
        for(i=1;i<=c;i++)
        {
            add(n+i,to,m*time[i]-m*time[i-1]);
            for(j=1;j<=n;j++)
            {
                if(s[j]<=time[i-1]&&time[i]<=e[j])
                    add(j,n+i,inf);
            }
        }
        if(sum==dinic())
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值