zoukankan      html  css  js  c++  java
  • 洛谷4015运输问题

    题目链接:运输问题

    当然可以写KM了对吧

    但是写费用流也是一样简单啊

    不管那边都是模板啊

    #include<iostream>
    #include<string>
    #include<string.h>
    #include<stdio.h>
    #include<algorithm>
    #include<math.h>
    #include<vector>
    #include<queue>
    #include<map>
    using namespace std;
    #define rep(i,a,b) for (i=a;i<=b;i++)
    typedef long long ll;
    #define maxd 1e9+7
    
    struct node{
        int to,nxt,cost,flow;
    }sq[100100];
    int n,m,all=1,dis[100100],pre[100100],head[100100],s,t,maxf=0,minc=0,
        mp[110][110],flow[100100],a[120],b[120];
    bool vis[100100];
    void add(int u,int v,int f,int w)
    {
        all++;sq[all].to=v;sq[all].nxt=head[u];sq[all].cost=w;sq[all].flow=f;head[u]=all;
        all++;sq[all].to=u;sq[all].nxt=head[v];sq[all].cost=-w;sq[all].flow=0;head[v]=all;
    }
    
    bool spfa()
    {
        queue<int> q;int i;
        for (i=1;i<=t;i++) dis[i]=maxd;dis[s]=0;
        memset(vis,0,sizeof(vis));
        memset(flow,0,sizeof(flow));
        vis[s]=1;q.push(s);flow[s]=maxd;
        while (!q.empty())
        {
            int u=q.front();q.pop();vis[u]=0;
            for (i=head[u];i;i=sq[i].nxt)
            {
                int v=sq[i].to;
                if ((sq[i].flow) && (dis[v]>dis[u]+sq[i].cost))
                {
                    dis[v]=sq[i].cost+dis[u];pre[v]=i;
                    flow[v]=min(flow[u],sq[i].flow);
                    if (!vis[v])
                    {
                        vis[v]=1;q.push(v);
                    }
                }
            }
        }
        return (dis[t]!=maxd);
    }
    
    void update()
    {
        int now=t;
        maxf+=flow[t];
        minc+=(flow[t]*dis[t]);
        while (now!=s)
        {
            int tmp=pre[now];
            sq[tmp].flow-=flow[t];
            sq[tmp^1].flow+=flow[t];
            now=sq[tmp^1].to;
        }
    }
    
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
        while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
        return x*f;
    }
    
    void init()
    {
        m=read();n=read();
        s=n+m+1;t=n+m+2;
        int i,j;
        for (i=1;i<=m;i++) a[i]=read();
        for (i=1;i<=n;i++) b[i]=read();
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++) mp[i][j]=read();
        }
    }
    
    void init_max()
    {
        int i,j;all=1;
        memset(head,0,sizeof(head));
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
            {
                add(i,j+m,maxd,-mp[i][j]);
            }
        }
        for (i=1;i<=m;i++) add(s,i,a[i],0);
        for (i=1;i<=n;i++) add(i+m,t,b[i],0);
    }
    
    void init_min()
    {
        int i,j;all=1;
        memset(head,0,sizeof(head));
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
            {
                add(i,j+m,maxd,mp[i][j]);
            }
        }
        for (i=1;i<=m;i++) add(s,i,a[i],0);
        for (i=1;i<=n;i++) add(i+m,t,b[i],0);
    }
    
    void work(int fu)
    {
        maxf=0;minc=0;
        while (spfa()) update();
        printf("%d
    ",minc*fu);
    }
    
    int main()
    {
        init();
        init_min();
        work(1);
        init_max();
        work(-1);
        return 0;
    }
    
  • 相关阅读:
    HNOI2018退役记
    codeforces 960G Bandit Blues
    codeforces 933D A Creative Cutout
    tyvj1953 Normal
    loj6119 「2017 山东二轮集训 Day7」国王
    codeforces 293E Close Vertices
    bzoj1808 [Ioi2007]training 训练路径
    bzoj2219 数论之神
    bzoj4361 isn
    loj2064[HAOI2016]找相同字符
  • 原文地址:https://www.cnblogs.com/encodetalker/p/10182736.html
Copyright © 2011-2022 走看看