zoukankan      html  css  js  c++  java
  • luogu [SDOI2009]晨跑 费用流模板题

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<iostream>
    using namespace std;
    const int maxn=100010;
    bool vis[maxn];
    int n,m,s,t,x,y,z,f,dis[maxn],pre[maxn],last[maxn],flow[maxn],maxflow,mincost;
    //dis最小花费;pre每个点的前驱;last每个点的所连的前一条边;flow源点到此处的流量
    //maxflow 最大流量
    //mincost 最大流量的情况下的最小花费
    struct Edge
    {
        int to,next,flow,dis;//flow流量 dis花费
    } edge[maxn];
    int head[maxn],num_edge;
    queue <int> q;
    void add_edge(int from,int to,int flow,int dis)
    {
        edge[++num_edge].next=head[from];
        edge[num_edge].to=to;
        edge[num_edge].flow=flow;
        edge[num_edge].dis=dis;
        head[from]=num_edge;
    }
    bool spfa(int s,int t)
    {
        memset(dis,0x7f,sizeof(dis));
        memset(flow,0x7f,sizeof(flow));
        memset(vis,0,sizeof(vis));
        q.push(s);
        vis[s]=1;
        dis[s]=0;
        pre[t]=-1;
        while (!q.empty())
        {
            int now=q.front();
            q.pop();
            vis[now]=0;
            for (int i=head[now]; i!=-1; i=edge[i].next)
            {
                if (edge[i].flow>0 && dis[edge[i].to]>dis[now]+edge[i].dis)//正边
                {
                    dis[edge[i].to]=dis[now]+edge[i].dis;
                    pre[edge[i].to]=now;
                    last[edge[i].to]=i;
                    flow[edge[i].to]=min(flow[now],edge[i].flow);//
                    if (!vis[edge[i].to])
                    {
                        vis[edge[i].to]=1;
                        q.push(edge[i].to);
                    }
                }
            }
        }
        return pre[t]!=-1;
    }
    
    void MCMF()
    {
        while (spfa(s,t))
        {
            int now=t;
            maxflow+=flow[t];
            mincost+=flow[t]*dis[t];
            while (now!=s)
            {
                //从源点一直回溯到汇点
                edge[last[now]].flow-=flow[t];//flow和dis容易搞混
                edge[last[now]^1].flow+=flow[t];
                now=pre[now];
            }
        }
    }
    int main()
    {
        memset(head,-1,sizeof(head));
        num_edge=-1;
        cin>>n>>m;
        s=1+n,t=n;
        for (int i=1; i<=m; i++)
        {
            //z 最大流量
            //f  单位流量的费用
            int a,b,c;
            cin>>a>>b>>c;
            add_edge(a+n,b,1,c);
            add_edge(b,a+n,0,-c);
        }
        for(int i=1; i<=n; i++)
        {
            add_edge(i,i+n,1,0);
            add_edge(i+n,i,0,0);
        }
        MCMF();
        printf("%d %d",maxflow,mincost);
        return 0;
    }
  • 相关阅读:
    每个部门都有自己的游戏规则
    ssh作为代理,反向登录没有固定公网ip的局域网内的某远程服务器
    x11vnc 作为远程桌面服务器时vnc客户端键盘无法长按连续输入字符
    vim 编译使用ycm启动问题 fixed
    ubuntu设置普通用户也能执行docker命令
    git常见使用
    切图的必要步骤
    css居中
    清除浮动
    Spring-AOP(2)
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/13178367.html
Copyright © 2011-2022 走看看