博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2112 Optimal Milking (Dinic + Floyd + 二分)
阅读量:5054 次
发布时间:2019-06-12

本文共 4636 字,大约阅读时间需要 15 分钟。

                          
Optimal Milking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 19456   Accepted: 6947
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 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和二分,可是就做不下去了,因为不知道怎么把二分的值,应用到图里面。没想到我竟然如此地菜呀。 这题要用到网络流。 先跑一个Floyd,求出各点的最短路。然后,二分答案,假如现在的二分值为mid,那么我们建立一个新图,图的边有以下部分(原图的标号是1->k+c): 1.原图(Floyd之后)距离小于mid,并且,起点是奶牛,终点是收奶机的边,每条边权值为1。 2.源点,也是就0号点,到每个奶牛的边,权值为1 3.每个收奶机,到汇点,也就是c+k+1点的边,每条边的权值为m 其中,源点与汇点都不是原图中存在的点。 然后,求最大流,判断最大流是否小于奶牛数。 代码:
#include
#include
#include
#include
using namespace std;int n,k,c,m;bool vis[300];int num[300];bool outflag;struct node{ int v; int ser;};int mp[300][300];const int inf = 99999999;int mmp[300][300];void init(){ scanf("%d%d%d",&k,&c,&m); n=k+c; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&mp[i][j]); if(i!=j&&mp[i][j]==0){mp[i][j]=inf;} } }}int floyd(){ for(int k=1;k<=n;k++){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(mp[i][j]>mp[i][k]+mp[k][j]){ mp[i][j]=mp[i][k]+mp[k][j]; } } } }}void build(int maxn){ memset(mmp,0,sizeof(mmp)); for(int i=k+1;i<=n;i++){ mmp[0][i]=1; } for(int i=1;i<=k;i++){ mmp[i][n+1]=m; } for(int i=k+1;i<=n;i++){ for(int j=1;j<=k;j++){ if(mp[i][j]<=maxn){ mmp[i][j]=1; } } }}bool bfs(int s,int t){ queue
q; memset(vis,0,sizeof(vis)); q.push(node{s,1}); node cur;vis[s]=true; while(!q.empty()){ cur=q.front();q.pop(); num[cur.v]=cur.ser; vis[cur.v]=true; for(int i=0;i<=n+1;i++){ if(mmp[cur.v][i]>0&&!vis[i]){ q.push(node{i,cur.ser+1}); } } } if(num[t]){return true;} else return false;}int dfs(int s,int t,int f){ if(s==t){return f;} vis[s]=true; int d; for(int i=0;i<=n+1;i++){ if(!vis[i]&&mmp[s][i]>0&&num[s]==num[i]-1){ d=dfs(i,t,min(f,mmp[s][i])); mmp[s][i]-=d; mmp[i][s]+=d; if(d!=0){return d;} } } return 0;}int dinic(){ memset(num,0,sizeof(num)); int ans=0; while(bfs(0,n+1)){ int d; memset(vis,0,sizeof(vis)); while(d=dfs(0,n+1,inf)){ ans+=d; memset(vis,0,sizeof(vis)); } memset(num,0,sizeof(num)); } return ans;}int solve(){ int l=0,r=inf,mid; while(r>=l){ mid=(r+l)>>1; if(mid==2){outflag=1;} build(mid); if(dinic()>=c){ r=mid-1; } else{ l=mid+1; } } return l;}int main(){ init(); floyd(); printf("%d\n",solve());}

  

 

转载于:https://www.cnblogs.com/ZGQblogs/p/9403982.html

你可能感兴趣的文章
HTML列表,表格与媒体元素
查看>>
PHP、Java、Python、C、C++ 这几种编程语言都各有什么特点或优点?
查看>>
感谢青春
查看>>
Jquery Uploadify4.2 falsh 实现上传
查看>>
雨林木风 GHOST_XP SP3 快速装机版YN12.08
查看>>
linux基础-命令
查看>>
java对象的深浅克隆
查看>>
Hadoop流程---从tpch到hive
查看>>
数据结构3——浅谈zkw线段树
查看>>
Introduction to my galaxy engine 2: Depth of field
查看>>
shell判断网络主机存活
查看>>
根据时间戳,增量同步数据的解决办法
查看>>
03 SeekBar 音频播放拖拽进度条
查看>>
自定义view实现阻尼效果的加载动画
查看>>
清北学堂的小技巧和小收获
查看>>
模型压缩方向一个很牛的paper
查看>>
Android--AsyncTask异步加载详解
查看>>
YARN学习总结
查看>>
C#基础温习(2):温习控制台程序(二)
查看>>
一些文章
查看>>