Noi2008 volunteer recruitment

Source: Internet
Author: User
1061: [noi2008] volunteer recruitment time limit: 20 sec memory limit: 162 MB
Submit: 1859 solved: 1169
[Submit] [Status] Description

After successfully applying for the Olympic Games, bu made unremitting efforts and finally became the supervisor of the HR department of the organizing committee. Bu encountered a difficult problem when he took office: recruiting a group of short-term volunteers for the upcoming New Olympic project. It is estimated that this project will take n days to complete, and the day I will require at least AI individuals. Bu learned that a total of M volunteers can be recruited. Category I can work from the SI day to the Ti day, and the recruitment fee is CI yuan per person. In order to do his job well, bu hopes to recruit enough volunteers at a low cost, but this is not his specialty! So Bu found you and hoped that you could help him design an optimal recruitment solution.

Input

The first line contains two integers, n, m, indicating the number of days of completion of the project and the type of volunteers that can be recruited. The next row contains N non-negative integers, indicating at least the number of volunteers required each day. Each row in the next m row contains three integers, Si, Ti, and CI. The meaning is described above. For convenience, we can consider that the number of volunteers for each type is infinite.

Output

Contains only one integer, indicating the total cost of the optimal solution you have designed.

Sample input3 3
2 3 4
1 2 2
2 3 5
3 3 2
Sample output14
Hint

 

3 first-class volunteers, 4 Third-class volunteers, 30% of the data, 1 ≤ n, m ≤ 10, 1 ≤ AI ≤ 10; 100% of the data, 1 ≤ n ≤ 10000, 1 ≤ m ≤, and other data involved in the question cannot exceed 2 ^ 31-1.

 

Source

Problem: Byvoid

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The correct solution to this question is to construct a network and find the maximum flow with the minimum cost of the network, but the model is hidden and hard to think. Constructing a network is the key to this question. The following example describes the methods and explanations of the diagram.

For example, a total of four days are required, and the numbers required for four days are 4, 2, 5, and 3. There are 5 types of volunteers, as shown in the following table:

Type 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 people who hire class I volunteers is X [I], the cost of each volunteer is V [I], and the number of people hired on day J is P [J], the number of employees per day must meet an inequality, as described in the preceding table.

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-th inequality, add the auxiliary variable Y [I] (Y [I]> = 0) to change it to 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 preceding four equations, and subtract the formula from the formula below to obtain

① 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 and is positive at a time and negative at a time. The right side of all equations is 0. Next, based on the above five equations.

  • Each equation is a vertex in the graph. The Source Vertex s and the sink vertex t are added.
  • If the right side of an equation is a non-negative integer c, a directed edge with a capacity of C and a weight of 0 is connected to the vertex corresponding to the equation from The Source Vertex S. If the right side of an equation is a negative integer c, A directed edge with a capacity of C and a weight of 0 is connected from the vertex t corresponding to the formula.
  • If the X [I] variable appears as X [I] in the J equation, the K equation is-X [I], link a directed edge with a capacity of ∞ and a weight of V [I] from vertex J to vertex K.
  • If the Y [I] variable appears as Y [I] in the J equation, the K equation is-y [I], link a directed edge with a capacity of ∞ and a weight of 0 from vertex J to vertex K.

After the diagram, find the maximum minimum fee flow from the source point S to the sink point T. The cost value is the result.

The following network can be constructed based on the above example. The red edge represents the edge of each variable X, and the blue edge represents the edge of each variable Y, the size and weight of the edge have been marked (blue is not marked, because both are capacity ∞ and weight 0 ).

In this figure, the maximum minimum cost flow is obtained. For example, the traffic on each red edge is the value of the corresponding variable X.

Therefore, the answer is 4.3 + 23 + 3*6 = 36.

The above method is amazing to find the result and think about why the diagram is like this. The last five equations are further transformed 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 on the left side of each equation is a few variables plus or minus a constant, and the right side is 0, just like in the network flow, except that the vertices of the Source and Sink vertices meetTraffic balance. Each positive variable is equivalent to the traffic flowing into the vertex, and the negative variable is equivalent to the traffic flowing out of the vertex, while the normal number can be seen as the traffic from the additional source point, the negative constant is the flow to the additional sink point. Therefore, we can construct a network based on this and obtain the maximum network flow from the supplementary source to the supplementary sink to satisfy all equations. We also require the minimum value, so we need to add the weight value on the edge corresponding to the X variable, and then findMaximum Flow with minimum cost.

I wrote the simple spfa algorithm to find the minimum cost flow algorithm of the zengguang road. All test points can be passed within the question time limit.

At Noi's site, the question scored an average score of 12.56, and only Gao yihan received a full score. It cannot be said that this is a difficult problem. It is difficult to abstract the problematic mathematical model and design effective algorithms. The Informatics competition is moving towards this direction, and mathematical modeling will be a common key step to solve the problem.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Code:

  1 const inf=maxlongint;  2 type node=record  3      from,go,next,v,c:longint;  4      end;  5 var e:array[0..200000] of node;  6     pre,head,q,d,p:array[0..100000] of longint;  7     v:array[0..100000] of boolean;  8     i,j,n,m,s,t,l,r,mincost,tot,x,y,z:longint;  9     function min(x,y:Longint):longint; 10      begin 11      if x<y then exit(x) else exit(y); 12      end; 13  14 procedure ins(x,y,z,w:longint); 15  begin 16  inc(tot); 17  with e[tot] do 18   begin 19   from:=x;go:=y;v:=z;c:=w;next:=head[x];head[x]:=tot; 20   end; 21  end; 22 procedure insert(x,y,z,w:longint); 23  begin 24  ins(x,y,z,w);ins(y,x,0,-w); 25  end; 26 function spfa:boolean; 27  var i,x,y:longint; 28  begin 29  fillchar(v,sizeof(v),false); 30  for i:=s to t do d[i]:=inf; 31  l:=0;r:=1;q[1]:=s;d[s]:=0;v[s]:=true; 32  while l<r do 33   begin 34   inc(l); 35   x:=q[l];v[x]:=false; 36   i:=head[x]; 37   while i<>0 do 38    begin 39    y:=e[i].go; 40    if (e[i].v<>0) and (d[x]+e[i].c<d[y]) then 41     begin 42     d[y]:=d[x]+e[i].c; 43     pre[y]:=i; 44     if not(v[y]) then 45      begin 46      v[y]:=true; 47      inc(r); 48      q[r]:=y; 49      end; 50     end; 51    i:=e[i].next; 52   end; 53  end; 54  exit(d[t]<>inf); 55  end; 56 procedure mcf; 57  var i,tmp:longint; 58  begin 59  mincost:=0; 60  while spfa do 61   begin 62   tmp:=inf; 63   i:=pre[t]; 64   while i<>0 do 65    begin 66    tmp:=min(tmp,e[i].v); 67    i:=pre[e[i].from]; 68    end; 69   inc(mincost,tmp*d[t]); 70   i:=pre[t]; 71   while i<>0 do 72    begin 73    dec(e[i].v,tmp); 74    inc(e[i xor 1].v,tmp); 75    i:=pre[e[i].from]; 76    end; 77   end; 78  end; 79 procedure init; 80  begin 81  tot:=1; 82  readln(n,m); s:=0;t:=n+2; 83  for i:=1 to n do read(p[i]); 84  for i:=1 to n+1 do 85   begin 86   if i>1 then insert(i,i-1,inf,0); 87   if p[i]>=p[i-1] then insert(s,i,p[i]-p[i-1],0) 88   else insert(i,t,p[i-1]-p[i],0); 89   end; 90  for i:=1 to m do 91   begin 92   readln(x,y,z); 93   insert(x,y+1,inf,z); 94   end; 95  end; 96 procedure main; 97  begin 98  mincost:=0; 99  mcf;100  writeln(mincost);101  end;102 begin103  assign(input,‘input.txt‘);assign(output,‘output.txt‘);104  reset(input);rewrite(output);105  init;106  main;107  close(input);close(output);108 end.                          
View code

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.