POJ2112 Optimal Milking 【最大流+二分】

Optimal Milking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 12482   Accepted: 4508
Case Time Limit: 1000MS

Description

FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C. 

Each milking point can "process" at most M (1 <= M <= 15) cows each day. 

Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine. 

Input

* Line 1: A single line with three space-separated integers: K, C, and M. 

* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line. 

Output

A single line with a single integer that is the minimum possible total distance for the furthest walking cow. 

Sample Input

2 3 2
0 3 2 1 1
3 0 3 2 0
2 3 0 1 0
1 2 1 0 2
1 0 0 2 0

Sample Output

2

Source

USACO 2003 U S Open

题意:有k台挤奶器,每台挤奶器最多容纳m头奶牛,该牧场共有c头奶牛,现在给定这k台机器和c头奶牛相互间的直接距离,求让所有奶牛到达挤奶器且满足该条件时奶牛走到挤奶器间的最大距离的最小值。

题解:构图:先用Floyd求出相互间的最短距离,然后设置源点到每头牛的距离为1,每台机器到汇点的距离为m,然后若牛到机器的距离不大于maxdist,那么则将该边加入到新图中,最后对新图求最大流,判断最大流是否等于c,就这样二分枚举maxdist直到找到最小的maxdist为止。

#include <stdio.h>
#include <string.h>
#define inf 0x3fffffff
#define maxn 235

int dist[maxn][maxn], k, c, m, n;
int G[maxn][maxn], Layer[maxn];
int queue[maxn], maxDist;
bool vis[maxn];

void Floyd() {
    int x, i, j;
    maxDist = 200;
    for(x = 1; x <= n; ++x)
        for(i = 1; i <= n; ++i)
            for(j = 1; j <= n; ++j)
                if(dist[i][j] > dist[i][x] + dist[x][j]) {
                    dist[i][j] = dist[i][x] + dist[x][j];
                    if(maxDist < dist[i][j]) maxDist = dist[i][j];
                }
}

void build(int flow) {
    memset(G, 0, sizeof(G));
    int i, j;
    for(i = k + 1; i <= n; ++i) {
        G[0][i] = 1;
        for(j = 1; j <= k; ++j)
            if(dist[i][j] <= flow)
                G[i][j] = 1;
    }
    for(j = 1; j <= k; ++j)
        G[j][n + 1] = m;
}

bool countLayer() {
    int id = 0, front = 0, now, i;
    memset(Layer, 0, sizeof(Layer));
    Layer[0] = 1; queue[id++] = 0;
    while(front < id) {
        now = queue[front++];
        for(i = 0; i <= n + 1; ++i)
            if(G[now][i] && !Layer[i]) {
                Layer[i] = Layer[now] + 1;
                if(i == n + 1) return true;
                else queue[id++] = i;
            }
    }
    return false;
}

bool Dinic() {
    int i, maxFlow = 0, id = 0, now, minCut, pos, u, v;
    while(countLayer()) {
        memset(vis, 0, sizeof(vis));
        vis[0] = 1; queue[id++] = 0;
        while(id) {
            now = queue[id - 1];
            if(now == n + 1) {
                minCut = inf;
                for(i = 1; i < id; ++i) {
                    u = queue[i - 1];
                    v = queue[i];
                    if(G[u][v] < minCut) {
                        minCut = G[u][v];
                        pos = u;
                    } 
                }
                maxFlow += minCut;
                for(i = 1; i < id; ++i) {
                    u = queue[i - 1];
                    v = queue[i];
                    G[u][v] -= minCut;
                    G[v][u] += minCut;
                }
                while(id && queue[id - 1] != pos)
                    vis[queue[--id]] = 0;
            } else {
                for(i = 0; i <= n + 1; ++i) {
                    if(G[now][i] && !vis[i] && Layer[now] + 1 == Layer[i]) {
                        queue[id++] = i;
                        vis[i] = 1; break;
                    }
                }
                if(i > n + 1) --id;
            }
        }
    }
    return maxFlow == c;
}

int binarySolve() {
    int left = 0, right = maxDist, mid;
    while(left < right) {
        mid = (left + right) >> 1;
        build(mid);
        if(Dinic()) right = mid;
        else left = mid + 1;
    }
    return left;
}

int main() {
    //freopen("stdin.txt", "r", stdin);
    int i, j;
    while(scanf("%d%d%d", &k, &c, &m) == 3) {
        for(i = 1, n = k + c; i <= n; ++i)
            for(j = 1; j <= n; ++j) {
                scanf("%d", &dist[i][j]);
                if(!dist[i][j] && i != j)
                    dist[i][j] = inf;
            }
        Floyd();
        printf("%d\n", binarySolve());
    }
    return 0;
}


2015.4.20

#include <iostream>
#include <cstring>

using namespace std;

const int maxn = 235;
const int inf = 0x3f3f3f3f;
int G[maxn][maxn], K, C, M;
int G0[maxn][maxn];

int min(int a, int b) { return a < b ? a : b; }
int Dinic(int s, int t);

void Floyd(int dist[][maxn], int n) {
    int i, j, k;
    for (k = 1; k <= n; ++k)
        for (i = 1; i <= n; ++i)
            for (j = 1; j <= n; ++j)
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}

void getMap()
{
    int i, j, n = K + C;
    for (i = 1; i <= n; ++i)
        for (j = 1; j <= n; ++j) {
            cin >> G0[i][j];
            if (G0[i][j] == 0 && i != j)
                G0[i][j] = inf;
        }
    Floyd(G0, n);
}

bool binary_search(int maxDist)
{
    memset(G, 0, sizeof(G));
    int i, j, s = 0, t = K + C + 1;
    for (i = 1; i <= K; ++i) {
        G[s][i] = M;
        for (j = K + 1; j < t; ++j)
            if (G0[i][j] <= maxDist)
                G[i][j] = 1;
    }
    for (i = K + 1; i < t; ++i)
        G[i][t] = 1;

    return Dinic(s, t) == C;
}

int queue[maxn];
bool vis[maxn]; int Layer[maxn];
bool countLayer(int s, int t) {
    memset(Layer, 0, sizeof(Layer));
    int id = 0, front = 0, now, i; 
    Layer[s] = 1; queue[id++] = s;
    while(front < id) {
        now = queue[front++];
        for(i = s; i <= t; ++i)
            if(G[now][i] && !Layer[i]) {
                Layer[i] = Layer[now] + 1;
                if(i == t) return true;
                else queue[id++] = i;
            }
    }
    return false;
}
// 源点,汇点,源点编号必须最小,汇点编号必须最大
int Dinic(int s, int t) {
    int minCut, pos, maxFlow = 0;
    int i, id = 0, u, v, now;
    while(countLayer(s, t)) {
        memset(vis, 0, sizeof(vis));
        vis[s] = true; queue[id++] = s;
        while(id) {
            now = queue[id - 1];
            if(now == t) {
                minCut = inf;
                for(i = 1; i < id; ++i) {
                    u = queue[i - 1];
                    v = queue[i];
                    if(G[u][v] < minCut) {
                        minCut = G[u][v];
                        pos = u;
                    }
                }
                maxFlow += minCut;
                for(i = 1; i < id; ++i) {
                    u = queue[i - 1];
                    v = queue[i];
                    G[u][v] -= minCut;
                    G[v][u] += minCut;
                }
                while(queue[id - 1] != pos)
                    vis[queue[--id]] = false;
            } else {
                for(i = s; i <= t; ++i) {
                    if(G[now][i] && Layer[now] + 1 == Layer[i] && !vis[i]) {
                        vis[i] = 1; queue[id++] = i; break;
                    }
                }
                if(i > t) --id;
            }
        }
    }
    return maxFlow;
}


int solve()
{
    int leftDist = 0, rightDist = inf, midDist;
    while (leftDist < rightDist) {
        midDist = leftDist + rightDist >> 1;
        if (binary_search(midDist)) rightDist = midDist;
        else leftDist = midDist + 1;
    }
    cout << leftDist << endl;
}

int main()
{
    while (cin >> K >> C >> M) {
        getMap();
        solve();
    }
    return 0;
}



你可能感兴趣的:(POJ2112)