hdu 2883(构图+最大流+压缩区间)

kebab

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


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
 
题意:

有不多于200个任务,每个任务要在si到ei这个时间段内完成,每个任务的任务量是ti*ni,只有一台机器,且其单位时间内可完成的任务量为m。现在问能否使所有的任务全部在规定的时间段内完成。

题解:这个题和之前做的一道任务分配的题很相似,但是那道题是将区间拆成一个个点拿出来,这个题点数太多是不行的,所以我们将区间排序,去重之后再加边,先从超级源点向每个人连ni*ti的边,然后每个人能够等待的时间区间连一条容量为INF的边,最后所有的时间区间都向汇点连一条(time[i]-time[i-1])*m的边,做最大流,如果max_flow==sum(ni*ti),那么这个这么多任务就是可以完成的.

#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
const int N = 1005;
const int INF = 999999999;
struct Edge{
    int v,w,next;
}edge[N*N];
int head[N];
int level[N];
int tot;
void init()
{
    memset(head,-1,sizeof(head));
    tot=0;
}
void addEdge(int u,int v,int w,int &k)
{
    edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
    edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des)
{
    queue<int>q;
    memset(level,0,sizeof(level));
    level[src]=1;
    q.push(src);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        if(u==des) return 1;
        for(int k = head[u]; k!=-1; k=edge[k].next)
        {
            int v = edge[k].v;
            int w = edge[k].w;
            if(level[v]==0&&w!=0)
            {
                level[v]=level[u]+1;
                q.push(v);
            }
        }
    }
    return -1;
}
int dfs(int u,int des,int increaseRoad){
    if(u==des||increaseRoad==0) return increaseRoad;
    int ret=0;
    for(int k=head[u];k!=-1;k=edge[k].next){
        int v = edge[k].v,w=edge[k].w;
        if(level[v]==level[u]+1&&w!=0){
            int MIN = min(increaseRoad-ret,w);
            w = dfs(v,des,MIN);
            if(w > 0)
            {
                edge[k].w -=w;
                edge[k^1].w+=w;
                ret+=w;
                if(ret==increaseRoad) return ret;
            }
            else level[v] = -1;
            if(increaseRoad==0) break;
        }
    }
    if(ret==0) level[u]=-1;
    return ret;
}
int Dinic(int src,int des)
{
    int ans = 0;
    while(BFS(src,des)!=-1) ans+=dfs(src,des,INF);
    return ans;
}
int n,m;
int s[N],num[N],e[N],t[N];
int time[N];
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        int k = 0;
        int sum = 0;
        for(int i=1;i<=n;i++){
            scanf("%d%d%d%d",&s[i],&num[i],&e[i],&t[i]);
            sum += num[i]*t[i];
            time[++k] = s[i];
            time[++k] = e[i];
        }
        sort(time+1,time+k+1);
        int cnt = 1;
        for(int i=2;i<=k;i++){
            if(time[i]==time[i-1]) continue;
            time[++cnt] = time[i];
        }
        int src = 0,des = n+cnt+1;
        for(int i=1;i<=n;i++){
            addEdge(src,i,num[i]*t[i],tot);
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=cnt;j++){
                if(s[i]<=time[j-1]&&time[j]<=e[i]){
                    addEdge(i,n+j,INF,tot);
                }
            }
        }
        for(int i=1;i<=cnt;i++){
            addEdge(n+i,des,(time[i]-time[i-1])*m,tot);
        }
        if(Dinic(src,des)==sum) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5720689.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值