[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:

二分答案+网络流判可行性的模板题。

可以先用 Floyd 跑出两两之间的最短路,再二分最大的最小值,利用网络流的 Dinic 算法判断可行性。

构图时,从超级源点到每台挤奶机连一条容量为 M 的边,从挤奶机到与之距离小于当前二分的答案的奶牛连一条容量为 1 的边,再从每只奶牛到超级汇点连一条容量为 1 的边,比较最大流是否为奶牛总数 C 即可。

注意每次 check 时要重新建图,即使是静态边,其流量也有可能不为初始值。

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;
}

评论

还没有任何评论,你来说两句吧



常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
If the code cannot be displayed normally, please refresh the page.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai