Optimal Milking POJ - 2112 (二分+floyd+最大流)

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

题目大意: 一个机器能容纳m个奶牛,每个奶牛到机器有个距离,问奶牛到机器距离最大值的最小值。
可把奶牛和机器看成结点,添加源点到汇点,源点到奶牛流量为1,机器到汇点流量为m。假定一个最大距离,若奶牛到机器的距离小于这个最大距离则加一条流量为1的边,求最大流,若最大流为C,那么减小最大距离,否则增加,不断二分。
AC代码:

#include<iostream>
#include<cstring>
#include<string>
#include<deque>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 300;
const int inf = 0x3f3f3f3f;
int visit[N], layer[N];
int k, c,m, n;
int g[N][N], dist[N][N];
void floyd()
{
    for (int m = 1; m <= k + c; m++)
    {
        for (int i = 1; i <= k + c; i++)
        {
            for (int j = 1; j <= k + c; j++)
            {
                dist[i][j] = min(dist[i][j], dist[i][m] + dist[m][j]);
            }
        }
    }

}
bool layering() //进行分层
{
    deque<int> q;//双向队列
    memset(layer, -1, sizeof(layer));
    q.push_back(0);
    layer[0] = 0;
    while (!q.empty())
    {
        int front = q.front();
        q.pop_front();
        for (int i = 0; i <= n+1; i++)
        {
            if (layer[i] == -1 && g[front][i]>0)
            {
                layer[i] = layer[front] + 1;
                if (i == n+1) return true;
                else    q.push_back(i);
            }
        }
    }
    return false;
}
int dinic()
{
    int maxflow = 0;
    deque<int> q;
    while (layering()) 
    {
        memset(visit, 0, sizeof(visit));
        q.push_back(0), visit[0] = 1;
        while (!q.empty())
        {
            int front = q.back();
            if (front == n+1) //如果该点是汇点
            {
                int minflow = inf, minflow_no;// 最小流以及最小流的起始点
                for (int i = 1; i < (int)q.size(); i++) //在双向队列里寻找
                {
                    int s = q[i - 1], e = q[i];
                    if (g[s][e] < minflow&&g[s][e]>0) minflow = g[s][e], minflow_no = s;
                }
                maxflow += minflow;
                for (int i = 1; i < (int)q.size(); i++) //添加反向边
                {
                    int s = q[i - 1], e = q[i];
                    g[s][e] -= minflow, g[e][s] += minflow;
                }
                while (!q.empty()&&q.back()!=minflow_no) //回溯到最小流的起始点或栈顶
                {
                    visit[q.back()] = 0;
                    q.pop_back();
                }
            }
            else  //如果不是汇点则继续向下寻找
            {
                int i;
                for ( i = 0; i <= n+1; i++)
                {
                    if (g[front][i] > 0 && layer[i] == layer[front] + 1 && !visit[i])
                    {
                        visit[i] = 1;
                        q.push_back(i);
                        break;
                    }
                }
                    if (i > n+1) //如果没找到则回溯
                    {
                        q.pop_back();
                    }

            }
        }
    }
    return maxflow;
}
int main()
{
    while (cin>>k>>c>>m)
    {
        n = k + c;
        for (int i = 1; i <= k + c; i++)
        {
            for (int j = 1; j <= k + c; j++)
            {
                cin >> dist[i][j];
                if (dist[i][j] == 0&&i!=j) dist[i][j] = inf;
            }
        }
        floyd();
        int left = 0, right = 1000000;
        while (left<right)
        {
            memset(g, 0, sizeof(g));
            int mid = (left + right) / 2;
            for (int i = k+1; i <= k+c; i++) g[0][i] = 1;//源点到奶牛流量为1
            for (int i = 1; i <= k; i++) g[i][n + 1] = m; //机器到源点流量为m
            for (int i = k+1; i <= k + c; i++)
            {
                for (int j = 1; j <= k; j++)
                {
                    if (dist[i][j] <= mid) g[i][j] = 1;
                }
            }
            if (dinic() >= c) right = mid;
            else left = mid + 1;
        }
        cout << right << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinduo16/article/details/81670020
今日推荐