P4014 distribution Minimum Cost Maximum

  

Title Description

There are  the n- the n-pieces of work to be assigned to  the n- the n-do individuals. The first  I I be the first individual  j benefits arising from the work piece j is  C_ {} ij of C I j  . Try to design a will  the n- the n-pieces of work assigned to  the n- the n-do individuals allocation scheme, bringing the total maximum benefit generated.

Input and output formats

Input formats:

 

The first file  1 Line 1 has  1 a positive integer  the n- the n-, expressed  the n- the n-pieces of work to be assigned to  the n- the n-do individuals.

The next  n- n-rows, each row having  n- n-integer  C_ {} ij of C I j , represents  I I be the first individual  j effective j generated by the operation member is  C_ {} ij of C I j .

 

Output formats:

 

Two lines are the smallest total output efficiency and maximum total benefit.

 

Sample input and output

Input Sample # 1:  Copy
5
2 2 2 1 2
2 3 1 2 4
2 0 1 1 1
2 3 4 3 3
3 2 1 2 1
Output Sample # 1:  Copy
5
14

Explanation

1 \ leq n \ leq 100 1 n 1 0 0

A person can only repair a workpiece

 

The cost of water problem

He ran twice the minimum and maximum are seeking to

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=1000001;

int n,m,S,T,maxflow,mincost,last[N],pre[N],dis[N],flow[N];
bool vis[N];
struct Edge{
    int next,to,flow,dis;
}edge[N<<1];
int pos=1,head[N];
void init()
{
    pos=1;
    CLR(head,0);
    mincost=maxflow=0;
}
queue <int> q;
int id(int x,int y) {return n*(x-1)+y;}

void add(int from,int to,int flow,int dis)//flow流量 dis费用
{
    edge[++pos].next=head[from];
    edge[pos].flow=flow;
    edge[pos].dis=dis;
    edge[pos].to=to;
    head[from]=pos;
    edge[++pos].next=head[to];
    edge[pos].flow=0;
    edge[pos].dis=-dis;
    edge[pos].to=from;
    head[to]=pos;
}
bool spfa(int s,int t)
{
    CLR(dis,0x3f);
    CLR(flow,0x3f);
    CLR(vis,0);
    while (!q.empty()) q.pop();
    dis[s]=0; pre[t]=-1; q.push(s); vis[s]=1;
    int tot=0;
    while (!q.empty())
    {
        int now=q.front(); q.pop(); vis[now]=0;
        for (int i=head[now]; i; i=edge[i].next)
        {
            int to=edge[i].to;
            if  (edge[i].flow>0 && dis[to]>dis[now]+edge[i].dis)
            {
                dis[to]=edge[i].dis+dis[now];
                flow[to]=min(edge[i].flow,flow[now]);
                last[to]=i;
                pre[to]=now;
                if (!vis[to])
                {
                    q.push(to); vis[to]=1;
                }
            }
        }
    }
    return pre[t]!=-1;
}
void MCMF(int s,int t)
{
    while (spfa(s,t))
    {
        int now=t;
        maxflow+=flow[t];
        mincost+=flow[t]*dis[t];
        while (now!=s)
        {
            edge[last[now]].flow-=flow[t];//dis . flow
            edge[last[now]^1].flow+=flow[t];
            now=pre[now];
        }
    }
}
struct node
{
    int u,v,cost;
}node[N];
int s,t,k;

int mp[200][200];
int main()
{
    RI(n);
    s=0,t=2*n+1;
    rep(i,1,n)
    rep(j,1,n)
    {
        RI(mp[i][j]);add(i,j+n,1,mp[i][j]);
    }
    rep(i,1,n){add(s,i,1,0);add(i+n,t,1,0);}
    MCMF(s,t);
    cout<<mincost<<endl;
    
    init();
    rep(i,1,n)rep(j,1,n)add(i,j+n,1,-mp[i][j]);
    rep(i,1,n){add(s,i,1,0);add(i+n,t,1,0);}
    MCMF(s,t);
    cout<<-mincost;

    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/bxd123/p/10944026.html