其他分享
首页 > 其他分享> > BZOJ 1221 [HNOI2001] 软件开发 费用流_建模

BZOJ 1221 [HNOI2001] 软件开发 费用流_建模

作者:互联网

Code:

#include <bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin) 
#define maxn 300000  
#define inf 1000000 
#define N 3005 
#define nex(n) (n + 1002) 
using namespace std;
struct Edge
{
    int from,to,cap,cost;
    Edge(int from = 0,int to = 0,int cap = 0,int cost = 0) : from(from),to(to),cap(cap),cost(cost){} 
}; 
vector<Edge>edges;
vector<int>G[maxn]; 
void addedge(int u,int v,int c,int d)
{
    edges.push_back(Edge(u,v,c,d)); 
    edges.push_back(Edge(v,u,0,-d)); 
    int m = edges.size(); 
    G[u].push_back(m - 2); 
    G[v].push_back(m - 1); 
}
int flow2[maxn],d[maxn],inq[N],pre[N],nn[N]; 
int ans,s,t; 
queue <int> Q; 
int spfa()
{
    for(int i = 0;i < N; ++i) d[i] = flow2[i] = inf; 
    memset(inq,0,sizeof(inq)); 
    d[s] = 0, inq[s] = 1;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop(); 
        inq[u] = 0; 
        for(int sz = G[u].size(),i = 0; i < sz ; ++i)
        {
            Edge e = edges[G[u][i]]; 
            if(d[e.to] > d[u] + e.cost && e.cap > 0)
            {
                d[e.to] = d[u] + e.cost;
                flow2[e.to] = min(e.cap, flow2[u]); 
                pre[e.to] = G[u][i]; 
                if(!inq[e.to]) 
                {
                    Q.push(e.to); 
                    inq[e.to] = 1; 
                }
            } 
        }
    }
    if(flow2[t] == inf) return 0; 
    int f = flow2[t]; 
    edges[pre[t]].cap -= f, edges[pre[t] ^ 1].cap += f; 
    int u = edges[pre[t]].from; 
    while(u != s)
    {
        edges[pre[u]].cap -= f, edges[pre[u] ^ 1].cap += f; 
        u = edges[pre[u]].from; 
    }
    ans += f * d[t]; 
    return 1; 
}
int getcost()
{
    while(spfa());
    return ans; 
}
int main()
{
    // setIO("input"); 
    int n,a,b,f,fa,fb; 
    scanf("%d%d%d%d%d%d",&n,&a,&b,&f,&fa,&fb); 
    for(int i = 1;i <= n; ++i) scanf("%d",&nn[i]);   
    s = 0,t = 2500; 
    for(int i = 1;i <= n; ++i) 
    {
        addedge(s, i, nn[i], 0);
        addedge(nex(i), t, nn[i], 0); 
        addedge(s, nex(i), inf, f); 
        if(i + 1 <= n) addedge(i, i + 1, inf,0); 
    }
    for(int i = 1;i <= n; ++i) 
    {
        if(i + a + 1 <= n) addedge(i,nex(i + 1 + a),inf,fa); 
        if(i + b + 1 <= n) addedge(i,nex(i + b + 1),inf,fb); 
    }    
    printf("%d",getcost()); 
    return 0; 
}

  

标签:pre,1221,int,cap,inq,flow2,HNOI2001,edges,BZOJ
来源: https://www.cnblogs.com/guangheli/p/10929065.html