HDU 2883 kebab 离散化 + 最大流 判断满流

 

                                                kebab

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


 

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

 

 

因为t的范围太大,所以无法将单独一个单位时间当作一个点,所以将时间的起点和终点离散化,然后将时间间隔块当作点,然后建图。注意Just at exactly time ei is also OK,这句话的意思之还是要在ei之前烤好,不然无法解释样例,然后剩下的和hdu3572差不多。

 

 

 

 

//Max_flow
//@2018/05/04 Friday
//SAP  O(n^2 * m)  O(m*3*2)
//by Tawn
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <map>

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 100;
const int maxm = 1000000 + 10;
typedef pair<int,int> P;

int head[maxn];//链式前向星
int tot = 0,num;
int n,m;

struct edge
{
  int to;
  int c;
  int next;
  edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), next(z) {}
 }es[maxm*2];//记录边 注意是2倍

void add_edge(int u, int v, int c)
{
    es[tot] = edge(v,c,head[u]);
    head[u] = tot++;
}


int SAP(int s, int t)
{
    int numh[maxn],h[maxn],ce[maxn],pre[maxn];
    //numh 记录gap优化的统计高度数量数组,h 距离标号数组,ce 当前弧,pre前驱数组
    int f, ans = 0, u, temp, neck, i; //初始化最大流为0
    memset(h,0,sizeof(h));
    memset(numh,0,sizeof(numh));
    memset(pre,-1,sizeof(pre));
    for(i = 0; i <= num; i++)  ce[i] = head[i];
    numh[0] = num;
    u = s;
    while(h[s] < num)
    {
        //寻找增广路
        if(u == t)
        {
            f = INF;
            for(i = s; i != t; i = es[ce[i]].to)
            {
                if(f > es[ce[i]].c)
                {
                    neck = i;
                    f = es[ce[i]].c;
                }
            }
            for(i = s; i != t; i = es[ce[i]].to)
            {
                temp = ce[i];
                es[temp].c -= f;
                es[temp^1].c += f;
            }
            ans += f;
            u = neck;
        }

        //寻找可行弧
        for(i = ce[u]; i != -1; i = es[i].next)
            if(es[i].c && h[u] == h[es[i].to] + 1)  break;

       //寻找增广路
        if(i != -1)
        {
            ce[u] = i;
            pre[es[i].to] = u;
            u = es[i].to;
        }
        else
        {
            if(!--numh[h[u]]) break; //gap optimization
            ce[u] = head[u];
            for(temp = num, i = head[u]; i != -1; i = es[i].next)
                if(es[i].c)  temp = min(temp, h[es[i].to]);

            h[u] = temp + 1;
            ++numh[h[u]];
            if(u != s) u = pre[u];//重标号并且从当前点前驱重新增广
        }

    }
    return ans;
}


int a[maxn][4];

int main()
{
  while(~scanf("%d%d",&n,&m))
  {
    memset(head,-1,sizeof(head));
    int s = 0,t = 1,res = 0;
    num = 2;
    //int si,ni,ei,ti;
    vector<int>p;
    for(int i = 0; i < n; i++)
     {
       for(int j = 0; j < 4; j++)
         scanf("%d",&a[i][j]);
       res += a[i][1]*a[i][3];
       add_edge(s,i+2,a[i][1]*a[i][3]);
       add_edge(i+2,s,0);
        p.push_back(a[i][0]);
        p.push_back(a[i][2]);
     }
    unique(p.begin(),p.end());
    sort(p.begin(),p.end());
    for(int i = 0; i < p.size()-1; i++)
     {
       add_edge(i+n+2,t,(p[i+1]-p[i])*m);
       add_edge(t,i+n+2,0);
     }
    for(int i = 0; i < p.size()-1 ; i++)
      {
        for(int j = 0; j < n; j++)
          {
            if(a[j][0] <= p[i] && a[j][2] >= p[i+1])
              {
                add_edge(j+2,i+n+2,INF);
                add_edge(i+n+2,j+2,0);
              }
          }
      }
    num = 2+n+p.size();
    //cout << num << endl;
    int ans = SAP(s,t);
    //cout << ans << " " << res << endl;
    if(ans == res) printf("Yes\n");
    else printf("No\n");
  }
  return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值