码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2112 Optimal Milking (二分 + floyd + 网络流)

时间:2014-08-27 22:04:08      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:poj   网络流   二分   floyd   

POJ 2112 Optimal Milking 


题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C≤200)头奶牛,在奶牛和挤奶器之间有一组不同长度的路。K个挤奶器的位置用1~K的编号标明,奶牛的位置用K+1~K+C 的编号标明。每台挤奶器每天最多能为M(1≤M≤15)头奶牛挤奶。寻找一个方案,安排每头奶牛到某个挤奶器挤奶,并使得C 头奶牛需要走的所有路程中的最大路程最小。每个测试数据中至少有一个安排方案。每条奶牛到挤奶器有多条路。

思路:先用Floyd 算法求出能达到的任意两点之间的最短路径,然后二分最大距离的最小值,每次用二分的值求最大流。
在求最大流时构图:建立一个源点,每个点到挤奶器连一条流量为m的边。建立一个汇点,每头奶牛到汇点连一条流量为1的边。挤奶器与奶牛之间的距离小于等于mid则连边,流量为1。最后求最大流是否为c即可。

代码:
/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-6
#define debug puts("===============")
#define pb push_back
//#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define POSIN(x,y) (0 <= (x) && (x) < n && 0 <= (y) && (y) < m)
typedef long long ll;
typedef unsigned long long ULL;
const int maxn = 250;
const int maxm = 100000;
int k, c, m;
int mp[maxn][maxn];
struct node {
    int v;    // vertex
    int cap;    // capacity
    int flow;   // current flow in this arc
    int nxt;
} e[maxm * 2];
int g[maxn], cnt;
int st, ed, n;
void add(int u, int v, int c) {
    e[++cnt].v = v;
    e[cnt].cap = c;
    e[cnt].flow = 0;
    e[cnt].nxt = g[u];
    g[u] = cnt;

    e[++cnt].v = u;
    e[cnt].cap = 0;
    e[cnt].flow = 0;
    e[cnt].nxt = g[v];
    g[v] = cnt;
}

void init(int mid) {
    mem(g, 0);
    cnt = 1;
    st = 0, ed = k + c + 1;
    n = k + c;
    for (int i = 1; i <= k; i++) add(st, i, m);
    for (int i = k + 1; i <= n; i++) {
        for (int j = 1; j <= k; j++) if (mp[i][j] <= mid) add(j, i, 1);
        add(i, ed, 1);
    }
    n += 3;
}

int dist[maxn], numbs[maxn], q[maxn];
void rev_bfs() {
    int font = 0, rear = 1;
    for (int i = 0; i <= n; i++) { //n为总点数
        dist[i] = maxn;
        numbs[i] = 0;
    }
    q[font] = ed;
    dist[ed] = 0;
    numbs[0] = 1;
    while(font != rear) {
        int u = q[font++];
        for (int i = g[u]; i; i = e[i].nxt) {
            if (e[i ^ 1].cap == 0 || dist[e[i].v] < maxn) continue;
            dist[e[i].v] = dist[u] + 1;
            ++numbs[dist[e[i].v]];
            q[rear++] = e[i].v;
        }
    }
}
int maxflow() {
    rev_bfs();
    int u, totalflow = 0;
    int curg[maxn], revpath[maxn];
    for(int i = 0; i <= n; ++i) curg[i] = g[i];
    u = st;
    while(dist[st] < n) {
        if(u == ed) {   // find an augmenting path
            int augflow = INF;
            for(int i = st; i != ed; i = e[curg[i]].v)
                augflow = min(augflow, e[curg[i]].cap);
            for(int i = st; i != ed; i = e[curg[i]].v) {
                e[curg[i]].cap -= augflow;
                e[curg[i] ^ 1].cap += augflow;
                e[curg[i]].flow += augflow;
                e[curg[i] ^ 1].flow -= augflow;
            }
            totalflow += augflow;
            u = st;
        }
        int i;
        for(i = curg[u]; i; i = e[i].nxt)
            if(e[i].cap > 0 && dist[u] == dist[e[i].v] + 1) break;
        if(i) {   // find an admissible arc, then Advance
            curg[u] = i;
            revpath[e[i].v] = i ^ 1;
            u = e[i].v;
        } else {    // no admissible arc, then relabel this vertex
            if(0 == (--numbs[dist[u]])) break;    // GAP cut, Important!
            curg[u] = g[u];
            int mindist = n;
            for(int j = g[u]; j; j = e[j].nxt)
                if(e[j].cap > 0) mindist = min(mindist, dist[e[j].v]);
            dist[u] = mindist + 1;
            ++numbs[dist[u]];
            if(u != st)
                u = e[revpath[u]].v;    // Backtrack
        }
    }
    return totalflow;
}
void get() {
    n = k + c;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            scanf("%d", mp[i] + j);
            if (mp[i][j] == 0 && i != j) mp[i][j] = INF;
        }
    }
}
void floyd(int n, int mp[][maxn]) {
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) if (mp[i][k] != INF) {
            for (int j = 1; j <= n; j++) mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
        }
    }
}
int main () {

    while(scanf("%d%d%d", &k, &c, &m) != EOF) {
        get();
        floyd(n, mp);
        int l = 0, r = 10000, mid;
        while(l < r) {
            mid = (l + r) >> 1;
            init(mid);
            //debug;
            if (maxflow() >= c) r = mid;
            else l = mid + 1;
        }
        printf("%d\n", r);
    }
    return 0;
}


POJ 2112 Optimal Milking (二分 + floyd + 网络流)

标签:poj   网络流   二分   floyd   

原文地址:http://blog.csdn.net/sio__five/article/details/38876281

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!