"Bzoj 1061" [Noi2008] Volunteer recruitment

Source: Internet
Author: User

1061: [Noi2008] Volunteer recruitment time limit: Sec Memory Limit: 162 MB
Submit: 2066 Solved: 1282
[Submit] [Status] 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.

Input

The first line 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

Recruitment of the first category of volunteers 3, the third category of volunteers 4 30% of the data, 1≤n, m≤10,1≤ai≤10, 100% of the data, 1≤n≤1000,1≤m≤10000, the title of the other involved in the data are not more than 2^31-1.


Cost flow, modeling good problem!!


The byvoid is very detailed.


Why would he do that?


My understanding is:

Because the network flow is a traffic balance, the inequality is transformed into an equation.


Next, each of the equations as a point in the graph, + for inflow,-indicating outflow;


In order for each variable to have an outflow, the upper and lower formulas are subtracted.


According to the flow balance, the sum of outflow and outflow is 0, so the Bashi sub-shift item.


Finally, the model is modeled according to inflow and outflow.


#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include < cstdlib> #include <cstring> #define INF 0x3f3f3f3f#define M 200005using namespace Std;int h[m],tot=1,s,t,n,m, Need[1005],d[m],inq[m],p[m],f[m];struct segtree{int L,r;} A[3005];struct edge{int From,to,cap,flow,cost,ne;} E[500005];void addedge (int from,int to,int cap,int cost) {tot++; E[tot]= (Edge) {from,to,cap,0,cost,h[from]};h[from]=tot;tot++; E[tot]= (Edge) {To,from,0,0,-cost,h[to]};h[to]=tot;} BOOL SPFA (int &flow,int &cost) {for (int i=s;i<=t;i++) inq[i]=0,d[i]=inf;queue<int> Q;q.push (s); inq[s    ]=1,d[s]=0,p[s]=0,f[s]=inf;while (!q.empty ()) {int X=q.front (); Q.pop (); inq[x]=0;for (int i=h[x];i;i=e[i].ne) {edge e=e[i];if (e.cap>e.flow&&d[e.to]>d[x]+e.cost) {d[ E.to]=d[x]+e.cost;f[e.to]=min (E.cap-e.flow,f[x]);p [E.to]=i;if (!inq[e.to]) Q.push (e.to), Inq[e.to]=1;}}} if (D[t]==inf) return false;flow+=f[t];cost+=f[t]*d[t];int u=t;while (u!=s) {e[p[u]].flow+=f[t]; E[p[U]^1].flow-=f[t];u=e[p[u]].from;} return true;} int Mincost () {int flow=0,cost=0;while (SPFA (Flow,cost)); return cost;}  int main () {scanf ("%d%d", &n,&m), need[0]=0;s=0;t=n+2;for (int i=1;i<=n;i++) {scanf ("%d", &need[i]); int K=need[i]-need[i-1];if (k>=0) Addedge (s,i,k,0); else Addedge (i,t,-k,0);} Addedge (n+1,t,need[n],0); for (int i=1;i<=m;i++) {int s,t,c;scanf ("%d%d%d", &s,&t,&c); Addedge (s,t+1,inf,c);} for (int i=1;i<=n;i++) Addedge (i+1,i,inf,0); Cout<<mincost () <<endl;return 0;}


Sentiment:

1. One of the points learned from this question is that network flow modeling can determine inflow and outflow based on the addition and subtraction of the equation, with the exception of the source and sink, all other point traffic is balanced.

"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.