# [POJ 2112] Optimal Milking【Floyd+二分答案+网络流】

• 2018-01-15
• 0
• 0

## Problem:

 Time Limit: 2000MS Memory Limit: 30000K 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

## Solution:

Dinic 的写法有两种，一种是 1 次 BFS 进行多次 DFS，每次 DFS 找出 1 条增广路，详见 Code #1；另一种是 1 次 BFS 进行 1 次 DFS ，每次 DFS 找出当前所有增广路，详见 Code #2。理论上后者效率略高，但相差不大。

## Code #1: O(V3+V2ElogD), D为最大距离 [596K, 141MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define SOURCE 0
#define SINK 231
using namespace std;

int K, C, M;
int ent, dis[233][233];

struct Edge{
int np, flow, cap;
Edge *nxt, *rev;
};

struct Graph{
Edge *V[233], E[13000];
int tope;

inline void clear() {tope = 0, memset(V, 0, sizeof(V));}

inline void addedge(int u, int v, int cap){
E[++tope].np = v, E[tope].flow = 0, E[tope].cap = cap;
E[tope].nxt = V[u], E[tope].rev = &E[tope + 1], V[u] = &E[tope];
E[++tope].np = u, E[tope].flow = 0, E[tope].cap = 0;
E[tope].nxt = V[v], E[tope].rev = &E[tope - 1], V[v] = &E[tope];
}
} G;  // V[0] is source, V[1..30] are machines, V[31..230] are cows, V[231] is sink

struct Queue{
#define inc(x) (x) = ((x) < 233 ? (x) + 1 : (x) - 233)
int node[234];
int fr, re;

inline void clear() {fr = re = 0;}

inline bool empty() {return fr == re;}

inline void push(const int &x) {node[re] = x, inc(re);}

inline void pop() {inc(fr);}

inline int front() {return node[fr];}
} q;

inline void Floyd(){
for(register int k = 1; k <= ent; k++)
for(register int i = 1; i <= ent; i++)
for(register int j = 1; j <= ent; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}

int lev[233];
Edge *arc[233];

inline bool Dinic_BFS(){
memset(lev, -1, sizeof(lev)), lev[SOURCE] = 0;
q.clear(), q.push(SOURCE);
while(!q.empty()){
int u = q.front(); q.pop();
for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
if(ne->cap > ne->flow && lev[ne->np] == -1)
lev[ne->np] = lev[u] + 1, q.push(ne->np);
}
return lev[SINK] > -1;
}

inline int Dinic_DFS(int u, int curflow){
if(u == SINK) return curflow;
for(register Edge *ne = arc[u]; ne; ne = ne->nxt){
arc[u] = ne;
if(lev[ne->np] == lev[u] + 1 && ne->cap > ne->flow){
int dflow = Dinic_DFS(ne->np, min(curflow, ne->cap - ne->flow));
if(dflow){
ne->flow += dflow, ne->rev->flow -= dflow;
return dflow;
}
}
}
return 0;
}

inline int Dinic(){
int maxflow = 0, flow;
while(Dinic_BFS()){
for(register int i = SOURCE; i <= SINK; i++) arc[i] = G.V[i];
while(flow = Dinic_DFS(0, 0x3f3f3f3f))
maxflow += flow;
}
return maxflow;
}

inline bool check(int key){
G.clear();
for(register int i = 1; i <= K; i++) G.addedge(SOURCE, i, M);
for(register int i = 1; i <= C; i++) G.addedge(i + 30, SINK, 1);
// The static edges must be reconstructed because the member "flow" may be modified !!!
for(register int i = 1; i <= K; i++)
for(register int j = 1; j <= C; j++)
if(dis[i][j + K] <= key) G.addedge(i, j + 30, 1);
int maxflow = Dinic();
return maxflow == C;
}

int main(){
scanf("%d%d%d", &K, &C, &M), ent = K + C;
for(register int i = 1; i <= ent; i++)
for(register int j = 1; j <= ent; j++){
scanf("%d", &dis[i][j]);
if(!dis[i][j] && i != j) dis[i][j] = 0x3f3f3f3f;
}
Floyd();
int lft = 0, rt = 200 * ent;
while(lft < rt){
int mid = lft + rt >> 1;
if(check(mid)) rt = mid;
else lft = mid + 1;
}
printf("%d\n", lft);
return 0;
}
```

## Code #2: O(V3+V2ElogD), D为最大距离 [596K, 141MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define SOURCE 0
#define SINK 231
using namespace std;

int K, C, M;
int ent, dis[233][233];

struct Edge{
int np, flow, cap;
Edge *nxt, *rev;
};

struct Graph{
Edge *V[233], E[13000];
int tope;

inline void clear() {tope = 0, memset(V, 0, sizeof(V));}

inline void addedge(int u, int v, int cap){
E[++tope].np = v, E[tope].flow = 0, E[tope].cap = cap;
E[tope].nxt = V[u], E[tope].rev = &E[tope + 1], V[u] = &E[tope];
E[++tope].np = u, E[tope].flow = 0, E[tope].cap = 0;
E[tope].nxt = V[v], E[tope].rev = &E[tope - 1], V[v] = &E[tope];
}
} G;  // V[0] is source, V[1..30] are machines, V[31..230] are cows, V[231] is sink

struct Queue{
#define inc(x) (x) = ((x) < 233 ? (x) + 1 : (x) - 233)
int node[234];
int fr, re;

inline void clear() {fr = re = 0;}

inline bool empty() {return fr == re;}

inline void push(const int &x) {node[re] = x, inc(re);}

inline void pop() {inc(fr);}

inline int front() {return node[fr];}
} q;

inline void Floyd(){
for(register int k = 1; k <= ent; k++)
for(register int i = 1; i <= ent; i++)
for(register int j = 1; j <= ent; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}

int lev[233];
Edge *arc[233];

inline bool Dinic_BFS(){
memset(lev, -1, sizeof(lev)), lev[SOURCE] = 0;
q.clear(), q.push(SOURCE);
while(!q.empty()){
int u = q.front(); q.pop();
for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
if(ne->cap > ne->flow && lev[ne->np] == -1)
lev[ne->np] = lev[u] + 1, q.push(ne->np);
}
return lev[SINK] > -1;
}

inline int Dinic_DFS(int u, int curflow){
if(u == SINK) return curflow;
int remflow = curflow;
for(register Edge *ne = arc[u]; ne; ne = ne->nxt){
arc[u] = ne;
if(lev[ne->np] == lev[u] + 1 && ne->cap > ne->flow){
int dflow = Dinic_DFS(ne->np, min(remflow, ne->cap - ne->flow));
if(dflow){
ne->flow += dflow, ne->rev->flow -= dflow;
remflow -= dflow;
}
}
}
return curflow - remflow;
}

inline int Dinic(){
int maxflow = 0;
while(Dinic_BFS()){
for(register int i = SOURCE; i <= SINK; i++) arc[i] = G.V[i];
maxflow += Dinic_DFS(0, 0x3f3f3f3f);
}
return maxflow;
}

inline bool check(int key){
G.clear();
for(register int i = 1; i <= K; i++) G.addedge(SOURCE, i, M);
for(register int i = 1; i <= C; i++) G.addedge(i + 30, SINK, 1);
// The static edges must be reconstructed because the member "flow" may be modified !!!
for(register int i = 1; i <= K; i++)
for(register int j = 1; j <= C; j++)
if(dis[i][j + K] <= key) G.addedge(i, j + 30, 1);
int maxflow = Dinic();
return maxflow == C;
}

int main(){
scanf("%d%d%d", &K, &C, &M), ent = K + C;
for(register int i = 1; i <= ent; i++)
for(register int j = 1; j <= ent; j++){
scanf("%d", &dis[i][j]);
if(!dis[i][j] && i != j) dis[i][j] = 0x3f3f3f3f;
}
Floyd();
int lft = 0, rt = 200 * ent;
while(lft < rt){
int mid = lft + rt >> 1;
if(check(mid)) rt = mid;
else lft = mid + 1;
}
printf("%d\n", lft);
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai