POJ 2112--Optimal Milking【二分找最大距离的最小值 && 最大流dinic】

来源:互联网 发布:触摸屏幕软件 编辑:程序博客网 时间:2024/05/18 03:05

Optimal Milking
Time Limit: 2000MS Memory Limit: 30000KTotal Submissions: 13897 Accepted: 5018Case 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 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0

Sample Output

2

 

上面图片有详细的题意和解析,来自图论算法理论,实现及应用这本书。


先用floyd求解任意两点之间的最短距离,再构建网络,将牛与源点相连容量为1(则从原点出去的中流量为C),将挤奶器与汇点相连容量为m。然后用dinic算法求最大流,二分枚举答案,将牛与挤奶器之间的距离小于枚举值得边 加入网络,大于枚举值得边去掉。找到可以使汇点的流入量为C的且符合条件的最小值就是答案。

#include <cstdio>#include <cstring>#include <iostream>#include <vector>#include <queue>#include <algorithm>#define maxn 1100#define maxm 550000#define INF 1000000using namespace std;int dist[maxn], vis[maxn];int head[maxn], cur[maxn], cnt;int map[maxn][maxn];struct node{    int u, v, cap, flow, next;};node edge[maxm];int K, C, M, n;void init(){    cnt = 0;    memset(head, -1, sizeof(head));}void add(int u, int v, int w){    edge[cnt] = {u, v, w, 0, head[u]};    head[u] = cnt++;    edge[cnt] = {v, u, 0, 0, head[v]};    head[v] = cnt++;}void getmap(int min_max){    int i, j;    for(i = K + 1; i <= n; ++i)        add(0, i, 1);    for(i = 1; i <= K; ++i)        add(i, n + 1, M);    for(i = K + 1; i <= n; ++i)        for(j = 1; j <= K; ++j)        if(map[i][j] <= min_max) add(i, j, 1);}bool BFS(int st, int ed){    queue<int>q;    memset(vis, 0, sizeof(vis));    memset(dist, -1, sizeof(dist));    q.push(st);    vis[st] = 1;    dist[st] = 0;    while(!q.empty()){        int u = q.front();        q.pop();        for(int i = head[u]; i != -1; i = edge[i].next){            node E = edge[i];            if(!vis[E.v] && E.cap > E.flow){                vis[E.v] = 1;                dist[E.v] = dist[u] + 1;                if(E.v == ed) return true;                q.push(E.v);            }        }    }    return false;}int DFS(int x, int ed, int a){    if(a == 0 || x == ed)        return a;    int flow = 0, f;    for(int &i = cur[x]; i != -1; i =edge[i].next){        node &E = edge[i];        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){            E.flow += f;            edge[i ^ 1].flow -= f;            flow += f;            a -= f;            if(a == 0) break;        }    }    return flow;}int maxflow (int st, int ed){    int flowsum  = 0;    while(BFS(st, ed)){        memcpy(cur, head, sizeof(head));        flowsum += DFS(st, ed, INF);    }    return flowsum;}int main (){    while(scanf("%d%d%d", &K,&C,&M) != EOF){        n = C + K;        int i, j , k;        for(i = 1; i <= n; ++i)        for(j = 1; j <= n; ++j){            scanf("%d", &map[i][j]);            if(map[i][j] == 0) map[i][j] = INF;        }        for(k = 1; k <= n; ++k)            for(i = 1; i <= n; ++i)                if(map[i][k] != INF){                    for(j = 1; j <= n; ++j)                        map[i][j] = min(map[i][k] + map[k][j], map[i][j]);            }        int L = 0, R = 60000, mid, ans;        while( R > L){            mid = (L + R) / 2;            ans = 0;            init();            getmap(mid);            ans = maxflow(0, n + 1);            if(ans >= C) R = mid;            else L = mid + 1;        }        printf("%d\n", R);    }    return 0;}

1 0
原创粉丝点击