zoukankan      html  css  js  c++  java
  • 洛谷 P2053 [SCOI2007]修车(最小费用最大流)

    题解

    最小费用最大流

    n和m是反着的

    首先,

    [ans = sum{cost[i][j]}*k ]

    其中,(k)为它在当前技术人员那里,排倒数第(k)个修

    我们可以对于每个技术人员进行拆点,

    对于每个技术人员的各个点,表示倒数第几次修

    然后每个人连向技术人员,显然花费是根据连的点来算的

    然后就是二分图带权最小匹配了

    我只会Dinic

    Code

    #include<bits/stdc++.h>
    
    #define LL long long
    #define RG register
    
    using namespace std;
    template<class T> inline void read(T &x) {
        x = 0; RG char c = getchar(); bool f = 0;
        while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
        while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
        x = f ? -x : x;
        return ;
    }
    template<class T> inline void write(T x) {
        if (!x) {putchar(48);return ;}
        if (x < 0) x = -x, putchar('-');
        int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
        for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
    }
    
    int n, m;
    
    const int N = 1010;
    
    struct node {
        int to, nxt, w, v;
    }g[2000000];
    int last[N], gl = 1;
    void add(int x, int y, int w, int v) {
        g[++gl] = (node) {y, last[x], w, v};
        last[x] = gl;
        g[++gl] = (node) {x, last[y], 0, -v};
        last[y] = gl;
    }
    
    int dis[N], s, t, pre[N], from[N];
    bool vis[N];
    queue<int> q;
    
    bool spfa() {
        memset(dis, 127, sizeof(dis));
        q.push(s);
        dis[s] = 0;
        while (!q.empty()) {
            int u = q.front(); q.pop();
            //	printf("u = %d
    ", u);
            for (int i = last[u]; i; i = g[i].nxt) {
                int v = g[i].to;
                if (g[i].w && dis[v] > dis[u]+g[i].v) {
                    dis[v] = dis[u]+g[i].v;
                    from[v] = i; pre[v] = u;
                    if (!vis[v]) {
                        vis[v] = 1;
                        q.push(v);
                    }
                }
            }
            vis[u] = 0;		
        }
    //	printf("%d
    ", dis[t]);
        return dis[t] != dis[0];
    }
    
    int Mcmf() {
        int ans = 0;
        while (spfa()) {
            ans += dis[t];
            for (int i = t; i != s; i = pre[i])
                g[from[i]].w--, g[from[i]^1].w++;
        }
        return ans;
    }
    
    int a[70][10];
    
    int main() {
        //freopen(".in", "r", stdin);
        //freopen(".out", "w", stdout);
        read(m), read(n);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                read(a[i][j]);
        for (int k = 1; k <= m; k++)//第k个技术人员
            for (int i = 1; i <= n; i++)//第i个顾客
                for (int j = 1; j <= n; j++)//倒数第j个修理
                    add(n*m+i, (k-1)*n+j, 1, j*a[i][k]);
        s = n*m+n+1, t = s+1;
        for (int i = n*m+1; i < s; i++)
            add(s, i, 1, 0);
        for (int i = 1; i <= n*m; i++)
            add(i, t, 1, 0);
        printf("%.2lf
    ", Mcmf()*1.0/n);
        return 0;
    }
    
    
    
  • 相关阅读:
    mininet和ryu控制器的连接
    Linux服务器(Ubuntu14.04)添加远程连接VNC Server
    KVM的前世今生
    Ubuntu下搭建ryu环境
    Ubuntu下搭建Mininet环境
    手机蓝牙
    常见的js算法面试题收集,es6实现
    前端笔试题面试题记录(上)
    关于js中onclick字符串传参问题(html="")
    Angular $scope和$rootScope事件机制之$emit、$broadcast和$on
  • 原文地址:https://www.cnblogs.com/zzy2005/p/10290528.html
Copyright © 2011-2022 走看看