Optimal Milking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 16550   Accepted: 5945
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

 
 

水平太差,做这个题可谓是历经千辛万苦,差不多做了两天多。开始想要偷懒,一次见图,再DINIC种根据边的大小进行判断是否可以搜索,失败!然后改为每次建图,然后dinic,然后就OK了,提交的时候有出了个低级失误,在POJ提交时用的C++,结果提示“

Main.cpp
xlocale(1242) : fatal error C1088: Cannot flush compiler intermediate file: '_CL_75c8ace5ex': No space left on device

”磁盘空间不足,但是就懵了,尝试了N次才发现,我的智商啊!!!!

 
题目:floyd算出牛和挤奶器相互之间的最短距离,二分答案用最大流进行判断,如果最大流==牛数就可能是答案,否则不是。
代码如下:
  1 Source Code
2 #include<cstdio>
3 #include<iostream>
4 #include<cstring>
5 #include<vector>
6 #include<queue>
7
8 using namespace std;
9 int k,c,m,ans;
10 int map[240][240];
11 int tu[240][240];
12 int lays[240];
13 int vis[240];
14 void floyd()
15 {
16 for(int kk=1;kk<=k+c;kk++)
17 for(int i=1;i<=k+c;i++)
18 for(int j=1;j<=k+c;j++)
19 if(map[i][kk]+map[kk][j]<map[i][j])
20 map[i][j]=map[i][kk]+map[kk][j];
21 }
22 void mideg(int mid)
23 {
24 memset(tu,0,sizeof(tu));
25 for(int i=1;i<=k;i++)
26 {
27 tu[0][i]=m;
28 map[0][i]=1;
29 }
30 for(int i=1;i<=k;i++)
31 {
32 for(int j=k+1;j<=k+c;j++)
33 if(map[i][j]<=mid)
34 tu[i][j]=1;
35 }
36 for(int i=k+1;i<=k+c;i++)
37 {
38 tu[i][k+c+1]=1;
39 map[i][k+c+1]=1;
40 }
41 }
42 bool bfs()
43 {
44 memset(lays,-1,sizeof(lays));
45 queue<int>q;
46 q.push(0);
47 lays[0]=0;
48 while(!q.empty())
49 {
50 int u=q.front();
51 q.pop();
52 for(int i=0;i<=k+c+1;i++)
53 {
54 if(tu[u][i]>0&&lays[i]==-1)
55 {
56 lays[i]=lays[u]+1;
57 if(i==k+c+1)return 1;
58 else q.push(i);
59 }
60 }
61 }
62 return 0;
63 }
64 bool dinic()
65 {
66 int maxf=0;
67 vector<int>q;
68 while(bfs())
69 {
70 memset(vis,0,sizeof(vis));
71 q.push_back(0);
72 vis[0]=1;
73 while(!q.empty())
74 {
75 int nd=q.back();
76 if(nd==k+c+1)
77 {
78 int minn,minx=0x7fffffff;
79 for(int i=1;i<q.size();i++)
80 {
81 int u=q[i-1],v=q[i];
82 if(minx>tu[u][v])
83 {
84 minx=tu[u][v];
85 minn=u;
86 }
87 }
88 maxf+=minx;
89 for(int i=1;i<q.size();i++)
90 {
91 int u=q[i-1],v=q[i];
92 tu[u][v]-=minx;
93 tu[v][u]+=minx;
94 }
95 while(!q.empty()&&q.back()!=minn)
96 {
97 vis[q.back()]=0;
98 q.pop_back();
99 }
100 }
101 else
102 {
103 int i;
104 for(i=0;i<=k+c+1;i++)
105 {
106 if(tu[nd][i]>0&&!vis[i]&&lays[i]==lays[nd]+1)
107 {
108 q.push_back(i);
109 vis[i]=1;
110 break;
111 }
112 }
113 if(i>k+c+1)q.pop_back();
114 }
115 }
116 }
117 return maxf==c;
118 }
119 int main()
120 {
121
122 cin>>k>>c>>m;
123 memset(map,0x3f,sizeof(map));
124 for(int i=1;i<=k+c;i++)
125 for(int j=1;j<=k+c;j++)
126 {
127 int a;
128 scanf("%d",&a);
129 if(a)map[i][j]=a;
130 if(i==j)map[i][j]=0;
131 }
132
133 floyd();
134
135 int l=0,r=50000;
136 while(l<=r)
137 {
138 int mid=(l+r)/2;
139 mideg(mid);
140 bool pd=dinic();
141 if(pd)
142 {
143 ans=mid;
144 r=mid-1;
145 }
146 else
147 l=mid+1;
148 }
149 cout<<ans<<endl;
150 return 0;
151 }

poj 2112 最优挤奶方案的更多相关文章

  1. 题解 最优的挤奶方案(Optimal Milking)

    最优的挤奶方案(Optimal Milking) 时间限制: 1 Sec  内存限制: 128 MB 题目描述 农场主 John 将他的 K(1≤K≤30)个挤奶器运到牧场,在那里有 C(1≤C≤20 ...

  2. POJ 2112 Optimal Milking (二分 + floyd + 网络流)

    POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...

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

    Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Sub ...

  4. Poj 2112 Optimal Milking (多重匹配+传递闭包+二分)

    题目链接: Poj 2112 Optimal Milking 题目描述: 有k个挤奶机,c头牛,每台挤奶机每天最多可以给m头奶牛挤奶.挤奶机编号从1到k,奶牛编号从k+1到k+c,给出(k+c)*(k ...

  5. POJ 2112 Optimal Milking (二分 + 最大流)

    题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...

  6. POJ 2112 Optimal Milking (二分+最短路径+网络流)

    POJ  2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K To ...

  7. P3097 [USACO13DEC]最优挤奶Optimal Milking

    P3097 [USACO13DEC]最优挤奶Optimal Milking 题意简述:给定n个点排成一排,每个点有一个点权,多次改变某个点的点权并将最大点独立集计入答案,输出最终的答案 感谢@zht4 ...

  8. 洛谷P3097 - [USACO13DEC]最优挤奶Optimal Milking

    Portal Description 给出一个\(n(n\leq4\times10^4)\)个数的数列\(\{a_n\}(a_i\geq1)\).一个数列的最大贡献定义为其中若干个不相邻的数的和的最大 ...

  9. Optimal Milking POJ - 2112 (多重最优匹配+最小费用最大流+最大值最小化 + Floyd)

      Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 19347   Accepted: 690 ...

随机推荐

  1. 【目标检测】基于传统算法的目标检测方法总结概述 Viola-Jones | HOG+SVM | DPM | NMS

    "目标检测"是当前计算机视觉和机器学习领域的研究热点.从Viola-Jones Detector.DPM等冷兵器时代的智慧到当今RCNN.YOLO等深度学习土壤孕育下的GPU暴力美 ...

  2. Spring Boot中初始化资源的几种方式

    CommandLineRunner 定义初始化类 MyCommandLineRunner 实现 CommandLineRunner接口,并实现它的 run()方法,在该方法中编写初始化逻辑 注册成Be ...

  3. Maven安装配置和IDEA创建Maven项目

    maven 一个项目架构管理工具(约定大于配置) 1.配置 M2_HOME:指向maven bin目录 以后bootstrop要用 MAVEN_HOME:指向maven目录 path:指向maven ...

  4. VoltDB成功入选CNCF Landscape云原生数据库全景图

    近日,VoltDB正式入选 CNCF Landscape(可能是目前其中唯一的关系型分布式内存数据库).此次VoltDB 进入 CNCF Landscape,意味着 VoltDB 正式成为了 CNCF ...

  5. 关于.NET中的控制反转(二)- 依赖注入之 MEF

    一.MEF是什么 Managed Extensibility Framework (MEF) 是用于创建可扩展的轻量级应用程序的库. 它让应用程序开发人员得以发现和使用扩展且无需配置. 它还让扩展开发 ...

  6. 织梦dedecms自增变量autoindex标签的使用(转)

    织梦dedecms自增变量autoindex标签的使用 例1: {dede:arclist titlelen='120' row='8' typeid='2'}         <li clas ...

  7. Redis集群搭建与简单使用【转】

    Redis集群搭建与简单使用 安装环境与版本 用两台虚拟机模拟6个节点,一台机器3个节点,创建出3 master.3 salve 环境. redis 采用 redis-3.2.4 版本. 两台虚拟机都 ...

  8. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  9. 🎉 Element UI for Vue 3.0 来了!

    第一个使用 TypeScript + Vue 3.0 Composition API 重构的组件库 Element Plus 发布了 ~ 2016 年 3 月 13 日 Element 悄然诞生,经历 ...

  10. pandas 写csv 操作

    pandas 写csv 操作 def show_history(self): df = pd.DataFrame() df['Time'] = pd.Series(self.time_hist) df ...