"Bzoj" 1061: [Noi2008] Volunteer recruitment

Source: Internet
Author: User

1061: [Noi2008] Volunteers recruitment time limit:20 Sec Memory limit:162 MB
submit:3752 solved:2298
[Submit] [Status] [Discuss] Description

After the successful bid, Bubu after unremitting efforts, and finally become the head of the organization's human resources department. Bubu has just come into a dilemma: recruiting short-term volunteers for the upcoming new Olympic project. It is estimated that the project will take n days to complete, with the first day I need an AI person at least. Bubu learned by understanding that there are altogether m-type volunteers can recruit. In which class I can work from Si days to ti days, the recruitment fee is per person ci yuan. In order to do his job well, Bubu hopes to recruit enough volunteers with as little money as possible, but this is not his specialty! So Bubu found you, I hope you help him design an optimal recruitment program. The first line of Input contains two integers n, M, indicating the number of days to complete the project and the types of volunteers that can be recruited. The next line contains n non-negative integers, representing at least the number of volunteers needed per day. The next M-line contains three integers of Si, Ti, Ci, meaning as described above. For the sake of convenience, we can assume that the number of each type of volunteer is infinitely large. Output

Contains only an integer representing the total cost of the optimal scheme you have designed.

Sample Input3 3
2 3 4
1 2 2
2 3 5
3 3 2Sample Output -HINT

1≤n≤1000,1≤m≤10000, the other data involved in the topic are not more than 2^31-1.

God problem .... Read the https://www.byvoid.com/zhs/blog/noi-2008-employee/to understand the wave
#include <bits/stdc++.h>#defineRep (i,l,r) for (int i=l;i<=r;++i)using namespacestd;Const intn=102333, inf=214748364;structedge{intTo,next, from, C,w;} e[2000000];inthead[n],tot=1, Ans,dis[n], from[N],x[n],m,n,a,t,b,c;BOOLUsed[n];inlinevoidInsintUintVintWintCost ) {e[++tot].to=v; E[tot].next=head[u]; Head[u]=tot; E[tot].w=w; E[tot].c=cost; E[tot]. from=u;} InlineBOOLSPFA () {Queue<int> q; for(intI=0; i<=t;i++) Dis[i]=inf; dis[0]=0; Q.push (0); used[0]=1;  while(!Q.empty ()) {          intx=Q.front (); Q.pop ();  for(intk=head[x];k;k=e[k].next)if(e[k].w>0&&dis[x]+e[k].c<Dis[e[k].to]) {Dis[e[k].to]=DIS[X]+E[K].C; from[e[k].to]=K; if(!Used[e[k].to]) {Used[e[k].to]=1; Q.push (e[k].to); }} Used[x]=0; }     if(Dis[t]==inf)return 0;Else return 1;} Inlinevoidrun () {intx=inf;  for(intk= from[t];k;k= from[E[k]. from]) x=min (X,E[K].W);  for(intk= from[t];k;k= from[E[k]. from]) {E[K].W-=x; e[k^1].w+=x; ans+=e[k].c*x; }}inlinevoidInsertintUintVintWintc) {ins (u,v,w,c); Ins (V,u,0,-c);}intMain () {scanf ("%d%d",&n,&m); Rep (I,1, N) scanf ("%d",&X[i]); T=n+2; Rep (I,2, n+1) Insert (i,i-1Inf0); Rep (I,1, M) {scanf ("%d%d%d",&a,&b,&c); Insert (A, B+1, Inf,c); } Rep (I,1, n+1) {        if(x[i]-x[i-1]<0) Insert (i,t,x[i-1]-x[i],0);ElseInsert0, i,x[i]-x[i-1],0); }     while(SPFA ()) run (); printf ("%d\n", ans);}
View Code

The correct solution to this problem is to construct the network, the maximum flow of the minimum cost of the network, but the model hidden deeper, not easy to think. The construction of the network is the key to the problem, the following example illustrates the composition of the method and interpretation.

For example, it takes 4 days, and the number of four days is 4,2,5,3. There are 5 categories of volunteers, as shown in the following table:

Kinds 1 2 3 4 5
Time 1-2 1-1 2-3 3-3 3-4
Cost 3 4 3 5 6

The number of persons who employ category I volunteers is x[i], the cost of each volunteer is v[i], the number of persons employed on J Day is p[j], the number of persons employed per day shall satisfy an inequality, as described in the table above, can be listed

P[1] = x[1] + x[2] >= 4

P[2] = x[1] + x[3] >= 2

P[3] = x[3] + x[4] +x[5] >= 5

P[4] = x[5] >= 3

For the I inequality, add auxiliary variable y[i] (y[i]>=0), you can make it into an equation

P[1] = x[1] + x[2]-y[1] = 4

P[2] = x[1] + x[3]-y[2] = 2

P[3] = x[3] + x[4] +x[5]-y[3] = 5

P[4] = x[5]-y[4] = 3

Add p[0]=0,p[5]=0 to the above four equations, subtracting the upper equation from the lower one,

①P[1]-p[0] = x[1] + x[2]-y[1] = 4

②P[2]-p[1] = x[3]-x[2]-y[2] +y[1] = 2

③P[3]-p[2] = X[4] + x[5]-x[1]-y[3] + y[2] =3

④P[4]-p[3] =-x[3]-x[4] + y[3]-y[4] = 2

⑤P[5]-p[4] =-x[5] + y[4] =-3

It is observed that each variable appears in two formulas, one at a time and one negative at a time. All equations to the right and to 0. Next, the composition is based on the five equations above.

    • Each equation is a vertex in the graph, adding the source point S and the meeting point T.
    • If an equation to the right is a non-negative integer c, from the source point s to the corresponding vertex of the equation is connected to a capacity of C, the weighted value of 0 of the forward edge; if a negative integer c is to the right of an equation, the vertex corresponding to the equation is connected to the meeting point T with a positive edge of C and 0.
    • If a variable x[i] appears in the J equation as X[i], in the K-equation appears as-x[i], from the vertex J to the vertex K joins a capacity of ∞, the weight is v[i] the forward edge.
    • If a variable y[i] appears in the J equation as Y[i], in the K-equation appears as-y[i], from the vertex J to the vertex K joins a capacity of ∞, the weighted value of 0 of the forward edge.

After the composition, the minimum cost maximum flow from the source point S to the meeting point T is obtained, and the cost value is the result.

According to the above example can construct the following network, the red side for each variable x represents the edge, the blue edge for each variable y represents the edge, the edge of the capacity and weight of the flag has been marked (blue is not marked, because it is the capacity ∞, the weight of 0).

In this figure, the minimum cost maximum flow, the traffic network, such as each red edge of the traffic is the corresponding variable x value.

So the answer is 43+23+3*6=36.

The above method is a magical way to find out the result and think about why this composition. We will further deform the last five equations to produce the following results

①-X[1]-x[2] + y[1] + 4 = 0

②-X[3] + x[2] + y[2]-y[1]-2 = 0

③-X[4]-x[5] + x[1] + y[3]-y[2] + 3 = 0

④X[3] + x[4]-y[3] + y[4]-2 = 0

⑤X[5]-y[4]-3 = 0

It can be found that the left side of each equation is a number of variables and a constant added minus, the right is 0, just like the network flow in addition to the source point and the vertex of the meeting point to meet the traffic balance . Each positive variable corresponds to the flow that flows into the vertex, and the negative variable is the amount of traffic that flows out of the vertex, while the normal number can be considered as traffic from the attached source point, and the negative constant is the flow to the additional sink point. Therefore, the network can be constructed accordingly, and the maximum network flow from the additional source to the additional sink is satisfied. We also ask for the minimum, so we need to add weights to the corresponding edges of the x variable, and then we want the minimum cost maximum flow .

"Bzoj" 1061: [Noi2008] Volunteer recruitment

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.