"The main topic"
A project needs to be completed in n days, with the first day I need an AI person at least. A total of M-class persons can be recruited, of which class I can be from Si days to ti days, the recruitment cost per person is CI yuan. Minimum recruitment costs.
Ideas
Byvoid God Ben Map detailed, to understand the network flow has a good help, the following reference again, the original PO please poke: ★
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 .
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <vector>6#include <queue>7 #defineS 08 #defineT n+29 using namespacestd;Ten structnode One { A intTo,pos,cap,val; - }; - Const intmaxm=10000+ -; the Const intmaxn= ++ -; - Const intinf=0x7fffffff; - intN,M,A[MAXN],S[MAXM],T[MAXM],C[MAXM]; - intPRE[MAXN],PREEDGE[MAXN]; +Vector<node>E[MAXN]; - + voidAddedge (intUintVintCaintva) A { at e[u].push_back (node) {v,e[v].size (), Ca,va}); -E[v].push_back (node) {u,e[u].size ()-1,0,-va}); - } - - intSPFA () - { inqueue<int>que; - intVIS[MAXN],DIS[MAXN]; tomemset (Vis,0,sizeof(Vis)); +memset (pre,-1,sizeof(pre)); - for(inti=s;i<=t;i++) dis[i]=INF; theQue.push (0); *vis[0]=1; $dis[0]=0;Panax Notoginseng while(!que.empty ()) - { the intHead=Que.front (); Que.pop (); +vis[head]=0; A for(intI=0; I<e[head].size (); i++) the { +Node &tmp=E[head][i]; - if(tmp.cap>0&& dis[tmp.to]>dis[head]+tmp.val) $ { $dis[tmp.to]=dis[head]+Tmp.val; -pre[tmp.to]=head; -preedge[tmp.to]=i; the if(!vis[tmp.to]) - {Wuyi Que.push (tmp.to); thevis[tmp.to]=0; - } Wu } - } About } $ if(Dis[t]==inf)return 0;Else return 1; - } - - intMCF () A { + intflow=0; the intans=0; - while(SPFA ()) $ { the intf=INF; the for(inti=t;pre[i]!=-1; i=Pre[i]) the { theNode &tmp=E[pre[i]][preedge[i]]; -f=min (f,tmp.cap); in } the for(inti=t;pre[i]!=-1; i=Pre[i]) the { AboutNode &tmp=E[pre[i]][preedge[i]]; thetmp.cap-=F; thee[tmp.to][tmp.pos].cap+=F; theans+=f*Tmp.val; + } - } the returnans;Bayi } the the voidInit () - { -scanf"%d%d",&n,&m); the for(intI=1; i<=n;i++) scanf ("%d",&a[i]); the for(intI=1; i<=m;i++) scanf ("%d%d%d",&s[i],&t[i],&c[i]); the } the - voidbuild () the { thea[0]=a[n+1]=0; the for(intI=1; i<=n+1; i++)94 { the intc=a[i]-a[i-1]; the if(c>0) Addedge (S,i,c,0); the if(c<0) Addedge (I,t,-c,0);98 } About for(intI=1; i<=m;i++) -Addedge (s[i],t[i]+1, Inf,c[i]);101 for(intI=1; i<=n;i++) Addedge (i+1, I,inf,0);102 }103 104 intMain () the {106 init ();107 build ();108COUT<<MCF () <<Endl;109 return 0; the}
"Fee Flow" bzoj1061[noi2008]-Volunteer recruitment