kebab
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1496 Accepted Submission(s): 624
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.
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
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
题意: 有n个人来烤肉店吃烤肉,每个人在si 时刻来ei 时刻离开并且点了ni 份,每份烤肉要烤到ti 个单位时间才算烤熟,烤肉店里可以同时烤m份。问是否有一种计划使得n个人都可以拿到自己的ni 份。
此题目和Task Schedule那题很像,应该说基本没区别,主要就是建图,我们可以先对客人的时间进行排序,把每个客人都和源点相连,每个时间区间也要和汇点相连,假设有cnt个时间点,则有cnt-1个时间区间,如果第i个人的si和ei包含区间j,就把i和n+j连边,容量为无穷大,对于每个时间区间,与汇点连线,容量为时间长度乘m。最后就是求出最大流是否与工作量相等即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#define inf 0x7fffffff
using namespace std;
const int maxn = 600+10;
const int M = 100000+10;
int n,m,from,to;
int d[maxn];
struct node
{
int v,flow;
int next;
}edge[M*2];
int head[maxn],edgenum;
void add(int u,int v,int flow)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow ;
edge[edgenum].next=head[u];
head[u]=edgenum++;
edge[edgenum].v=u ;edge[edgenum].flow=0;
edge[edgenum].next=head[v];
head[v]=edgenum++;
}
int bfs()
{
memset(d,0,sizeof(d));
d[from]=1;
queue<int> Q;
Q.push(from);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=-1 ;i=edge[i].next)
{
int v=edge[i].v;
if (!d[v] && edge[i].flow>0)
{
d[v]=d[u]+1;
Q.push(v);
if (v==to) return 1;
}
}
}
return 0;
}
int dfs(int u,int flow)
{
if(u==to || flow==0) return flow;
int cap=flow;
for(int i=head[u] ;i!=-1 ;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]==d[u]+1 && edge[i].flow>0)
{
int x=dfs(v,min(cap,edge[i].flow));
edge[i].flow -= x;
edge[i^1].flow += x;
cap -= x;
if (cap==0) return flow;
}
}
return flow-cap;
}
int dinic()
{
int sum=0;
while(bfs()) sum += dfs(from,inf);
return sum;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-1,sizeof(head));
edgenum=0;
int s[222],q[222],e[222],t[222];
int time[maxn],cnt=1;
memset(time,0,sizeof(time));
int sum=0;
for(int 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(int i=1 ;i<cnt ;i++)
{
if(time[c] != time[i])
time[++c]=time[i];
}
from=n+c+1;
to=from+1;
for(int i=1 ;i<=n ;i++)
add(from,i,q[i]*t[i]);
for(int i=1 ;i<=c ;i++)
{
add(n+i,to,m*(time[i]-time[i-1]));
for(int 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;
}