Hdoj 2883 Kebab "time interval discretization + maximum Flow"

Source: Internet
Author: User



KebabTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1273 Accepted Submission (s): 532


Problem descriptionalmost Everyone likes kebabs nowadays (here a kebab means pieces of meat grilled on a long thin stick). However, considered about the hardship of a kebab roaster while enjoying the delicious food? Well, here's a chance for your help of the poor roaster make sure whether he can deal with the following orders without dis Satisfying the customers.

Now N customers is coming. Customer I'll arrive at time Si (which means the roaster cannot serve customer I until time Si). He/she would order NI kebabs, each one of the 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 have a big grill which can hold an unlimited amount of kebabs (unbelievable huh? Trust me, it ' s real!). But he had so little charcoal, at a most M kebabs can is roasted at the same time. He is skillful enough to take no time changing the kebabs being roasted. Can him determine if he can meet all the customers ' demand?

Oh, I forgot to say so 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 to K (1<=k<=ti) parts such that any of the adjacent parts don ' t have To is 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 was linear to the amount of meat it contains. So if a kebab needs unit time to roast well, he can divide it into the parts and roast them simultaneously just one unit Time. Remember, however, a single unit time was indivisible and the kebab can only being divided into such parts so each needs an Integral unit time to roast well.

Inputthere is multiple test cases. The first line of all case contains, positive integers n and M. n is the number of customers and M are the maximum Keba BS 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 <=, 1 <= M <= 1,000
1 <= ni, ti <= 50
1 <= si < ei <= 1,000,000

Outputif the roaster can satisfy all the customers, output "Yes" (without quotes). Otherwise, Output "No".
Sample Input
2 101 10 6 32 10 4 22 101 10 5 32 10 4 2

Sample Output
YesNo



suggest doing hdoj 3572 First


See this problem, know to be discretized, also know how to discretization of the time interval, but do not know how to deal with the building edge. O (╯-╰) o had to Orz God's ox


Test instructions: There are N people to eat barbecue, everyone arrives at the time of Si, leaving time for EI, the number of grilled meat is ni, roast a piece of flesh takes time for ti. Now there is only one barbecue machine, the machine can bake M servings of meat per unit of time. To meet the needs of all customers.


Idea: First use the array rec to record all Si and ei, sorting to remove the weight (discretization), to ensure that the interval does not overlap.

the adjacent time interval is virtual into a point, that is, the interval [rec[i], rec[i+1]] as a point.

It turns each person's time-point processing into a time-interval-building process. Build a good diagram, run the maximum flow to determine whether the full stream can be.


Build: Set Super source point sources, super sink point sink

1,source to everyone I built side, capacity for Ni * TI;

2, adjacent time interval [rec[i], rec[i+1]] to sink, the capacity of (rec[i+1]-rec[i]) * m, the same can be roasted m;

3, determine whether each person's time interval [si, ei] contains the discretization of the adjacent time interval [rec[i], rec[i+1]]. If the inclusion indicates that enough time is available to supply the person with barbecue, the building edge capacity is infinite, otherwise it will not be built.


AC Code:

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 800# Define MAXM 200000+10#define INF 0x3f3f3f3fusing namespace std;struct edge{int from, to, cap, flow, next;};    Edge Edge[maxm];int HEAD[MAXN], edgenum, Cur[maxn];int dist[maxn];bool vis[maxn];int N, m;void init () {edgenum = 0; Memset (Head,-1, sizeof (head));}    void Addedge (int u, int v, int w) {Edge E1 = {u, V, W, 0, Head[u]};    Edge[edgenum] = E1;    Head[u] = edgenum++;    Edge E2 = {V, u, 0, 0, Head[v]};    Edge[edgenum] = E2; HEAD[V] = edgenum++;} int sumflow;//Record source points incoming total traffic int source, sink;//super-source point super sinks struct node{int s, n, E, t;};    Node num[210];int rec[500];void Getmap () {Source = 0;    int len = 1;    Sumflow = 0;        for (int i = 1; I <= N; i++) {scanf ("%d%d%d%d", &num[i].s, &AMP;NUM[I].N, &AMP;NUM[I].E, &num[i].t);        Addedge (source, I, NUM[I].N * num[i].t);        Sumflow + = NUM[I].N * NUM[I].T;       rec[len++] = NUM[I].S; rec[len++] = NUM[I].E;    } sort (rec+1, rec+len);    de-weight int R = 2; for (int i = 2; i < Len; i++)//scroll array optimization if (rec[i]! = Rec[i-1]) rec[r++] = rec[i];//get R-1 end of sort (rec+    1, Rec+r); R-1 endpoints R-2 Interval sink = n+r-1;//Super sinks for (int i = 1; i < R-1; i++)//R-2 interval processing Addedge (i+n, sink, (rec[i+1]-r    Ec[i]) (M); for (int i = 1; I <= N; i++)//For each person's meal interval processing {for (int j = 1; j < R-1; J + +)//enumeration R-2 interval {i        F (num[i].s <= rec[j] && num[i].e >= rec[j+1])//time accommodates the interval capacity infinitely large addedge (i, J+n, INF);    }}}bool BFS (int s, int t) {queue<int> Q;    memset (Dist,-1, sizeof (Dist));    Memset (Vis, false, sizeof (VIS));    Dist[s] = 0;    Vis[s] = true;    Q.push (s); while (!        Q.empty ()) {int u = q.front ();        Q.pop ();            for (int i = head[u]; i =-1; i = Edge[i].next) {Edge E = Edge[i];      if (!vis[e.to] && e.cap > E.flow) {          Dist[e.to] = Dist[u] + 1;                if (e.to = = t) return true;                Vis[e.to] = true;            Q.push (e.to); }}} return false;}    int DFS (int x, int a, int t) {if (x = = T | | a = = 0) return A;    int flow = 0, F;        for (int &i = cur[x]; i =-1; i = Edge[i].next) {Edge &e = Edge[i]; if (dist[e.to] = = Dist[x] + 1 && (f = DFS (e.to, Min (A, e.cap-e.flow), T)) > 0) {Edge[i].flo            W + = f;            Edge[i^1].flow-= f;            Flow + + F;            A-= f;        if (a = = 0) break; }} return flow;}    int Maxflow (int s, int t) {int flow = 0;        while (BFS (s, t)) {memcpy (cur, head, sizeof (head));    Flow + = DFS (s, INF, T); } return flow;}        int main () {while (scanf ("%d%d", &n, &m)! = EOF) {init ();        Getmap ();        if (Maxflow (source, sink) = = Sumflow)//Full stream printf ("yes\n");  else printf ("no\n");  } return 0;} 


KebabTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1273 Accepted Submission (s): 532


problem DescriptionAlmost everyone likes kebabs nowadays (here a kebab means pieces of meat grilled on a long thin stick). However, considered about the hardship of a kebab roaster while enjoying the delicious food? Well, here's a chance for your help of the poor roaster make sure whether he can deal with the following orders without dis Satisfying the customers.

Now N customers is coming. Customer I'll arrive at time Si (which means the roaster cannot serve customer I until time Si). He/she would order NI kebabs, each one of the 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 have a big grill which can hold an unlimited amount of kebabs (unbelievable huh? Trust me, it ' s real!). But he had so little charcoal, at a most M kebabs can is roasted at the same time. He is skillful enough to take no time changing the kebabs being roasted. Can him determine if he can meet all the customers ' demand?

Oh, I forgot to say so 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 to K (1<=k<=ti) parts such that any of the adjacent parts don ' t have To is 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 was linear to the amount of meat it contains. So if a kebab needs unit time to roast well, he can divide it into the parts and roast them simultaneously just one unit Time. Remember, however, a single unit time was indivisible and the kebab can only being divided into such parts so each needs an Integral unit time to roast well.

InputThere is multiple test cases. The first line of all case contains, positive integers n and M. n is the number of customers and M are the maximum Keba BS 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 <=, 1 <= M <= 1,000
1 <= ni, ti <= 50
1 <= si < ei <= 1,000,000

OutputIf The roaster can satisfy all the customers, output "Yes" (without quotes). Otherwise, Output "No".
Sample Input
2 101 10 6 32 10 4 22 101 10 5 32 10 4 2

Sample Output
YesNo

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 2883 Kebab "time interval discretization + maximum Flow"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.