bzoj1061 [Noi2008] Volunteer recruitment (network flow solves linear programming problems)

Source: Internet
Author: User
Tags bool
bzoj1061 [Noi2008] Volunteer recruitment

Original title address : http://www.lydsy.com/JudgeOnline/problem.php?id=1061

Test Instructions:
A project takes n days to complete, with the first day I need an AI person at least. A total of M-type volunteers can be recruited. In which class I can work from Si days to ti days, the recruitment fee is per person ci yuan. Bubu hopes to recruit enough volunteers with as little as possible to find the best cost of recruiting programs.

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

The following:
The byvoid of God

is to construct a formula that says that each variable appears one negative at a time.
The auxiliary variable is then added for the processing of the >=.
For positive and negative variables as inflow and outflow, satisfy inequalities ..... =0 is the flow balance.

Code:

#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <
Queue> using namespace std;
const int n=505;
const int m=805;
const int inf=0x3f3f3f3f;
Queue<int> Q;
int n,m,head[n],to[4*m],nxt[4*m],w[4*m],id,u[m],v[m],w[m],s,t,num=1,dep[n];
BOOL Vis[n];
    void build (int u,int v,int ww) {num++; To[num]=v;
    Nxt[num]=head[u]; Head[u]=num;
    W[NUM]=WW;
    num++; To[num]=u;
    NXT[NUM]=HEAD[V]; Head[v]=num;
w[num]=0;
    } bool BFs () {memset (vis,0,sizeof (VIS)); Q.push (S); Vis[s]=1;
    Dep[s]=1; while (! Q.empty ()) {int U=q.front ();
        Q.pop ();
            for (int i=head[u];i;i=nxt[i]) {int v=to[i]; if (w[i]<=0| |
            VIS[V]) continue; Vis[v]=1;
            dep[v]=dep[u]+1;
        Q.push (v);
}} return vis[t]; } int dfs (int u,int d) {if (!d| | U==T) return D;
    int ret=0;
        for (int i=head[u];i;i=nxt[i]) {int v=to[i]; if (w[i]<=0| | Dep[v]!=dEP[U]+1) continue;
        int Flow=dfs (V,min (D,w[i])); Ret+=flow;
        D-=flow; W[i]-=flow;
        W[i^1]+=flow;
    if (!d) break;
    } if (!ret) dep[u]=-1;
return ret;
    } int main () {scanf ("%d%d%d", &n,&m,&id);
    for (int i=1;i<=m;i++) scanf ("%d%d%d", &u[i],&v[i],&w[i]);
        for (int i=1;i<=m;i++) {if (i==id) s=u[i],t=v[i];
    else if (W[i]<=w[id]) build (u[i],v[i],w[id]-w[i]+1), build (v[i],u[i],w[id]-w[i]+1);
    } int ret=0;
    while (BFS ())) Ret+=dfs (S,inf);
    printf ("%d\n", ret);
return 0;
 }

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.