2014
02-17

# kebab

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.

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 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

2 10
1 10 6 3
2 10 4 2

2 10
1 10 5 3
2 10 4 2

Yes
No

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#define inf 2139062143
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
const int maxn=1000+10;
const int N=1000000+10;
struct Edge
{
int from,to,cap,flow;
};
struct Query
{
int S,ni,E,ti;
};
struct Dinic
{
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn],cur[maxn];
int n,m,s,t;
void Init(int n)
{
for(int i=0;i<=n;++i) G[i].clear();
edges.clear();
}
{
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
queue<int>q;
memset(vis,0,sizeof(vis));
vis[s]=true;
d[s]=0;
q.push(s);
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=0;i<G[x].size();++i)
{
Edge e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
d[e.to]=d[x]+1;
vis[e.to]=true;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
if(x==t||a==0) return a;
int flow=0,f;
for(int & i=cur[x];i<G[x].size();++i)
{
Edge & e=edges[G[x][i]];
if(d[e.to]==d[x]+1&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0)
{
flow+=f;
edges[G[x][i]].flow+=f;
edges[G[x][i]^1].flow-=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
this->s=s;this->t=t;
int flow=0;
while(BFS())
{
memset(cur,0,sizeof(cur));
flow+=DFS(s,inf);
}
return flow;
}
}dinic;
Query query[300];
int convert[N],dd[500],val[maxn];
int getCon(int sz)
{
sort(dd+1,dd+sz);
int u=1;
int i=1,j=1;
for(i=2;i<sz;++i)
{
if(dd[i]==dd[j]) continue;
dd[++j]=dd[i];
}
for(i=1;i<j;++i)
{
convert[dd[i]]=u;
val[u]=dd[i+1]-dd[i];
u++;
}
convert[dd[i]]=u;
val[u]=0;
return u;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
while(~scanf("%d%d",&n,&m))
{
int sum=0;
int sz=1;
for(int i=1;i<=n;++i)
{
scanf("%d%d%d%d",&query[i].S,&query[i].ni,&query[i].E,&query[i].ti);
sum+=query[i].ni*query[i].ti;
dd[sz++]=query[i].S;
dd[sz++]=query[i].E;
}
sz=getCon(sz);
int N=n+sz+1;
dinic.Init(N);
for(int i=1;i<=n;++i)
{
int t=convert[query[i].S];
while(t!=convert[query[i].E])
{
t++;
}
}
for(int i=1;i<sz;++i)
}