POJ2112_Optimal Milking(网洛流最大流Dinic+最短路Flody+二分)
来源:互联网 发布:图片尺寸编辑软件 编辑:程序博客网 时间:2024/05/21 06:59
解题报告
农场有k个挤奶机和c头牛,每头牛到每一台挤奶机距离不一样,每台挤奶机每天最多挤m头牛的奶。
寻找一个方案,安排每头牛到某一挤奶机挤奶,使得c头牛需要走的所有路程中的最大路程的最小值。
要使每一头牛都去挤奶,那么建完模型就要判断是否满流。
由于是多源多点的网络,假设源点0,汇点n+1(n=k+c)
源点到每一头牛的容量为1,每一台机器到汇点的容量为m;用flody求出任意一头牛到任意一台机器的最短路;
对于取最大距离的最小值可以用二分来找。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define inf 99999999#define K 33#define C 210using namespace std;int k,c,m,mmap[K+C][K+C],l[K+C],edge[K+C][K+C],n;void flody(){ for(int l=1; l<=n; l++) { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(mmap[i][j]>mmap[i][l]+mmap[l][j]) mmap[i][j]=mmap[i][l]+mmap[l][j]; } } }}int bfs(){ memset(l,-1,sizeof(l)); queue<int>Q; Q.push(0); l[0]=0; while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=0; i<=n+1; i++) { if(edge[u][i]&&l[i]==-1) { l[i]=l[u]+1; Q.push(i); } } } if(l[n+1]>0)return 1; else return 0;}void G(int mid){ int i,j; memset(edge,0,sizeof(edge)); for(i=1; i<=k; i++) edge[i][n+1]=m; for(i=k+1; i<=n; i++) edge[0][i]=1; for(i=k+1; i<=n; i++) { for(j=1; j<=k; j++) { if(mmap[i][j]<=mid) edge[i][j]=1; else edge[i][j]=0; } }}int dfs(int x,int f){ int a; if(x==n+1)return f; for(int i=0; i<=n+1; i++) { if(edge[x][i]&&l[i]==l[x]+1&&(a=dfs(i,min(f,edge[x][i])))) { edge[x][i]-=a; edge[i][x]+=a; return a; } } return 0;}int dinic(int mid){ int ans=0,a; G(mid); while(bfs()) while(a=dfs(0,inf)) ans+=a; return ans;}int main(){ int i,j; while(~scanf("%d%d%d",&k,&c,&m)) { n=k+c; for(i=1; i<=n; i++) { for(j=1; j<=n; j++) { scanf("%d",&mmap[i][j]); if(!mmap[i][j]) mmap[i][j]=inf; } } flody(); int L=0,R=20000; while(L<R) { int mid=(L+R)/2; int ans=dinic(mid); if(ans>=c)R=mid; else L=mid+1; } printf("%d\n",R); } return 0;}
Optimal Milking
Time Limit: 2000MS Memory Limit: 30000KTotal Submissions: 11664 Accepted: 4238Case 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.
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.
* 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
1 0
- POJ2112_Optimal Milking(网洛流最大流Dinic+最短路Flody+二分)
- poj 2112 Optimal Milking, 二分,最短路,最大流
- 【最大流+最短路+二分】POJ-2112 Optimal Milking
- POJ 2112 Optimal Milking (二分+最短路+最大流or二分图多重匹配)
- poj 2112 Optimal Milking 【最大流 简单题】 【floyd预处理最短路 + 二分 + 最大流】
- 【最大流+floyd+二分+dinic】北大 poj 2112 Optimal Milking
- 【最大流+dinic+二分】北大 poj 2455 Secret Milking Machine
- POJ 2112 Optimal Milking(最大流-Dinic+Floyd+二分)
- Poj 2112 Optimal Milking【Floyd+二分+最大流Dinic】
- POJ 2112 Optimal Milking【网络流+二分+最短路】
- POJ2112Optimal Milking(二分+floyd最短路+网络流)
- poj 2112 Optimal Milking 二分最短路网络流
- POJ 2112--Optimal Milking【二分找最大距离的最小值 && 最大流dinic】
- Flody-最短路
- HDU 5889 Barricade(最短路spfa+最大流dinic)
- [最小割最大流 || 最短路] roadblock Dinic && SPFA + SLE
- Optimal Milking-最大流/FordFulkerson/Dinic
- poj 2112 Optimal Milking(最大费用流+dinic算法+二分搜索)
- 埃拉托色尼筛选法 算法
- SRM Rfx(寻源)的含义及相应的增强
- OpenStack基于Libvirt的虚拟化平台调度实现----Nova虚拟机启动源码实现(3)
- 现在的一切都可以忍
- LATTICE FPGA IO 约束设置 初探
- POJ2112_Optimal Milking(网洛流最大流Dinic+最短路Flody+二分)
- jquery easyui combobox
- 最好用的浏览器
- JavaWeb之Servlet课堂笔记[自己总结的]
- Google翻译PDF文档
- 如何用Maven创建web项目(具体步骤)
- 将后台数据转换成 JS Tree 的形式----------工作问题003
- MD5加密
- strcpy函数的实现