kebab
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 982 Accepted Submission(s): 414
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分析:此题题意比较难懂,而且对于没做过网络满流的人,建图比较难,由于数据量比较大造成的时间问题,还要进行如离散化这一类的优化。建图及优化如下:首先建立源点和汇点,使每个顾客i与源点连接,权值为ni*ti;然后将所有的起始时间si和结束时间ei进行升序排序得到x<=2*n-1个【sj,ej】区间,并进行编号,如果每位顾客i的时间范围【si,ei】包含这x个区间中的某一段【sj,ej】,则使i顾客与【sj,ej】建立连接,权值为INF;最后每个【sj,ej】与汇点连接权值为、(ej-sj)*m,然后求网络最大流和sum(n个(ni*ti)之和)比较。至此此题可解决。代码示例:#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<queue> #define Lh 200000 #define Le 200000 #define Lg 200000 #define max 1000000000 using namespace std; typedef struct { int to; int w; int next; }node; typedef struct { int si,ei,ni,ti; }nodf; typedef struct { int star,end; }nodg; typedef struct { int x; int t; }DEP; node E[Le]; nodf e[Le]; nodg g[Lg]; DEP fir,nex; int head[Lh],headx[Lh],deep[Lh],cnt; int len[Lh]; void ADD(int a,int b,int c) { E[++cnt].to=b; E[cnt].w=c; E[cnt].next=head[a]; head[a]=cnt; E[++cnt].to=a; E[cnt].w=0; E[cnt].next=head[b]; head[b]=cnt; } int min(int x,int y) { return x<y?x:y; } int bfs(int s,int t,int n) { memset(deep,255,sizeof(deep)); queue<DEP>Q; fir.x=s; fir.t=0; deep[s]=0; Q.push(fir); while(!Q.empty()) { fir=Q.front(); Q.pop(); for(int i=head[fir.x];i;i=E[i].next) { nex.x=E[i].to; nex.t=fir.t+1; if(deep[nex.x]!=-1||!E[i].w) continue; deep[nex.x]=nex.t; Q.push(nex); } } for(int i=0;i<=n;i++) headx[i]=head[i]; return deep[t]!=-1; } int dfs(int s,int t,int flow) { if(s==t) return flow; int newflow=0; for(int i=headx[s];i;i=E[i].next) { headx[s]=i; int to=E[i].to; int w=E[i].w; if(!w||deep[to]!=deep[s]+1) continue; int temp=dfs(to,t,min(w,flow-newflow)); newflow+=temp; E[i].w-=temp; E[i^1].w+=temp; if(newflow==flow) break; } if(!newflow)deep[s]=0; return newflow; } int Dinic(int s,int t,int m) { int sum=0; while(bfs(s,t,m)) { sum+=dfs(s,t,max); } return sum; } int main() { int n,m,s,t,num,sum; int si,ei,ni,ti; while(~scanf("%d%d",&n,&m)) { memset(head,0,sizeof(head)); cnt=1,num=0,sum=0; int h=1; for(int i=1;i<=n;i++) { scanf("%d%d%d%d",&si,&ni,&ei,&ti); e[i].si=si,e[i].ni=ni,e[i].ei=ei,e[i].ti=ti; len[h++]=si,len[h++]=ei; sum+=ni*ti; } s=1,t=2; for(int i=1;i<=n;i++) { ADD(s,i+t,e[i].ni*e[i].ti); num++; } sort(len,len+h); for(int i=1;i<h;i++) { g[i].star=len[i-1]; g[i].end=len[i]; } for(int i=1;i<h;i++) { ADD(t+n+i,t,(g[i].end-g[i].star)*m); num++; } for(int i=1;i<h;i++) { for(int j=1;j<=n;j++) { if(e[j].si<=g[i].star&&e[j].ei>=g[i].end) { ADD(t+j,t+n+i,max); num++; } } } int w=Dinic(s,t,num); if(w==sum) { printf("Yes\n"); } else { printf("No\n"); } } return 0; }