Luogu P4014 allocation problem solution

Cackle

Tacca first questions to come up with how to build their own road map! !Although no technical content of a chart I thought, or write an article in memory of problem solutions.

Face questions

Face questions

Solution

It requires a minimum cost and maximum costs, while limiting the flow, consider cost flow.

A super virtual source point, respectively, from this point to (N \) \ task even a flow rate of \ (1 \) at a cost of \ (0 \) side.

A super virtual meeting point, just from \ (N \) th items are even a point to flow \ (1 \) at a cost of \ (0 \) side.

Because each person can only do one, and each work can only be done once, so the edges are even flow into one. The first \ (i \) individual to do the first \ (j \) contribution to the tasks available for the \ (val_ {i, J} \) , so from the first \ (j \) th goods into the first \ (i \) Personal even a cost of \ (val_ {i, j} \) side.

If you are seeking the minimum cost maximum flow, then ran directly templates.

If you are seeking the most cost maximum flow, even just to change sides when the cost is negative, find a minimum cost maximum flow, then the answer then take a number to the contrary. (This deal seconds ah, their thought out, or read the solution to a problem)

Code

#include<bits/stdc++.h>
#define del(a,i) memset(a,i,sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
using namespace std;
template<class T>il read(T &x){
    int f=1;char k=getchar();x=0;
    for(;k>'9'||k<'0';k=getchar()) if(k=='-') f=-1;
    for(;k>='0'&&k<='9';k=getchar()) x=(x<<3)+(x<<1)+k-'0';
    x*=f;
}
template<class T>il print(T x){
    if(x/10) print(x/10);
    putchar(x%10+'0');
}
ll mul(ll a,ll b,ll mod){long double c=1.;return (a*b-(ll)(c*a*b/mod)*mod)%mod;}
it qpow(int x,int m,int mod){
    int res=1,bas=x%mod;
    while(m){
        if(m&1) res=(res*bas)%mod;
        bas=(bas*bas)%mod,m>>=1;
    }
    return res%mod;
}
int n,s,t,head[205],num_edge=-1,pre[205],last[205],flow[205],dis[205],mn_cost,val[105][105];
struct Edge{
    int next,to,w,c;
    Edge(){}
    Edge(int next,int to,int w,int c):next(next),to(to),w(w),c(c){}
}edge[30000];
il add_edge(int u,int v,int w,int c){
    edge[++num_edge]=Edge(head[u],v,w,c),head[u]=num_edge;
    edge[++num_edge]=Edge(head[v],u,0,-c),head[v]=num_edge;
}
bool tr[205];
inl bool SPFA(int s,int t){
    queue<int> q;q.push(s);
    del(dis,0x3f),del(flow,0x3f);
    dis[s]=0,pre[t]=-1,tr[s]=1;
    while(!q.empty()){
        ri pos=q.front();q.pop(),tr[pos]=0;
        for(ri i=head[pos];i!=-1;i=edge[i].next)
            if(dis[edge[i].to]>dis[pos]+edge[i].c&&edge[i].w>0){
                dis[edge[i].to]=dis[pos]+edge[i].c;
                pre[edge[i].to]=pos,last[edge[i].to]=i;
                flow[edge[i].to]=min(flow[pos],edge[i].w);
                if(!tr[edge[i].to]) q.push(edge[i].to),tr[edge[i].to]=1;
            }
    }
    return pre[t]!=-1;
}
il MCMF(int s,int t){
    while(SPFA(s,t)){
        mn_cost+=dis[t]*flow[t];
        for(ri u=t;u^s;u=pre[u]) edge[last[u]].w-=flow[t],edge[last[u]^1].w+=flow[t];
    }
}
int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    read(n),del(head,-1),t=2*n+1;
    for(ri i=1;i<=n;++i)
        for(ri j=1;j<=n;++j){
            read(val[i][j]);
            add_edge(j,i+n,1,val[i][j]);
        }
    for(ri i=1;i<=n;++i) add_edge(s,i,1,0);
    for(ri i=1;i<=n;++i) add_edge(i+n,t,1,0);
    MCMF(s,t);
    printf("%d\n",mn_cost);
    del(head,-1),num_edge=-1,mn_cost=0;
    for(ri i=1;i<=n;++i)
        for(ri j=1;j<=n;++j)
            add_edge(j,i+n,1,-val[i][j]);
    for(ri i=1;i<=n;++i) add_edge(s,i,1,0);
    for(ri i=1;i<=n;++i) add_edge(i+n,t,1,0);
    MCMF(s,t);
    printf("%d",-mn_cost);
    return 0;
}

to sum up

This question is a board problem for entry plus a familiar template.

Figure way to build a network stream thousands and thousands of really good magic, not satisfied with the present achievements, still have to practice questions, find their own network flow routine it ~ ~

Guess you like

Origin www.cnblogs.com/TheShadow/p/11370196.html