Volunteer recruitment

Description

Suppose you will recruit a group of volunteers for a coming event. It is estimated that this event will take N days to complete, and the i(th) day needs at least Ai volunteers. The number of kinds of volunteers is M. The volunteers of i(th) kind can volunteer from the Si day to the Fi day and the recruit fee is Ci. In order to do his job well, you hope to recruit enough volunteers with least money. Please give an optimal plan.

note: i(th) means the ordinal of this number is i

Input

The first line presents two integers: N, M. The second line contains N nonnegative integers presenting the numbers of volunteers each day needs. Each of the other lines contain three integers: Si, Fi, Ci

note: You can recruit each kind of volunteers as many as possible.

Output

Just a number meaning the total money of your optimal plan.

Sample Input

3 3

2 3 4

1 2 2

2 3 5

3 3 2

Sample Output

14

Hint
1<=N<=200, 1<=M<=1000, all data in the question does not exceed 2^31-1

Find it, find it, find it, finally found a great god blog with this question.

The following is what I glued over.

The correct solution to this problem is to construct the network and find the minimum cost and maximum flow of the network, but the model is hidden deeply and it is not easy to think of. Constructing a network is the key to this question. Take the following example to illustrate the method and explanation of the composition.

For example, it takes 4 days in total, and the number of people required for 4 days is 4, 2, 5, and 3. There are 5 types of volunteers, as shown in the following table:

Insert picture description here
Suppose the number of hiring type i volunteers is X[i], the cost of each volunteer is V[i], and the number of hires on day j is P[j], then the number of hires per day should satisfy an inequality, as above As described in the table, you can list

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 an auxiliary variable Y[i] (Y[i]>=0) to make it 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 and P[5]=0 above and below the above four equations, and subtract the above formula from the formula below each time to get

① 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

Observation found that each variable appeared in two formulas, and it was positive once and negative once. The sum on the right side of all equations is 0. Next, compose the picture according to the above five equations.

  • Each equation is a vertex in the graph, adding a source point S and a sink point T.
  • If the right side of an equation is a non-negative integer c, connect a directed edge with capacity c and a weight of 0 from the source point S to the vertex corresponding to the equation; if the right side of an equation is a negative integer c, from the The vertex corresponding to the formula connects a directed edge with capacity c and weight 0 to the sink point T.
  • If a variable X[i] appears as X[i] in the j-th equation, and appears as -X[i] in the k-th equation, a link from vertex j to vertex k has a capacity of ∞ and a weight Is the directed edge of V[i].
  • If a variable Y[i] appears as Y[i] in the j-th equation, and appears as -Y[i] in the k-th equation, a connection from vertex j to vertex k has a capacity of ∞ and a weight A directed edge that is 0.
  • After composing the picture, find the minimum cost and maximum flow from the source point S to the sink point T, and the cost value is the result.

According to the above example, the following network can be constructed. The red edge is the edge represented by each variable X, and the blue edge is the edge represented by each variable Y. The capacity and weight of the edge have been marked (the blue is not marked , Because the capacity is ∞, and the weight is 0).

Insert picture description here

Find the minimum cost and maximum flow in this figure. The flow network is as shown in the figure below. The flow of each red edge is the value of the corresponding variable X.

noi_employee_2

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

The above method is so magical to get the result, think about why the composition is like this. We further deform the last five equations and get 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 the addition and subtraction of several variables and a constant, and the right side is all 0, just like the vertices in the network flow except the source and sink all satisfy the flow balance. Each positive variable is equivalent to the flow into the vertex, the negative variable is equivalent to the flow out of the vertex, and the normal number can be regarded as the flow from the additional source, and the negative constant is the flow to the additional sink. Therefore, the network can be constructed based on this, and all the equations can be satisfied by finding the maximum network flow from the additional source to the additional sink. And we also require noi_employee_3 to be the smallest, so we need to add the weight to the edge corresponding to the X variable, and then find the minimum cost and maximum flow.

What I wrote is the simple SPFA algorithm to find the minimum cost flow algorithm of the augmented road, which can pass all the test points within the time limit of the question.

At the NOI site, the average score for this question was 12.56, and only Gao Yihan and Daniel got full marks. It must be said that this is a difficult problem. The difficulty lies in abstracting the mathematical model of the problem and designing an effective algorithm. The informatics competition is moving in this direction, and mathematical modeling will be a common key step to solve the problem.

/* 
 * Problem: NOI2008 employee
 * Author: Guo Jiabao
 * Time: 2009.3.2 14:03
 * State: Solved
 * Memo: SPFA最小费用最大流
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
const int MAXN=1003,MAXM=10002*4,INF=0x7FFFFFFF;
using namespace std;
struct edge
{
    
    
    edge *next,*op;
    int t,c,v;
}ES[MAXM],*V[MAXN];
struct Queue
{
    
    
    int Q[MAXN],QH,QL,Size;
    bool inq[MAXN];
    inline void ins(int v)
    {
    
    
        if (++QL>=MAXN)
            QL=0;
        Q[QL]=v;
        inq[v]=true;
        Size++;
    }
    inline int pop()
    {
    
    
        int r=Q[QH];
        inq[r]=false;
        Size--;
        if (++QH>=MAXN)
            QH=0;
        return r;
    }
    inline void reset()
    {
    
    
        memset(Q,0,sizeof(Q));
        QH=Size=0;
        QL=-1;
    }
}Q;
int N,M,S,T,EC=-1;
int demond[MAXN],sp[MAXN],prev[MAXN];
edge *path[MAXN];
inline void addedge(int a,int b,int v,int c=INF)
{
    
    
    edge e1={
    
    V[a],0,b,c,v} , e2={
    
    V[b],0,a,0,-v};
    ES[++EC]=e1; V[a]=&ES[EC];
    ES[++EC]=e2; V[b]=&ES[EC];
    V[a]->op=V[b]; V[b]->op=V[a];
}
void init()
{
    
    
    int i,a,b,c;
    freopen("employee.in","r",stdin);
    freopen("employee.out","w",stdout);
    scanf("%d%d",&N,&M);
    for (i=1;i<=N;i++)
        scanf("%d",&demond[i]);
    for (i=1;i<=M;i++)
    {
    
    
        scanf("%d%d%d",&a,&b,&c);
        addedge(a,b+1,c);
    }
    S=0,T=N+2;
    for (i=1;i<=N+1;i++)
    {
    
    
        c = demond[i] - demond[i-1];
        if (c>=0)
            addedge(S,i,0,c);
        else
            addedge(i,T,0,-c);
        if (i>1)
            addedge(i,i-1,0);
    }
}
bool spfa()
{
    
    
    int u,v;
    for (u=S;u<=T;u++)
        sp[u]=INF;
    Q.reset();
    Q.ins(S);
    sp[S]=0;
    prev[S]=-1;
    while (Q.Size)
    {
    
    
        u=Q.pop();
        for (edge *k=V[u];k;k=k->next)
        {
    
    
            v=k->t;
            if (k->c>0 && sp[u] + k->v <sp[v])
            {
    
    
                sp[v]=sp[u] + k->v;
                prev[v]=u;
                path[v]=k;
                if (!Q.inq[v])
                    Q.ins(v);
            }
        }
    }
    return sp[T]!=INF;
}
int argument()
{
    
    
    int i,delta=INF,flow=0;
    edge *e;
    for (i=T;prev[i]!=-1;i=prev[i])
    {
    
    
        e=path[i];
        if (e->c<delta)
            delta=e->c;
    }
    for (i=T;prev[i]!=-1;i=prev[i])
    {
    
    
        e=path[i];
        e->c-=delta;e->op->c+=delta;
        flow+=e->v*delta;
    }
    return flow;
}
int maxcostflow()
{
    
    
    int Flow=0;
    while (spfa())
        Flow += argument();
    return Flow;
}
int main()
{
    
    
    init();
    printf("%dn",maxcostflow());
    return 0;
}

【Problem Description】

After the successful bid for the Olympics, Bubu finally became the head of the human resources department of the company under the Olympic Organizing Committee through unremitting efforts. As soon as Boob took office, he ran into a problem: recruiting a group of short-term volunteers for the upcoming Olympic new project. It is estimated that this project will take N days to complete, of which at least Ai will be required on the i day.

Bubu learned through understanding that there are a total of M-type volunteers that can be recruited. The i-th category can work from day Si to day Ti, and the recruitment fee is Ci yuan per person. In order to do his job well, Bubu hopes to recruit enough volunteers with the least cost, but this is not his specialty! So Bubu found you, and hope you can help him design an optimal recruitment plan.

[Input format]

The first line of the input file contains two integers N and M, which represent 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, indicating the minimum number of volunteers needed each day.

Each of the next M rows contains three integers Si, Ti, Ci, with the meanings as described above. For convenience, we can consider the number of volunteers of each type to be unlimited.

[Output format]

The input file contains only an integer, which represents the total cost of the optimal solution you have designed.

【Input sample】

3 3
2 3 4
1 2 2
2 3 5
3 3 2

[Sample output]

14

[Sample description]

Guess you like

Origin blog.csdn.net/xiaohaigary/article/details/103568488