kebab
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1612 Accepted Submission(s): 679
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
思路:这题和机器分配任务是一样的,只不过时间区间变大了,有10W,那么肯定不能连所有的单位时间了,所以我们考虑把开始时间和结束时间存下来然后从小到大排序,去重,然后枚举所有的时间,判断当前时间与前面一个时间组成的区间有没有在1~n的某个区间里面,有就连边,容量为INF。想想易知不管两个时间点是开始时间还是结束时间结果都是对的(只要在某个顾客的时间区间内,那么都要加,最后所有时间点全部扫完一定可以把顾客的整段区间加进去)
最后判断满流即可~
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1050
#define INF 99999999
struct Edge
{
int v,next,cap;
} edge[N*N];
int cnt,head[N],d[N];
int time[N],s[N],e[N];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void addedge(int u,int v,int cap)
{
edge[cnt].v=v,edge[cnt].cap=cap;
edge[cnt].next=head[u],head[u]=cnt++;
edge[cnt].v=u,edge[cnt].cap=0;
edge[cnt].next=head[v],head[v]=cnt++;
}
int bfs(int s,int t)
{
memset(d,-1,sizeof(d));
d[s]=0;
queue<int>q;
q.push(s);
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,cap=edge[i].cap;
if(d[v]==-1&&cap>0)
{
d[v]=d[u]+1;
q.push(v);
}
}
}
return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
if(s==t||f==0) return f;
int flow=0;
for(int i=head[s]; i!=-1; i=edge[i].next)
{
int v=edge[i].v,cap=edge[i].cap;
if(d[v]==d[s]+1&&cap>0)
{
int x=min(f-flow,cap);
x=dfs(v,t,x);
flow+=x;
edge[i].cap-=x;
edge[i^1].cap+=x;
}
}
if(!flow) d[s]=-2;
return flow;
}
int Dinic(int s,int t)
{
int flow=0,f;
while(bfs(s,t))
{
while(f=dfs(s,t,INF))
flow+=f;
}
return flow;
}
int main()
{
int T,n,m,num,t;
while(~scanf("%d %d",&n,&m))
{
init();
int ss=0,sum=0,tot=0,cou=0;
for(int i=1; i<=n; i++)
{
scanf("%d %d %d %d",&s[i],&num,&e[i],&t);
sum+=num*t;
addedge(ss,i,num*t);
time[tot++]=s[i];
time[tot++]=e[i];
}
sort(time,time+tot);
for(int i=1; i<tot; i++)
if(time[i]!=time[i-1])
time[++cou]=time[i];
int tt=n+cou+1;
for(int i=1; i<=cou; i++)
{
addedge(n+i,tt,m*(time[i]-time[i-1]));
for(int j=1; j<=n; j++)
{
if(s[j]<=time[i-1]&&e[j]>=time[i])
addedge(j,n+i,INF);
}
}
if(Dinic(ss,tt)==sum) printf("Yes\n");
else printf("No\n");
}
return 0;
}