zoukankan      html  css  js  c++  java
  • 洛谷P3381 MCMF【网络流】

    题目描述

    如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用。

    输入格式

    第一行包含四个正整数N、M、S、T,分别表示点的个数、有向边的个数、源点序号、汇点序号。

    接下来M行每行包含四个正整数ui、vi、wi、fi,表示第i条有向边从ui出发,到达vi,边权为wi(即该边最大流量为wi),单位流量的费用为fi。

    输出格式

    一行,包含两个整数,依次为最大流量和在最大流量情况下的最小费用。

    输入输出样例

    输入 #1
    4 5 4 3
    4 2 30 2
    4 3 20 3
    2 3 20 1
    2 1 30 9
    1 3 40 5
    输出 #1
    50 280

    说明/提示

    时空限制:1000ms,128M

    (BYX:最后两个点改成了1200ms)

    数据规模:

    对于30%的数据:N<=10,M<=10

    对于70%的数据:N<=1000,M<=1000

    对于100%的数据:N<=5000,M<=50000

    第一条流为4-->3,流量为20,费用为3*20=60。

    第二条流为4-->2-->3,流量为20,费用为(2+1)*20=60。

    第三条流为4-->2-->1-->3,流量为10,费用为(2+9+5)*10=160。

    故最大流量为50,在此状况下最小费用为60+60+160=280。

    结合题目理解板子很高效,建图很明显的一道题和感谢帮助我很多的oi爷给的这个板子

    CODE:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    
    using namespace std;
    const int maxn = 1e5 + 7;
    
    bool vis[maxn];
    int n,m,s,t,u,v,w,c;
    int cost[maxn],pre[maxn],last[maxn],flow[maxn];
    int maxflow,mincost;
    
    
    template<class T>inline void read(T &res)
    {
        char c;T flag=1;
        while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
        while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
    }
    
    struct edge{
        int to,nxt,flow,cost;
    }e[maxn << 1];
    
    int head[maxn],cnt;
    
    queue <int> q;
    
    void init()
    {
        memset(cost,0x7f,sizeof(cost));
        memset(flow,0x7f,sizeof(flow));
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        cnt=-1;
    }
    
    inline void BuildGraph(int u,int to,int flow,int cost)
    {
        e[++cnt].nxt = head[u];
        e[cnt].to = to;
        e[cnt].flow = flow;
        e[cnt].cost  = cost;
        head[u]  = cnt;
    
        e[++cnt].nxt = head[to];
        e[cnt].to = u;
        e[cnt].flow = 0;
        e[cnt].cost = -cost;
        head[v] = cnt;
    }
    
    bool spfa(int s,int t)
    {
        memset(cost,0x7f,sizeof(cost));
        memset(flow,0x7f,sizeof(flow));
        memset(vis,0,sizeof(vis));
        q.push(s);
        vis[s] = 1;
        cost[s] = 0;
        pre[t] = -1;
    
        while (!q.empty()) {
            int temp = q.front();
            q.pop();
            vis[temp] = 0;
            for (int i = head[temp]; i != -1; i = e[i].nxt) {
                if (e[i].flow > 0 && cost[e[i].to] > cost[temp]+e[i].cost) {
                    cost[e[i].to]  = cost[temp]+e[i].cost;
                    pre[e[i].to]  = temp;
                    last[e[i].to] = i;
                    flow[e[i].to] = min(flow[temp],e[i].flow);//
                    if (!vis[e[i].to]) {
                        vis[e[i].to] = 1;
                        q.push(e[i].to);
                    }
                }
            }
        }
        return pre[t]!=-1;
    }
    
    void MCMF()
    {
        while (spfa(s,t)) {
            int temp = t;
            maxflow  += flow[t];
            mincost  += flow[t]*cost[t];
            while (temp != s) {
                e[last[temp]].flow -= flow[t];
                e[last[temp]^1].flow += flow[t];
                temp = pre[temp];
            }
        }
    }
    
    int main()
    {
        init();
        scanf("%d %d %d %d",&n, &m, &s, &t);
        for (int i = 1; i <= m; i++)
        {
            read(u), read(v), read(w), read(c);
            BuildGraph(u,v,w,c);
        }
        MCMF();
        printf("%d %d",maxflow,mincost);
        return 0;
    }
  • 相关阅读:
    【QT】error: 'connect'/'sender' was not declared in this scope
    【QT】添加图片资源并使用QImage加载图片显示
    【QT】去掉.ui窗口的最大化、最小化和关闭按钮
    【Linux】(一)美化Linux终端之oh-my-zsh开源项目(Linux终端主题)
    【git】配置git命令行别名
    浅谈函数防抖与函数节流
    php连接数据库的天龙八部!
    JavaScript画圆
    表格的即时编辑
    用html+css+js模拟下拉菜单
  • 原文地址:https://www.cnblogs.com/orangeko/p/11937763.html
Copyright © 2011-2022 走看看