【poj2122】 Optimal Milking

http://poj.org/problem?id=2112 (题目链接)

题意

  有K个能挤M头奶牛的挤奶机和C头奶牛,告诉一些挤奶机和奶牛间距离,求最优分配方案使最大距离最小。

Solution

  先Floyd跑出两两点之间的最短距离,二分答案,最大流。

细节

  注意距离不超过200是Floyd之前两点之间的距离不超过200。

代码

// poj2112
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 10000000
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;

const int maxn=500010;
struct edge {int to,next,w;}e[maxn<<1];
int head[maxn],d[maxn],f[300][300];
int n,m,cnt=1,es,et,C,K,M,ans;

void link(int u,int v,int w) {
	e[++cnt]=(edge){v,head[u],w};head[u]=cnt;
	e[++cnt]=(edge){u,head[v],w};head[v]=cnt;
}
void Floyd() {
	for (int k=1;k<=K+C;k++)
		for (int i=1;i<=K+C;i++)
			for (int j=1;j<=K+C;j++)
				f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
}
bool bfs() {
	memset(d,-1,sizeof(d));
	queue<int> q;q.push(es);d[es]=0;
	while (!q.empty()) {
		int x=q.front();q.pop();
		for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) {
				d[e[i].to]=d[x]+1;
				q.push(e[i].to);
			}
	}
	return d[et]>0;
}
int dfs(int x,int f) {
	if (x==et || f==0) return f;
	int w,used=0;
	for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) {
			w=dfs(e[i].to,min(e[i].w,f-used));
			used+=w;
			e[i].w-=w;e[i^1].w+=w;
			if (used==f) return used;
		}
	if (!used) d[x]=-1;
	return used;
}
bool Dinic(int mid) {
	int ans=0;memset(head,0,sizeof(head));
	cnt=1;
	for (int i=K+1;i<=K+C;i++) {
		for (int j=1;j<=K;j++) if (f[i][j]<=mid) link(i,j,1);
		link(es,i,1);
	}
	for (int i=1;i<=K;i++) link(i,et,M);
	while (bfs()) ans+=dfs(es,inf);
	return ans==C;
}
int main() {
	scanf("%d%d%d",&K,&C,&M);
	es=K+C+1;et=es+1;
	for (int i=1;i<=K+C;i++)
		for (int j=1;j<=K+C;j++) {
			scanf("%d",&f[i][j]);
			if (f[i][j]==0) f[i][j]=inf;
		}
	Floyd();
	int L=0,R=60000,res;
	while (L<=R) {
		int mid=(L+R)>>1;
		if (Dinic(mid)) res=mid,R=mid-1;
		else L=mid+1;
	}
	printf("%d",res);
	return 0;
}

  

时间: 2024-10-09 23:10:17

【poj2122】 Optimal Milking的相关文章

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【BZOJ4094】[Usaco2013 Dec]Optimal Milking 线段树

[BZOJ4094][Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号为1 ... N,并排成一行.第i台挤奶机每天能够挤M(i )单位的牛奶 (1 < =M(i) <=100,000).由于机器间距离太近,使得两台相邻的机器不能在同一天使用.Farmer Jo hn可以自由选择不同的机器集合在不同的日子进行挤奶.在D(1 < = D < = 50,00

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) co

POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】

Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 20

【POJ2185】【KMP + HASH】Milking Grid

Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that is R (1 <= R <= 10,000) rows by C (1 <= C <= 75) columns. As we all know, Farmer John is quite the expert on cow behavior, and is currently wri

【模拟】Codeforces 710B Optimal Point on a Line

题目链接: http://codeforces.com/problemset/problem/710/B 题目大意: 给N个点的坐标,在X轴上找到最靠左的点使得这个点到N个点距离之和最小. 题目思路: [模拟] 先将N个点坐标排序,找夹在i中间的坐标即为答案.(中间2个数选左边的) 从点坐标是X的点往左移到X+1,代价是X右边的坐标数-左边的坐标数.当X不是给定的坐标时答案是不会变得. 所以最终寻找的这个点一定是N个点中个某个点.并且就在正中中间的位置.如果正中间有两个数,那么这两个数之间的任何

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

图论常用算法之一 POJ图论题集【转载】

POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:http://poj.org/ 1062* 昂贵的聘礼 枚举等级限制+dijkstra 1087* A Plug for UNIX 2分匹配 1094 Sorting It All Out floyd 或 拓扑 1112* Team Them Up! 2分图染色+DP 1125 Stockbroker