题目链接 :

       【POJ】点击打开链接

       【caioj】点击打开链接

算法 :

1:跑一遍弗洛伊德,求出点与点之间的最短路径

2:二分答案,二分”最大值最小“

3.1:建边,将原点与每头奶牛连边,流量为1,记dist[i][j]为i到j的最短路径,若dist[i][j]<=mid (K+1<=i<=K+C,1<=j<=K),则将i与j连边,流量为M,将每台挤奶机与汇点连边,流量为1

3.2 : 跑网络流,这里笔者使用的是dinic算法

3.3 : 判断最大流S是否等于K,等于K,则往小搜,否则往大搜

代码 :

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXK 30
#define MAXC 200
#define MAXM 15 typedef long long LL; LL i,j,low,high,mid,st,ed,K,C,M,tot,ans;
LL h[MAXK+MAXC+],dist[MAXK+MAXC+][MAXK+MAXC+],
U[MAXC*+],V[MAXC*+],W[MAXC*+],Head[MAXC*+],
Next[MAXC*+],other[MAXC*+]; template <typename T> inline void read(T &x) {
LL f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c=='-') f = -f; }
for (; isdigit(c); c = getchar()) x=x*+c-'';
x*=f;
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x % + '');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} inline void floyed() {
LL i,j,k;
for (k = ; k <= K + C; k++) {
for (i = ; i <= K + C; i++) {
if (i == k) continue;
for (j = ; j <= K + C; j++) {
if ((k == j) || (i == j)) continue;
dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
} inline void add(LL a,LL b,LL c) {
++tot;
U[tot] = a; V[tot] = b; W[tot] = c;
Next[tot] = Head[a]; Head[a] = tot;
other[tot] = ++tot;
U[tot] = b; V[tot] = a; W[tot] = ;
Next[tot] = Head[b]; Head[b] = tot;
other[tot] = tot - ;
} inline bool BFS() {
LL i,x,y;
queue<LL> q;
memset(h,,sizeof(h));
h[st] = ; q.push(st);
while (!q.empty()) {
x = q.front(); q.pop();
for (i = Head[x]; i; i = Next[i]) {
y = V[i];
if ((W[i] > ) && (!h[y])) {
h[y] = h[x] + ;
q.push(y);
}
}
}
if (h[ed]) return true;
else return false;
} inline LL maxflow(LL x,LL f) {
LL i,t,y,sum=;
if (x == ed) return f;
for (i = Head[x]; i; i = Next[i]) {
y = V[i];
if ((W[i] > ) && (h[y] == h[x] + ) && (sum < f)) {
sum += (t = maxflow(y,min(W[i],f-sum)));
W[i] -= t; W[other[i]] += t;
}
}
if (!sum) h[x] = ;
return sum;
} inline bool check(LL ml) {
LL i,j,sum=;
tot = ;
memset(Head,,sizeof(Head));
for (i = K + ; i <= K + C; i++) {
for (j = ; j <= K; j++) {
if (dist[i][j] <= ml)
add(i,j,);
}
}
for (i = K + ; i <= K + C; i++) add(st,i,);
for (i = ; i <= K; i++) add(i,ed,M);
while (BFS()) {
sum += maxflow(st,C);
}
return sum == C;
} int main() { read(K); read(C); read(M);
st = K + C + ; ed = st + ; for (i = ; i <= K + C; i++) {
for (j = ; j <= K + C; j++) {
read(dist[i][j]);
if (!dist[i][j]) dist[i][j] = 2e9;
}
} floyed(); for (i = K + ; i <= K + C; i++) {
for (j = ; j <= K; j++) {
if (dist[i][j] != 2e9)
high = max(high,dist[i][j]);
}
} low = ; while (low <= high) {
mid = (low + high) >> ;
if (check(mid)) {
high = mid - ;
ans = mid;
} else
low = mid + ;
}
writeln(ans); return ; }

【USACO】Optimal Milking的更多相关文章

  1. 【poj2122】 Optimal Milking

    http://poj.org/problem?id=2112 (题目链接) 题意 有K个能挤M头奶牛的挤奶机和C头奶牛,告诉一些挤奶机和奶牛间距离,求最优分配方案使最大距离最小. Solution 先 ...

  2. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  3. 1642: 【USACO】Payback(还债)

    1642: [USACO]Payback(还债) 时间限制: 1 Sec 内存限制: 64 MB 提交: 190 解决: 95 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 &quo ...

  4. 1519: 【USACO】超级书架

    1519: [USACO]超级书架 时间限制: 1 Sec 内存限制: 64 MB 提交: 1735 解决: 891 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 Farmer Jo ...

  5. Java实现【USACO】1.1.2 贪婪的礼物送礼者 Greedy Gift Givers

    [USACO]1.1.2 贪婪的礼物送礼者 Greedy Gift Givers 题目描述 对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for th ...

  6. 【CPLUSOJ】【USACO】【差分约束】排队(layout)

    [题目描述] Robin喜欢将他的奶牛们排成一队.假设他有N头奶牛,编号为1至N.这些奶牛按照编号大小排列,并且由于它们都很想早点吃饭,于是就很可能出现多头奶牛挤在同一位置的情况(也就是说,如果我们认 ...

  7. 【USACO】Dining

    [题目链接] [JZXX]点击打开链接 [caioj]点击打开链接 [算法] 拆点+网络流 [代码] #include<bits/stdc++.h> using namespace std ...

  8. 【USACO】 Balanced Photo

    [题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ] ...

  9. 【USACO】 Balanced Lineup

    [题目链接] 点击打开链接 [算法] 这是一道经典的最值查询(RMQ)问题. 我们首先想到线段树.但有没有更快的方法呢?对于这类问题,我们可以用ST表(稀疏表)算法求解. 稀疏表算法.其实也是一种动态 ...

随机推荐

  1. 【Todo】Java学习路线(方向指导)

    在网上搜了下Java学习路线(关键词:学习,因为众所周知,实践出牛人,在平时工作不怎么深入的情况下,才强调学习的方向的重要性 ^_^) 发现下面知乎这个回答写的真好.mark如下: https://w ...

  2. Windows 无法卸载IE9怎么办

    1 如下图所示,使用自带的卸载工具无法卸载IE9 运行命令提示符,粘贴下面的命令 FORFILES /P %WINDIR%\servicing\Packages /M Microsoft-Window ...

  3. 百度地图 创建 自定义控件(vue)

    1.组件代码 Bmap.vue <!-- 离线地图 组件 --> <template> <div id="map" :style="styl ...

  4. 用redis实现跨服务器session(转)

    这个月我们新开发了一个项目,由于使用到了4台机器做web,使用dns做负载均衡, 上面图上用户通过DNS的调度(一个域名对应多个ip)分别访问到VM2-VM5上,四台机器都访问VM1上的redis,两 ...

  5. node JS 微信开发

    JS-SDK 要点 微信测试号; 扫码登录;无需认证(只是名称统一为微信测试号)http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/logi ...

  6. MyBatis学习(一):简单的运行

    1.准备工作 jar包: mybatis-3.4.4.jar,下载地址:https://github.com/mybatis/ignite-cache/releases mysql-connector ...

  7. vs2005 未能完成操作。未指定的错误

    具体解决过程是这样的: 1.先把.vcproj 文件剪切到其他地方 2.打开.sln,报错->点“确定”->再点“确定” 3.把 .vcproj 文件 放回来,在vs2008右边的“解决方 ...

  8. 继承的文本框控件怎么响应EN_CHANGE等消息

    继承的文本框控件如何响应EN_CHANGE等消息?我从CEdit继承了一个CMyEdit类,想在这个类里填写它的一些消息.我在消息映射表里写的是MESSAGE_HANDLER(EN_CHANGE, O ...

  9. 【BZOJ1085】[SCOI2005]骑士精神 双向BFS

    [BZOJ1085][SCOI2005]骑士精神 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它 ...

  10. java中Integer在JDK1.6和JDK1.7中的区别

    运行下面这段代码: System.out.println(Integer.valueOf("127")==Integer.valueOf("127")); Sy ...