• POJ 3686 The Windy's (最小费用流或最佳完全匹配)


    题意:有n个订单m个车间,每个车间均可以单独完成任何一个订单。每个车间完成不同订单的时间是不同的。不会出现两个车间完成同一个订单的情况。给出每个订单在某个车间完成所用的时间。问订单完成的平均时间是多少。

    析:这个题可以用最小费用流或者最佳完全匹配来做,因为只有车间和订单,满足二分图,主要是在建图。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    //#define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 50 * 50 + 100 + 10;
    const int maxm = 1e5 + 10;
    const int mod = 50007;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, -1, 0, 1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    struct Edge{
      int from, to, cap, flow, cost;
    };
    
    struct MinCostMaxFlow{
      int n, m, s, t;
      vector<Edge> edges;
      vector<int> G[maxn];
      bool inq[maxn];
      int d[maxn];
      int p[maxn];
      int a[maxn];
    
      void init(int n){
        this-> n = n;
        for(int i = 0; i < n; ++i)  G[i].cl;
        edges.cl;
      }
    
      void addEdge(int from, int to, int cap, int cost){
        edges.pb((Edge){from, to, cap, 0, cost});
        edges.pb((Edge){to, from, 0, 0, -cost});
        m = edges.sz;
        G[from].pb(m - 2);
        G[to].pb(m - 1);
      }
    
      bool bellman(int &flow, int &cost){
        ms(d, INF);  ms(inq, 0);
        inq[s] = 1;  d[s] = 0;  a[s] = INF;  p[s] = 0;
        queue<int> q;
        q.push(s);
    
        while(!q.empty()){
          int u = q.front();  q.pop();
          inq[u] = 0;
          for(int i = 0; i < G[u].sz; ++i){
            Edge &e = edges[G[u][i]];
            if(e.cap > e.flow && d[e.to] > d[u] + e.cost){
              d[e.to] = d[u] + e.cost;
              p[e.to] = G[u][i];
              a[e.to] = min(a[u], e.cap - e.flow);
              if(!inq[e.to]){ q.push(e.to);  inq[e.to] = 1; }
            }
          }
        }
        if(d[t] == INF)  return false;
        cost += a[t] * d[t];
        flow += a[t];
        int u = t;
        while(u != s){
          edges[p[u]].flow += a[t];
          edges[p[u]^1].flow -= a[t];
          u = edges[p[u]].from;
        }
        return true;
      }
    
      int mincostmaxflow(int s, int t){
        this->s = s;  this->t = t;
        int flow = 0, cost = 0;
        while(bellman(flow, cost));
        return cost;
      }
    };
    MinCostMaxFlow mcmf;
    
    int a[55][55];
    
    int main(){
      int T;  cin >> T;
      while(T--){
        scanf("%d %d", &n, &m);
        int s = 0, t = n * m + n + 2;
        mcmf.init(t + 10);
        for(int i = 1; i <= n; ++i)
          for(int j = 1; j <= m; ++j)
            scanf("%d", a[i] + j);
        for(int i = 1; i <= n; ++i){
          mcmf.addEdge(s, i, 1, 0);
          for(int j = 1; j <= m; ++j)
            for(int k = 1; k <= n; ++k){
              mcmf.addEdge(i, j * n + k, 1, k * a[i][j]);
              if(i == 1)  mcmf.addEdge(j * n + k, t, 1, 0);
            }
        }
        printf("%f
    ", mcmf.mincostmaxflow(s, t) * 1. / n);
      }
      return 0;
    }
    

      

  • 相关阅读:
    MemCached总结二:数据管理指令
    MemCached总结一:Unbutu操作系统下memcached服务器安装和telnet方式连接memcache
    Laravel5 开启Debug
    状压dp
    树形dp
    区间dp
    线性dp
    背包九讲
    dp求解各种子串子序列
    线段树详解
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7652116.html
Copyright © 2020-2023  润新知