• luogu P3381【模板】最小费用最大流


    嘟嘟嘟


    没错,我开始学费用流了!


    做法也是比较朴素的(spfa)
    就是每一次以费用为权值跑一遍(spfa)找到一条最短路,然后把这条道全流满,并把这一次的流量和费用累加到答案上。因此我们需要记录路径。
    就这样一直跑直到没有增广路为止,然后好像就没了。(不难啊……)
    因为每一只找一条道,所以其实挺慢的。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define rg register
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 5e3 + 5;
    const int maxm = 5e4 + 5;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int n, m, s, t;
    struct Edge
    {
      int nxt, from, to, cap, c;
    }e[maxm << 1];
    int head[maxn], ecnt = -1;
    void addEdge(int x, int y, int w, int f)
    {
      e[++ecnt] = (Edge){head[x], x, y, w, f};
      head[x] = ecnt;
      e[++ecnt] = (Edge){head[y], y, x, 0, -f};
      head[y] = ecnt;
    }
    
    queue<int> q;
    int dis[maxn], flow[maxn], pre[maxn];
    bool in[maxn];
    bool spfa(int s, int t)
    {
      Mem(dis, 0x3f); Mem(in, 0);
      dis[s] = 0; in[s] = 1; flow[s] = INF; //源点流量无限,故初值为INF
      q.push(s);
      while(!q.empty())
        {
          int now = q.front(); q.pop(); in[now] = 0;
          for(int i = head[now], v; i != -1; i = e[i].nxt)
    	{
    	  v = e[i].to;
    	  if(e[i].cap > 0 && dis[now] + e[i].c < dis[v])
    	    {
    	      dis[v] = dis[now] + e[i].c;
    	      flow[v] = min(flow[now], e[i].cap);
    	      pre[v] = i;
    	      if(!in[v]) in[v] = 1, q.push(v);
    	    }
    	}
        }
      return dis[t] != INF;
    }
    int maxFlow = 0, minCost = 0;
    void update(int s, int t)
    {
      int x = t;
      while(x != s)
        {
          int i = pre[x];
          e[i].cap -= flow[t];
          e[i ^ 1].cap += flow[t];
          x = e[i].from;
        }
      maxFlow += flow[t];
      minCost += flow[t] * dis[t];
    }
    
    void MCMF(int s, int t)
    {
      while(spfa(s, t)) update(s, t);
    }
    
    int main()
    {
      Mem(head, -1);
      n = read(); m = read(); s = read(); t = read();
      for(int i = 1; i <= m; ++i)
        {
          int x = read(), y = read(), w = read(), f = read();
          addEdge(x, y, w, f);
        }
      MCMF(s, t);
      write(maxFlow), space, write(minCost), enter;
      return 0;
    }
    
  • 相关阅读:
    JQ和Js获取span标签的内容
    JS获取子节点、父节点和兄弟节点的方法实例总结
    JS实现系统时间(自动)
    CSS font-family 属性
    网页中导入特殊字体@font-face属性详解
    ****HTML模板资源汇总
    ***XAMPP:报错 Unable to load dynamic library的解决方法
    2016年宜昌楼市将迎来史上最激烈一战
    北大资源重磅来宜--宜昌未来商业中心将诞生
    HTML5调用传感器的资料汇总
  • 原文地址:https://www.cnblogs.com/mrclr/p/10009519.html
Copyright © 2020-2023  润新知